Using Java to realize the effect of code character drawing

Algorithm and idea
As we all know, when our computer processes graphics, it actually divides the graphics into N pixels. For each pixel, it has its own pixel value. Countless pixels with their own specific pixel values are merged into the pictures we see. In Java, RGB value, pixel value and gray scale have the conversion relationship under certain formula conditions. Based on this, we get the RGB value by traversing each pixel on a picture, and then convert it into gray, and select the symbols with corresponding complexity according to the size of different gray.

Perform steps

  1. Read the file. Imageio is provided in Java The return value of the read method is BufferedImage.
  2. Set the string base from complex to simple, the visual sense from black to white, and the corresponding gray value from low to high.
  3. Traverse each pixel of the image file to be processed, get the R, G and b values, and then convert them into gray through the formula.
  4. The grayscale range is 0 ~ 255, that is, in the range of 0-255, the lower the grayscale value, the more the picture tends to black; The higher the gray value, the more white the picture is. Therefore, when the gray value is lower, we choose the more complex characters; The higher the gray value, the simpler the character we choose.
  5. Finally, print the characters corresponding to the gray down corresponding to each pixel.

PS: when selecting characters, for example, there are 10 characters in the string base, and the corresponding gray range is 0-255, then 0 - (255 / 10) is the lowest gray level, and select the most complex string; (255 / 10) - 2 * (255 / 10) is the next lower gray level. Select the second complex string, and so on.

This is why we should choose characters from complex to simple when setting the string base.

PPS: output results can be copied to txt text.

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class myPic {
	public void drawPic(String picPath) {
		BufferedImage img;
		String base = "@&%#M8XOHLTI!)=+;:,^.' ";
		try {
			img = ImageIO.read(new File(picPath));
			int width = img.getWidth();
			int height = img.getHeight();
			for(int y = 0; y<height;y+=4) {
				for (int x = 0;x<width;x+=4) {
					int pixel = img.getRGB(x, y);
					int r = (pixel & 0xff0000) >> 16, 
						g = (pixel & 0xff00) >> 8, 
						b = pixel & 0xff;
					double gray = 0.299f * r + 0.578f * g + 0.114f * b;
					int index = (int) Math.floor(gray*base.length()/256);
					char symbol = base.charAt(index);
					System.out.print(symbol);
				}
				System.out.println();
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	
		

	}
	public static void main (String[] args) {
		myPic picture = new myPic();
		picture.drawPic("D:\\YY.jpg");
	}
	
}

Finally, put two character drawings:

Tags: Java string

Posted by azunoman on Sat, 14 May 2022 19:40:41 +0300