liurunyu
2024-10-19 8456b4e16a9fe4284c45b6d56d7dabeea1cb18e9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
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();
        }
    }
}