仿抖音--将图片转化成对应的字符图片

起初看到抖音上的那个将图片转化成对应的字符图片的视频,心动不已,哇!这哥们也太有心了吧,虽说莉哥好看,但你做这图要花多长时间呀!值不值啊! 后来我才知道,java实现的话简直一秒钟的事。 第一开始只能支持小尺寸的图片,宽度200或者以下的,稍微大点的出来的效果就走样了。 于是我便在转码之前改变了一下图片的大小。 效果如下: 废话不多说,呈上代码。

import java.awt.Graphics;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.HeadlessException;
import java.awt.Image;
import java.awt.Transparency;
import java.awt.image.BufferedImage;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;

public class AsciiPic {
          
   
    /*
     * 将image转换成bufferimage
     */
    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 (FileNotFoundException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }//取源图
        int width = 200; //假设要缩小到200点像素
        int height = srcImg.getHeight(null)*200/srcImg.getWidth(null);//按比例,将高度缩减
        Image smallImg =srcImg.getScaledInstance(width, height, Image.SCALE_SMOOTH);//缩小
        return smallImg; 
    }
    /**
     * @param path
     *            图片路径
     */
    public static void createAsciiPic(BufferedImage bfimage) throws IOException {
        final String base = "@#&$%*o!;.";// 字符串由复杂到简单
        final BufferedImage image = bfimage;
        for (int y = 0; y < image.getHeight(); y += 2) {
            for (int x = 0; x < image.getWidth(); x++) {
                final int pixel = image.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();
        }
    }
    /**
     * test
     *
     * @param args
     */
    public static void main(final String[] args) {
        try {
            AsciiPic.createAsciiPic(toBufferedImage(creatImage("E:/imgtoASCII/wx.jpg")));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

最后,由于本人才疏学浅,不足之地,还恳请大家不吝赐教。

经验分享 程序员 微信小程序 职场和发展