The examples in this article share with you the specific code for Java to convert images into character drawings for your reference. The specific content is as follows
public class ImageProcesser { private static final char[] charset1 = {'M','8','V','|':','.',' '}; //Default character material set private char[] charset; //Character drawing material set private String imgString = ""; //Storing converted strings//Constructing public ImageProcesser(char[] charset){ this.charset = charset; } //Constructing public ImageProcesser(){ this.charset = charset1; } public String getImgString(){ return imgString; } /*Convert the graphic file into a character to draw a string*/ public ImageProcesser toBitmapConvert(String imagepath){ return toBitmapConvert(new File(imagepath)); } public ImageProcesser toBitmapConvert(File imageFile){ StringBuffer sb = new StringBuffer(); if(!imageFile.exists()){ //When the read file does not exist, end the program System.out.println("File is not exists!"); System.exit(1); } Color color; try{ BufferedImage buff = ImageIO.read(imageFile); //Load the image file such as BufferedImage stream buff = compressImage(buff); int bitmapH = buff.getHeight(); int bitmapW = buff.getWidth(); //Scan the pixels of the image progressively, read the RGB value, take its average value, and get the corresponding character material from the charset, and load it into sb for(int y=0; y<bitmapH; y++){ for(int x=0; x<bitmapW; x++){ int rgb = buff.getRGB(x,y); color = new Color(rgb); int cvalue = (color.getRed()+color.getGreen()+color.getBlue()) / 3; sb.append(charset[(int)((cvalue * charset.length - 1)/255)]+" "); } sb.append("/r/n"); } }catch(IOException ex){ ex.printStackTrace(); } imgString = sb.toString(); return this; } /* Image file preprocessing: compress the image to the longest edge to 100px*/ private BufferedImage compressImage(BufferedImage srcImg){ int h = srcImg.getHeight(); int w = srcImg.getWidth(); if(Math.max(h,w)<=100) return srcImg; int new_H; int new_W; if(w>h){ new_W = 100; new_H = 100*h/w ; }else{ new_H = 100; new_W = 100*w/h; } BufferedImage smallImg = new BufferedImage(new_W,new_H,srcImg.getType()); Graphics g = smallImg.getGraphics(); g.drawImage(srcImg,0,0,new_W,new_H,null); g.dispose(); return smallImg; } /*Save the string as a .txt file*/ public void saveAsTxt(String fileName){ try{ PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(fileName))); for(int i = 0;i<imgString.length();i++){ out.print(imgString.charAt(i)); } out.close(); }catch(IOException ex){ ex.printStackTrace(); } } /*Batch image file*/ public static void batchImgFile(String srcfile, String tragetfile){ File folder = new File(tragetfile); //Folder that generates the image File srcfolder = new File(srcfile); if(!folder.exists() || !folder.isDirectory()) folder.mkdirs(); ImageProcesser processor = new ImageProcesser(); File[] filelist = srcfolder.listFiles(); for(int i=0;i<filelist.length;i++){ if(!filelist[i].isFile()) continue; processor.toBitmapConvert(filelist[i]); processor.saveAsTxt(tragetfile+"/"+(i+1)+".txt"); System.out.println(filelist[i].getName()+" is converted!"); } System.out.println("All img were converted!"); } }Click to view: Reference link.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.