Java | 将图片转换为txt文件

一、描述

        将图片转成字符画的形式,存储在 txt 文件里面。

二、源代码

ImageConvertTool.java

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

public class ImageConvertTool {
    private static String imagePath, txtPath;
    private static int imageHeight, imageWidth;
    private static String replaceString = "$@B%8&WM#*oahkbmZO0QJUYXzcvunxrjft/|()1{}[]?-_+~<>i!lI;:,^`'. ";
    private static StringBuffer imageString = new StringBuffer();

    public ImageConvertTool(String imagePath, String txtPath) {
        this.imagePath = imagePath;
        this.txtPath = txtPath;
        imageProcess();
        imageSave();
    }
    private static void imageProcess() {
        try {
            BufferedImage image = ImageIO.read(new File(imagePath));
            imageHeight = image.getHeight();
            imageWidth = image.getWidth();
            for (int height = 0; height < imageHeight; height += 2) {
                for (int width = 0; width < imageWidth; width++) {
                    // 像素点RBG值获取
                    int pixel = image.getRGB(width, height);
                    int R = (pixel & 0xff0000) >> 16;
                    int B = pixel & 0xff;
                    int G = (pixel & 0xff00) >> 8;

                    // 灰度计算公式
                    float pixelGray = 0.299f * R + 0.587f * G + 0.114f * B;
                    int pixelIndex = Math.round(pixelGray * (replaceString.length() + 1) / 255);

                    // 将灰度值转成字符
                    String pixelChar = pixelIndex >= replaceString.length() ? " " : String.valueOf(replaceString.charAt(pixelIndex));

                    // 控制台上输出
                    System.out.print(pixelChar);

                    // 添加到imageString中
                    imageString.append(pixelChar);
                }
                System.out.println();
                imageString.append('\n');
            }
        } catch (IOException error) {
            error.printStackTrace();
        }
    }

    private static void imageSave() {
        File file = new File(txtPath);
        if (!file.exists())
            try {
                file.createNewFile();
            } catch (Exception error) {
                error.printStackTrace();
            }
        try {
            FileWriter fileWriter = new FileWriter(file);
            String temp = new String(imageString);
            fileWriter.write(temp);
            fileWriter.close();
        } catch (Exception error) {
            error.printStackTrace();
        }
    }
}

Main.java

public class Main {
    public static void main(String []args){
        // 输入保存的txt文件路径
        String txtPath = "输入保存的txt文件路径";
        // 输入准备转换的图片路径
        String imagePath = "输入准备转换的图片路径";
        new ImageConvertTool(imagePath,txtPath);
    }
}

三、效果

        准备转换的图片:

         保存到txt中(注意:打开txt文件后还要进行放大缩小进行调节,一般要把txt文件的字体改为宋体或者楷体,效果更好):

 

 

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值