zoukankan      html  css  js  c++  java
  • 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();
            }
        }
    }
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.
  • 相关阅读:
    77. Combinations (Recursion)
    90. Subsets II (Back-Track, DP)
    78. Subsets (Back-Track, DP)
    131. Palindrome Partitioning (Back-Track, DP)
    41. First Missing Positive (HashTable)
    49. Group Anagrams (string, HashTable)
    76. Minimum Window Substring (String, Map)
    Leetcode Intersection of Two Linked Lists
    Cocos2d-js 开发记录:基本图形绘制
    Cocos2d-js 开发记录:骨骼动画载入
  • 原文地址:https://www.cnblogs.com/name-lizonglin/p/12187508.html
Copyright © 2011-2022 走看看