Skip to content

Commit 24b90f8

Browse files
committed
feat: 字符图案,我用字符画个冰墩墩(https://www.wdbyte.com/java/char-image.html)
1 parent 723eba6 commit 24b90f8

8 files changed

Lines changed: 135 additions & 1 deletion

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ Java 版本任你发,我用 Java 8 。但是多学点这种装x技巧总没错
142142

143143
## ⏳ Java 开发
144144

145+
- [字符图案,我用字符画个冰墩墩](https://www.wdbyte.com/java/char-image.html)
145146
- [Java 中 RMI 的使用](https://www.wdbyte.com/2021/05/java/java-rmi/)
146147
- [如何使用 Github Actions 自动抓取每日必应壁纸?](https://www.wdbyte.com/2021/03/bing-wallpaper-github-action/)
147148
- [三种骚操作绕过迭代器遍历时的数据修改异常](https://www.wdbyte.com/2021/02/develop/interator-update/)

core-java-modules/README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,6 @@
2929

3030
- [Java 中的监控与管理原理概述](https://www.wdbyte.com/java/monitoring.html)
3131
- [使用 JMX 监控和管理 Java 程序](https://www.wdbyte.com/java/jmx.html)
32-
- [Java 中的 5 个代码性能提升技巧](https://www.wdbyte.com/java/code-5-tips.html)
32+
- [Java 中的 5 个代码性能提升技巧](https://www.wdbyte.com/java/code-5-tips.html)
33+
34+
- [字符图案,我用字符画个冰墩墩](https://www.wdbyte.com/java/char-image.html)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
## core-java-performance-code
2+
当前模块包含 IO 相关代码
3+
4+
### 相关文章
5+
6+
- [字符图案,我用字符画个冰墩墩](https://www.wdbyte.com/java/char-image.html)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>core-java-modules</artifactId>
7+
<groupId>com.wdbyte.core-java-modules</groupId>
8+
<version>1.0.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
<artifactId>core-java-io</artifactId>
12+
<version>1.0.0-SNAPSHOT</version>
13+
<name>core-java-io</name>
14+
<packaging>jar</packaging>
15+
16+
<properties>
17+
<maven.compiler.source>1.8</maven.compiler.source>
18+
<maven.compiler.target>1.8</maven.compiler.target>
19+
</properties>
20+
21+
</project>
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
package com.wdbyte.io.image;
2+
3+
import java.awt.*;
4+
import java.awt.image.BufferedImage;
5+
import java.io.File;
6+
import java.io.IOException;
7+
8+
import javax.imageio.ImageIO;
9+
10+
/**
11+
* <p>
12+
* 根据图片生成字符图案
13+
* 1.图片大小缩放
14+
* 2.遍历图片像素点
15+
* 3.获取图片像素点亮度
16+
* 4.匹配字符
17+
* 5.输出图案
18+
*
19+
* @website https://www.wdbyte.com
20+
*/
21+
public class GeneratorTextImage {
22+
23+
public static void main(String[] args) throws Exception {
24+
BufferedImage image = resizeImage("/Users/darcy/Downloads/sanli.jpg", 120);
25+
printImage(image);
26+
}
27+
28+
/**
29+
* 图片缩放
30+
*
31+
* @param srcImagePath 图片路径
32+
* @param targetWidth 目标宽度
33+
* @return
34+
* @throws IOException
35+
*/
36+
public static BufferedImage resizeImage(String srcImagePath, int targetWidth) throws IOException {
37+
Image srcImage = ImageIO.read(new File(srcImagePath));
38+
int targetHeight = getTargetHeight(targetWidth, srcImage);
39+
BufferedImage resizedImage = new BufferedImage(targetWidth, targetHeight, BufferedImage.TYPE_INT_RGB);
40+
Graphics2D graphics2D = resizedImage.createGraphics();
41+
graphics2D.drawImage(srcImage, 0, 0, targetWidth, targetHeight, null);
42+
graphics2D.dispose();
43+
return resizedImage;
44+
}
45+
46+
/**
47+
* 图片缩放
48+
*
49+
* @param srcImagePath 图片路径
50+
* @param targetWidth 目标宽度
51+
* @return
52+
* @throws IOException
53+
*/
54+
public static BufferedImage resizeImage2(String srcImagePath, int targetWidth) throws IOException {
55+
Image srcImage = ImageIO.read(new File(srcImagePath));
56+
int targetHeight = getTargetHeight(targetWidth, srcImage);
57+
Image image = srcImage.getScaledInstance(targetWidth, targetHeight, Image.SCALE_DEFAULT);
58+
BufferedImage bufferedImage = new BufferedImage(targetWidth, targetHeight, BufferedImage.TYPE_INT_RGB);
59+
bufferedImage.getGraphics().drawImage(image, 0, 0, null);
60+
return bufferedImage;
61+
}
62+
63+
/**
64+
* 根据指定宽度,计算等比例高度
65+
*
66+
* @param targetWidth 目标宽度
67+
* @param srcImage 图片信息
68+
* @return
69+
*/
70+
private static int getTargetHeight(int targetWidth, Image srcImage) {
71+
int targetHeight = srcImage.getHeight(null);
72+
if (targetWidth < srcImage.getWidth(null)) {
73+
targetHeight = Math.round((float)targetHeight / ((float)srcImage.getWidth(null) / (float)targetWidth));
74+
}
75+
return targetHeight;
76+
}
77+
78+
/**
79+
* 图片打印
80+
*
81+
* @param image
82+
* @throws IOException
83+
*/
84+
public static void printImage(BufferedImage image) throws IOException {
85+
final char[] PIXEL_CHAR_ARRAY = {'W', '@', '#', '8', '&', '*', 'o', ':', '.', ' '};
86+
int width = image.getWidth();
87+
int height = image.getHeight();
88+
for (int i = 0; i < height; i++) {
89+
for (int j = 0; j < width; j++) {
90+
int rgb = image.getRGB(j, i);
91+
Color color = new Color(rgb);
92+
int red = color.getRed();
93+
int green = color.getGreen();
94+
int blue = color.getBlue();
95+
// 一个用于计算RGB像素点灰度的公式
96+
Double grayscale = 0.2126 * red + 0.7152 * green + 0.0722 * blue;
97+
double index = grayscale / (Math.ceil(255 / PIXEL_CHAR_ARRAY.length) + 0.5);
98+
System.out.print(PIXEL_CHAR_ARRAY[(int)(Math.floor(index))]);
99+
}
100+
System.out.println();
101+
}
102+
}
103+
104+
}
22.4 KB
Loading
133 KB
Loading
45 KB
Loading

0 commit comments

Comments
 (0)