码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2020-01-16 14:32:24      阅读:144      评论:0      收藏:0      [点我收藏+]

标签:函数   版本   turn   red   try   pix   技术   top   stack   

普通图片转换为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();
        }
    }
}

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

标签:函数   版本   turn   red   try   pix   技术   top   stack   

原文地址:https://www.cnblogs.com/name-lizonglin/p/12187508.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!