本文主要介绍了25行java代码将普通图片转换为字符画图片和文本的实现,分享给大家,具体如下:

原图

生成字符画文本(像素转换字符显示后,打开字符画显示相当于原图的好几倍大,不要用记事本打开,建议用notepad++等软件打开)

生成字符画图片(背景颜色和画笔颜色代码里可设置调节)

新建普通java 项目,java单类实现代码,复制到java项目中,用idea编辑器 主方法运行。(引入的class 都是jdk中自有的)

import javax.imageio.imageio;
import java.awt.*;
import java.awt.image.bufferedimage;
import java.io.*;
 
public class imagetoascii {
 
    //三十二位颜色
    private final static char[] color = {' ', '`', '.', '^', ',', ':', '~', '"',
            '<', '!', 'c', 't', '+', '{', 'i', '7', '?', 'u', '3', '0', 'p', 'w',
            '4', 'a', '8', 'd', 'x', '%', '#', 'h', 'w', 'm'};
 
 
    /**
     * 图片转字符画文本
     */
    public static void createchartxt(string path, string fileurl) {
        try {
            bufferedimage image = imageio.read(new file(path));
            stringbuilder imagetoascii = imagetoascii(image);
            filewriter filewriter = new filewriter(fileurl);
            filewriter.write(imagetoascii.tostring());
        } catch (ioexception e) {
            e.printstacktrace();
        }
    }
 
 
    /**
     * 图片转字符
     */
    public static stringbuilder imagetoascii(bufferedimage image) {
        stringbuilder sb = new stringbuilder();
        int width = image.getwidth();
        int height = image.getheight();
        for (int y = 0; y < height; y++) {
            for (int x= 0; x < width; x++) {
                sb.append(rgbtochar(image,x,y)+ " ");
            }
            sb.append("\n");
        }
        return sb;
    }
 
    /**
     * 像素转字节
     */
    public static char rgbtochar(bufferedimage image,int x,int y){
        int rgb = image.getrgb(x, y);
        int r = (rgb & 0xff0000) >> 16;
        int g = (rgb & 0x00ff00) >> 8;
        int b = rgb & 0x0000ff;
        int gray = (r * 30 + g * 59 + b * 11 + 50) / 100;
        int index = 31 * gray / 255;
        return color[index];
    }
 
    /**
     * @description: 生成字符码图片
     * @param  imgpath 图片路径
     * @param  fileurl 输出图片路径
     * @param  more 放大倍数
     * @throws
     */
    public static void createcharimg(string imgpath, string fileurl,int more){
        try {
            fileinputstream fileinputstream = new fileinputstream(imgpath);
            bufferedimage image = imageio.read(fileinputstream);
            //生成字符图片
            int w = image.getwidth();
            int h = image.getheight();
            bufferedimage imagebuffer = new bufferedimage(w*more, h*more, 1);;
            graphics g = imagebuffer.getgraphics();
            //设置背景色
            g.setcolor(color.white);// 画笔颜色
            g.fillrect(0, 0, w*more, h*more);// 填充图形背景
            // 设置字体
            g.setfont(new font("宋体", font.italic, more)); //more*2:字体高度
            g.setcolor(color.black);// 画笔颜色
            // 绘制字符
            for (int y = 0; y < h; y++) {
                for (int x = 0; x< w; x++) {
                    g.drawstring(rgbtochar(image,x,y)+ " ", x*more, (y+1)*more); //绘制每行字体位置,主要y轴改变
                }
            }
            g.dispose();
            imageio.write(imagebuffer, "jpg", new file(fileurl)); //输出图片
            system.out.println("字符画图片生成");
        } catch (exception e) {
            e.printstacktrace();
        }
    }
 
    /**
     * @description: 生成字符码图片
     * @param  imgpath 图片路径
     * @param  fileurl 输出图片路径
     * @more 放大倍数
     */
    public static void createcharimg(string imgpath, string fileurl){
        createcharimg(imgpath,fileurl,1);
    }
  
 
    public static void main(string[] args) throws ioexception {
        createcharimg("c:\\users\\tarzan\\desktop\\home.jpg","c:\\users\\tarzan\\desktop\\a.jpg");
        createchartxt("c:\\users\\tarzan\\desktop\\home.jpg","c:\\users\\tarzan\\desktop\\a.txt");
    }
}

运行结果截图

到此这篇关于25行java代码将普通图片转换为字符画图片和文本的实现的文章就介绍到这了,更多相关java图片转换为字符画 内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!