Java_图片转字符
把高达头像转换成字符[-V-]
调节双循环里y与x的增量改变字符输出的细节、高和长
1 public class ImgToStr { 2 public static void main(String args []){ 3 //图片路径 4 ImgToStr.createAsciiPic("D:\\StrLogo\\GUTy6x2.png"); 5 } 6 7 public static void createAsciiPic(final String path) { 8 //final String base = "@#&$%*o!;.";// 字符串由复杂到简单 9 final String base = "/lVXvi*!;.'"; 10 try { 11 final BufferedImage image = ImageIO.read(new File(path)); 12 System.out.println("W:"+image.getWidth()+" H:"+image.getHeight()); 13 for (int y = 0; y < image.getHeight(); y += 6) { 14 for (int x = 0; x < image.getWidth(); x+= 2) { 15 final int pixel = image.getRGB(x, y); 16 final int r = (pixel & 0xff0000) >> 16, g = (pixel & 0xff00) >> 8, b = pixel & 0xff; 17 final float gray = 0.299f * r + 0.578f * g + 0.114f * b; 18 final int index = Math.round(gray * (base.length() + 1) / 255); 19 System.out.print(index >= base.length() ? " " : String.valueOf(base.charAt(index))); 20 } 21 System.out.println(); 22 } 23 } catch (final IOException e) { 24 e.printStackTrace(); 25 } 26 } 27 }