把高達頭像轉換成字符[-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 }
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/162685.html
標籤:Java
