(小工具)将图片转换为0,1组成的文本

这篇文章介绍了如何以文本的形式来展示图片,以前做过这个程序,后来找不到了,重新记录一下.

代码

import net.coobird.thumbnailator.Thumbnails;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
import java.nio.charset.StandardCharsets;

public class ImageUtil {

    public static void main(String[] args) throws IOException {
        String fromPic = "C:\\Users\\Public\\Pictures\\Sample Pictures\\507597_240.jpg";
        String toTxt = "d:\\test5.txt";
        BufferedImage bufferedImage = ImageIO.read(new File(fromPic));
        // 1.压缩图片
        BufferedImage compactImage = Thumbnails.of(bufferedImage).size(100, 500).asBufferedImage();
        // 2.灰度化
        BufferedImage grayImage = grayingImage(compactImage);
        // 3.二值化
        BufferedImage binaryImage = binaryImage(grayImage);
        // 4.输出到txt文本
        writeToTxt(binaryImage, toTxt);

        
        /*保存图片
        File newFile = new File("d:\\test5.jpg");
        ImageIO.write(binaryImage, "jpg", newFile);*/
    }

    /**
     * 灰度化图片
     * @param bufferedImage 原图片
     * @return 灰度化之后的图片
     */
    private static BufferedImage grayingImage(BufferedImage bufferedImage) {
        BufferedImage grayImage = new BufferedImage(bufferedImage.getWidth(), bufferedImage.getHeight(), 
                BufferedImage.TYPE_BYTE_GRAY);
        for (int i = 0; i < bufferedImage.getWidth(); i++) {
            for (int j = 0; j < bufferedImage.getHeight(); j++) {
                int color = bufferedImage.getRGB(i, j);
                grayImage.setRGB(i, j, color);
            }
        }
        return grayImage;
    }

    /**
     * 二值化图片
     * @param bufferedImage 原图片
     * @return 二值化后的图片
     */
    private static BufferedImage binaryImage(BufferedImage bufferedImage) {
        BufferedImage grayImage = new BufferedImage(bufferedImage.getWidth(), bufferedImage.getHeight(), bufferedImage.getType());
        int threshold = getMeanThreshold(bufferedImage);
        for (int i = 0; i < bufferedImage.getWidth(); i++) {
            for (int j = 0; j < bufferedImage.getHeight(); j++) {
                int color = bufferedImage.getRGB(i, j);
                int r = (color >> 16) & 0xff;
                int g = (color >> 8) & 0xff;
                int b = color & 0xff;
                int gray = (int) (0.3 * r + 0.59 * g + 0.11 * b);
                if (gray > threshold) {
                    grayImage.setRGB(i, j, 0xFFFFFF);
                }
                else {
                    grayImage.setRGB(i, j, 0);
                }
            }
        }
        return grayImage;
    }

    /**
     * 获取图片的阀值,采用基于灰度平均值的阈值
     * @param bufferedImage 原图片
     * @return 二值化的阈值
     */
    private static int getMeanThreshold(BufferedImage bufferedImage) {
        int w = bufferedImage.getWidth();
        int h = bufferedImage.getHeight();
        int num = 0;
        int sum = 0;
        for(int i=0; i<w; i++) {
            for(int j = 0; j < h; j++) {
                int color = bufferedImage.getRGB(i, j);
                int r = (color >> 16) & 0xff;
                int g = (color >> 8) & 0xff;
                int b = color & 0xff;
                int gray = (int) (0.3 * r + 0.59 * g + 0.11 * b);
                sum += gray;
                num += 1;
            }
        }

        // 测试表明,阀值取平均值的1.2倍效果最好。
        int threshold = sum / num;
        if(threshold * 1.2 < 255) {
            threshold = (int)(1.2 * sum / num);
        }
        System.out.println("width: " + w + " height: " + h + " threshold: " + threshold);
        return threshold;
    }

    /**
     * 输出 0,1 TXT文本
     */
    public static void writeToTxt(BufferedImage bufferedImage, String toSaveFilePath) {
        File file = new File(toSaveFilePath);
        try {
            Writer writer = new OutputStreamWriter(new FileOutputStream(file, true), StandardCharsets.UTF_8);
            StringBuilder builder = new StringBuilder();
            for (int j = 0; j < bufferedImage.getHeight(); j++) {
                for(int i = 0; i < bufferedImage.getWidth(); i++) {
                    int color = bufferedImage.getRGB(i, j);
                    if(color == -1) {
                        builder.append(" ");
                    }
                    else {
                        builder.append("0");
                    }
                }
                builder.append("\r\n");
            }
            writer.write(builder.toString());
            writer.close();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }

}

效果

在这里插入图片描述

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值