编写代码时间长了,满屏幕的字符看着有些枯燥,如果能在字符中出现一幅画,应该能增加一些趣味,使屏幕看起来不那么枯燥。
于是百度了一下,发现知乎上有个问题https://www.zhihu.com/question/38638731/answer/132069108,回答的有道理,但用matlab,这个目前还不大了解,于是决定用Java实现一个。
1.首先提取图片的RGB值,代码参考CSDN一位作者,http://blog.csdn.net/ubuntu_yanglei/article/details/46443929。
2.提取好RGB后根据知乎上面的RGB2GRAY原理计算出一个gray的二维数组。
3.根据作者提供的黑白颜色字符集,进行相应的转换和输出,done!
代码:
public class ReadColorTest {
/**
* 读取一张图片的RGB值
*
* @throws Exception
*/
public void getImagePixel(String image) throws Exception {
int[] rgb = new int[3];
File file = new File(image);
BufferedImage bi = null;
try {
bi = ImageIO.read(file);
} catch (Exception e) {
e.printStackTrace();
}
int width = bi.getWidth();
int height = bi.getHeight();
int minx = bi.getMinX();
int miny = bi.getMinY();
System.out.println("width=" + width + ",height=" + height + ".");
System.out.println("minx=" + minx + ",miniy=" + miny + ".");
float[][] gray = new float[width][height];
for (int i = minx; i < width; i++) {
for (int j = miny; j < height; j++) {
int pixel = bi.getRGB(i, j); // 下面三行代码将一个数字转换为RGB数字
rgb[0] = (pixel & 0xff0000) >> 16;
rgb[1] = (pixel & 0xff00) >> 8;
rgb[2] = (pixel & 0xff);
System.out.println("i=" + i + ",j=" + j + ":(" + rgb[0] + ","
+ rgb[1] + "," + rgb[2] + ")");
gray[i][j] = rgb[0]*0.29900f + rgb[1]*0.58700f+rgb[2]*0.11400f;
}
}
float[][] transformGray = new float[height][width];
for (int i = 0; i < gray.length; i++) {
for (int j = 0; j < gray[i].length; j++) {
transformGray[j][i]=gray[i][j];
}
}
String strs = "$@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\\|()1{}[]?-_+~<>i!lI;:,\\\"^`'";
char[] array = strs.toCharArray();
File file2 = new File("z:\\car.txt");
if (file2.exists()) {
file2.delete();
}else {
file2.createNewFile();
}
FileWriter fileWriter = new FileWriter(file2);
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
for (int i = 0; i < transformGray.length; i++) {
StringBuilder sb = new StringBuilder();
for (int j = 0; j < transformGray[i].length; j++) {
//System.out.println("value:"+(int) Math.ceil(gray[i][j]/3.7f));
int index = (int) Math.ceil(transformGray[i][j]/3.7f)-1;
if (index < 0) {
index=0;
}
if (index >= array.length) {
index = array.length - 1;
}
sb.append(array[index]);
System.out.print(array[index]);
}
bufferedWriter.write(sb.toString());
bufferedWriter.newLine();
System.out.println();
}
bufferedWriter.flush();
bufferedWriter.close();
}
/**
* 返回屏幕色彩值
*
* @param x
* @param y
* @return
* @throws AWTException
*/
public int getScreenPixel(int x, int y) throws AWTException { // 函数返回值为颜色的RGB值。
Robot rb = null; // java.awt.image包中的类,可以用来抓取屏幕,即截屏。
rb = new Robot();
Toolkit tk = Toolkit.getDefaultToolkit(); // 获取缺省工具包
Dimension di = tk.getScreenSize(); // 屏幕尺寸规格
System.out.println(di.width);
System.out.println(di.height);
Rectangle rec = new Rectangle(0, 0, di.width, di.height);
BufferedImage bi = rb.createScreenCapture(rec);
int pixelColor = bi.getRGB(x, y);
return 16777216 + pixelColor; // pixelColor的值为负,经过实践得出:加上颜色最大值就是实际颜色值。
}
/**
* @param args
*/
public static void main(String[] args) throws Exception {
int x = 0;
ReadColorTest rc = new ReadColorTest();
x = rc.getScreenPixel(100, 345);
System.out.println(x + " - ");
rc.getImagePixel("z:\\car5.jpg");
}
}
异常情况:在首次输出的字符画中发现图片竟然是竖着的,并且方向也不对,检查后发现RGB信息的width和height需要分别作为二维数组的列数和行数,于是二维数组进行矩阵转置,字符画即可正常方向。
[20230225]
虽然目前的功能实现了,但是输出的字符画有一个很大的问题,纵向也就是垂直方向明显被拉长了,即便是一个正方形的图片生成的字符画也是这样,这是一个巨大的问题,有待解决。后续再说吧。

870

被折叠的 条评论
为什么被折叠?



