25行Java代码将普通图片转换为字符画图片和文本的实现
作者:洛阳泰山 时间:2023-11-24 02:04:26
本文主要介绍了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");
}
}
运行结果截图
来源:https://blog.csdn.net/weixin_40986713/article/details/116045299
标签:Java,图片,转换,字符画
0
投稿
猜你喜欢
使用@PropertySource读取配置文件通过@Value进行参数注入
2021-11-21 13:21:03
EditText限制小数点前后位数的实例
2023-04-10 03:56:14
Spring Boot将项目打包成war包的操作方法
2022-01-02 22:30:50
C#实现数字华容道游戏
2023-10-26 10:01:07
Andriod使用多线程实现轮播图片
2023-07-27 15:47:31
基于Java实现的Dijkstra算法示例
2021-09-17 02:51:13
android编程之ip2id程序实例
2023-07-20 16:08:09
Java日期时间操作的方法
2021-12-27 18:36:05
Java流程控制语句之If选择结构
2023-11-11 04:02:29
Spring AOP底层源码详解
2022-03-12 02:00:26
如何使用两个栈实现队列Java
2023-11-29 17:48:09
Android手机抓包步骤
2022-05-03 18:15:59
带你了解如何使用Spring基于ProxyFactoryBean创建AOP代理
2022-04-28 09:17:37
Java操作MongoDB数据库的示例代码
2023-11-23 04:15:51
详解Java token主流框架之JWT
2022-03-30 19:30:34
AjaxControlToolkit AjaxFileUpload 显示英文改成中文的解决方法
2021-06-01 21:09:57
MyBatis中的properties配置(推荐)
2023-06-04 20:47:58
java.lang.UnsatisfiedLinkError: %1 不是有效的Win32应用程序错误解决
2022-06-14 23:21:51
Java中scheduleAtFixedRate的用法
2022-10-10 11:07:37
详解LINQ入门(上篇)
2023-10-15 05:57:30