| New file | 
 |  |  | 
 |  |  | package com.dy.common.util; | 
 |  |  |  | 
 |  |  | import javax.imageio.ImageIO; | 
 |  |  | import javax.swing.*; | 
 |  |  | import java.awt.*; | 
 |  |  | import java.awt.image.BufferedImage; | 
 |  |  | import java.io.FileInputStream; | 
 |  |  | import java.io.IOException; | 
 |  |  |  | 
 |  |  | /** | 
 |  |  |  * 图片转化Ascii | 
 |  |  |  */ | 
 |  |  | public class AsciiPic { | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  |      * 将image转换成 BufferedImage | 
 |  |  |      * | 
 |  |  |      */ | 
 |  |  |     public static BufferedImage toBufferedImage(Image image) { | 
 |  |  |         if (image instanceof BufferedImage) { | 
 |  |  |             return (BufferedImage)image; | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |         // 加载所有像素 | 
 |  |  |         image = new ImageIcon(image).getImage(); | 
 |  |  |         BufferedImage bImage = null; | 
 |  |  |         GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); | 
 |  |  |         try { | 
 |  |  |             int transparency = Transparency.OPAQUE; | 
 |  |  |  | 
 |  |  |             // 创建buffer图像 | 
 |  |  |             GraphicsDevice gs = ge.getDefaultScreenDevice(); | 
 |  |  |             GraphicsConfiguration gc = gs.getDefaultConfiguration(); | 
 |  |  |             bImage = gc.createCompatibleImage(image.getWidth(null), image.getHeight(null), transparency); | 
 |  |  |         } catch (HeadlessException e) { | 
 |  |  |             e.printStackTrace(); | 
 |  |  |         } | 
 |  |  |         if (bImage == null) { | 
 |  |  |             int type = BufferedImage.TYPE_INT_RGB; | 
 |  |  |             bImage = new BufferedImage(image.getWidth(null), image.getHeight(null), type); | 
 |  |  |         } | 
 |  |  |         // 复制 | 
 |  |  |         Graphics g = bImage.createGraphics(); | 
 |  |  |         // 赋值 | 
 |  |  |         g.drawImage(image, 0, 0, null); | 
 |  |  |         g.dispose(); | 
 |  |  |         return bImage; | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     public static Image creatImage(String ImgPath) { | 
 |  |  |         Image srcImg = null; | 
 |  |  |         try { | 
 |  |  |             srcImg = ImageIO.read(new FileInputStream(ImgPath)); | 
 |  |  |         } catch (Exception e1) { | 
 |  |  |             e1.printStackTrace(); | 
 |  |  |         } | 
 |  |  |         Image smallImg = null ; | 
 |  |  |         if(srcImg != null){ | 
 |  |  |             //取源图 | 
 |  |  |             int width = 60; //假设要缩小到200点像素 | 
 |  |  |             int height = srcImg.getHeight(null)*60/srcImg.getWidth(null);//按比例,将高度缩减 | 
 |  |  |             smallImg = srcImg.getScaledInstance(width, height, Image.SCALE_SMOOTH);//缩小 | 
 |  |  |         } | 
 |  |  |         return smallImg; | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  |      * @param bfImage  图片路径 | 
 |  |  |      */ | 
 |  |  |     public static void createAsciiPic(BufferedImage bfImage) throws IOException { | 
 |  |  |         final String base = "@#&$O";// 字符串由复杂到简单 | 
 |  |  |         for (int y = 0; y < bfImage.getHeight(); y += 2) { | 
 |  |  |             for (int x = 0; x < bfImage.getWidth(); x++) { | 
 |  |  |                 final int pixel = bfImage.getRGB(x, y); | 
 |  |  |                 final int r = (pixel & 0xff0000) >> 16, g = (pixel & 0xff00) >> 8, b = pixel & 0xff; | 
 |  |  |                 final float gray = 0.299f * r + 0.578f * g + 0.114f * b; | 
 |  |  |                 final int index = Math.round(gray * (base.length() + 1) / 255); | 
 |  |  |                 System.out.print(index >= base.length() ? " " : String.valueOf(base.charAt(index))); | 
 |  |  |             } | 
 |  |  |             System.out.println(); | 
 |  |  |         } | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     public static void main(final String[] args) { | 
 |  |  |         try { | 
 |  |  |             AsciiPic.createAsciiPic(toBufferedImage(creatImage("DY.png"))); | 
 |  |  |         } catch (IOException e) { | 
 |  |  |             e.printStackTrace(); | 
 |  |  |         } | 
 |  |  |     } | 
 |  |  | } |