You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
35 lines
1.3 KiB
35 lines
1.3 KiB
import javax.imageio.ImageIO;
|
|
import java.awt.image.BufferedImage;
|
|
import java.io.File;
|
|
import java.io.IOException;
|
|
import java.io.*;
|
|
|
|
public class test03 {
|
|
public static void main(String[] args) {
|
|
jpgTostring("D:\\Java库\\福禄娃.jpg");
|
|
}
|
|
public static void jpgTostring(String imagePath) {
|
|
String base = "MNHQ&OC?7>!;:-. ";
|
|
try {
|
|
PrintWriter out = new PrintWriter("D:\\Java库\\福禄娃.txt");
|
|
BufferedImage image = ImageIO.read(new File(imagePath)); //读取图片
|
|
//输出到指定文件中
|
|
for (int y = 0; y < image.getHeight(); y+=2) {
|
|
for (int x = 0; x < image.getWidth(); x++) {
|
|
int pixel = image.getRGB(x, y);
|
|
int r = (pixel & 0xff0000) >> 16, g = (pixel & 0xff00) >> 8, b = pixel & 0xff;//分别获取 r、g、b 的值
|
|
|
|
float gray = 0.3f * r + 0.59f * g + 0.11f * b;//灰度计算公式
|
|
|
|
int index = Math.round(gray * (base.length() + 1) / 255);
|
|
String s = index >= base.length() ? " " : String.valueOf(base.charAt(index));
|
|
out.print(s);
|
|
}
|
|
out.println();
|
|
}
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
}
|