java实现图片转字符图(看的过去的亚子)

普通图片转换为ASSIC码灰度图片

  原图:

    

 

 

  效果图:

    

 

 

转换方法

  • 读取图片文件到BufferedImage
  • 读取BufferedImage中的RGB值
  • 将RGB三色值按照(0.3,0.59,0.11)权重获取灰度值(据说是眼睛对RGB敏感度不同)
  • 将当前灰度值根据大小转换为ASSIC编码输出

代码:

  

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
/**
 * @Description:ლ【lious】ლ->图片转字符图
 * @Author: Mr.li
 * @Date: 2020/1/13
 */
public class sort {
    final static String regularString = "@#$&%*!^;,.";
    /**
     * @Description:ლ【】ლ->
     * @Param: [bufferedImage]
     * @Return: void
     * @Author: Mr.li
     * @Date: 2020/1/13
     */
    public static void convertImageToASSIC(BufferedImage bufferedImage) {
        int imageWidth = bufferedImage.getWidth();//得到宽
        int imageHeight = bufferedImage.getHeight();//得到高
        for (int coordinateX = 0; coordinateX < imageHeight; coordinateX += 5) {
            for (int coordinateY = 0; coordinateY < imageWidth; coordinateY += 2) {
                int orientRGB = bufferedImage.getRGB(coordinateY, coordinateX);//得到颜色空间
                int componentR = (orientRGB >> 16) & 0xff;//编译时加上了 GD 库 2.0 或更高的版本并且图像是真彩色图像,
                int componentG = (orientRGB >> 8) & 0xff; // 则本函数以整数返回该点的 RGB 值。用移位加掩码来取得红,绿,蓝各自成分的值.
                int componentB = orientRGB & 0xff;
                int pixelDegree = (int) (componentR * 0.3 + componentG * 0.59 + componentB * 0.11);//得到 image 所指定的图形中指定位置像素的颜色索引值。
                System.out.print(regularString.charAt(pixelDegree / 24));
            }
            System.out.println();
        }
    }

    public static void main(String[] args) {
        File imageFile = new File("/Users/lzl/Desktop/python/wf.jpg");
        System.out.println(imageFile.getAbsoluteFile());
        try {
            BufferedImage bufferedImage = ImageIO.read(imageFile);//读取图片
            convertImageToASSIC(bufferedImage);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
posted @ 2020-01-16 13:54  四叶草的诗雨  阅读(687)  评论(0编辑  收藏  举报