Skip to content

Commit 9f38ca3

Browse files
committed
springboot-banner 自定义banner
1 parent a628814 commit 9f38ca3

File tree

8 files changed

+208
-0
lines changed

8 files changed

+208
-0
lines changed

springboot-banner/.gitignore

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
HELP.md
2+
/target/
3+
!.mvn/wrapper/maven-wrapper.jar
4+
5+
### STS ###
6+
.apt_generated
7+
.classpath
8+
.factorypath
9+
.project
10+
.settings
11+
.springBeans
12+
.sts4-cache
13+
14+
### IntelliJ IDEA ###
15+
.idea
16+
*.iws
17+
*.iml
18+
*.ipr
19+
20+
### NetBeans ###
21+
/nbproject/private/
22+
/nbbuild/
23+
/dist/
24+
/nbdist/
25+
/.nb-gradle/
26+
/build/

springboot-banner/pom.xml

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<parent>
6+
<groupId>org.springframework.boot</groupId>
7+
<artifactId>spring-boot-starter-parent</artifactId>
8+
<version>2.1.3.RELEASE</version>
9+
<relativePath/> <!-- lookup parent from repository -->
10+
</parent>
11+
<groupId>net.codingme</groupId>
12+
<artifactId>banner</artifactId>
13+
<version>0.0.1-SNAPSHOT</version>
14+
<name>banner</name>
15+
<description>banner</description>
16+
17+
<properties>
18+
<java.version>1.8</java.version>
19+
</properties>
20+
21+
<dependencies>
22+
<dependency>
23+
<groupId>org.springframework.boot</groupId>
24+
<artifactId>spring-boot-starter-web</artifactId>
25+
</dependency>
26+
27+
<dependency>
28+
<groupId>org.projectlombok</groupId>
29+
<artifactId>lombok</artifactId>
30+
<optional>true</optional>
31+
</dependency>
32+
<dependency>
33+
<groupId>org.springframework.boot</groupId>
34+
<artifactId>spring-boot-starter-test</artifactId>
35+
<scope>test</scope>
36+
</dependency>
37+
</dependencies>
38+
39+
<build>
40+
<plugins>
41+
<plugin>
42+
<groupId>org.springframework.boot</groupId>
43+
<artifactId>spring-boot-maven-plugin</artifactId>
44+
</plugin>
45+
</plugins>
46+
</build>
47+
48+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package net.codingme.banner;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class BannerApplication {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(BannerApplication.class, args);
11+
}
12+
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
package net.codingme.banner.util;
2+
3+
import java.awt.*;
4+
import java.awt.image.BufferedImage;
5+
import java.io.File;
6+
import java.io.FileOutputStream;
7+
import java.io.IOException;
8+
9+
import javax.imageio.ImageIO;
10+
11+
/**
12+
* <p>
13+
* 根据图片生成字符图案
14+
* 1.图片大小缩放
15+
* 2.遍历图片像素点
16+
* 3.获取图片像素点亮度
17+
* 4.匹配字符
18+
* 5.输出图案
19+
*
20+
* @author niujinpeng
21+
* @website www.codingme.net
22+
* @date 2019-02-25 23:03:01
23+
*/
24+
25+
public class GeneratorTextImage {
26+
private static final char[] PIXEL = {'@', '#', '8', '&', 'o', ':', '*', '.', ' '};
27+
28+
public static void main(String[] args) throws Exception {
29+
// 图片缩放
30+
BufferedImage bufferedImage = makeSmallImage("src/main/resources/banner.jpg");
31+
// 输出
32+
printImage(bufferedImage);
33+
}
34+
35+
public static void printImage(BufferedImage image) throws IOException {
36+
int width = image.getWidth();
37+
int height = image.getHeight();
38+
for (int i = 0; i < height; i++) {
39+
for (int j = 0; j < width; j++) {
40+
int rgb = image.getRGB(j, i);
41+
Color color = new Color(rgb);
42+
int red = color.getRed();
43+
int green = color.getGreen();
44+
int blue = color.getBlue();
45+
// 一个用于计算RGB像素点亮度的公式
46+
Double luminace = 0.2126 * red + 0.7152 * green + 0.0722 * blue;
47+
double index = luminace / (Math.ceil(255 / PIXEL.length) + 0.5);
48+
System.out.print(PIXEL[(int)(Math.floor(index))]);
49+
}
50+
System.out.println();
51+
}
52+
}
53+
54+
public static BufferedImage makeSmallImage(String srcImageName) throws Exception {
55+
File srcImageFile = new File(srcImageName);
56+
if (srcImageFile == null) {
57+
System.out.println("文件不存在");
58+
return null;
59+
}
60+
FileOutputStream fileOutputStream = null;
61+
BufferedImage tagImage = null;
62+
Image srcImage = null;
63+
try {
64+
srcImage = ImageIO.read(srcImageFile);
65+
int srcWidth = srcImage.getWidth(null);// 原图片宽度
66+
int srcHeight = srcImage.getHeight(null);// 原图片高度
67+
int dstMaxSize = 90;// 目标缩略图的最大宽度/高度,宽度与高度将按比例缩写
68+
int dstWidth = srcWidth;// 缩略图宽度
69+
int dstHeight = srcHeight;// 缩略图高度
70+
float scale = 0;
71+
// 计算缩略图的宽和高
72+
if (srcWidth > dstMaxSize) {
73+
dstWidth = dstMaxSize;
74+
scale = (float)srcWidth / (float)dstMaxSize;
75+
dstHeight = Math.round((float)srcHeight / scale);
76+
}
77+
srcHeight = dstHeight;
78+
if (srcHeight > dstMaxSize) {
79+
dstHeight = dstMaxSize;
80+
scale = (float)srcHeight / (float)dstMaxSize;
81+
dstWidth = Math.round((float)dstWidth / scale);
82+
}
83+
// 生成缩略图
84+
tagImage = new BufferedImage(dstWidth, dstHeight, BufferedImage.TYPE_INT_RGB);
85+
tagImage.getGraphics().drawImage(srcImage, 0, 0, dstWidth, dstHeight, null);
86+
return tagImage;
87+
} finally {
88+
if (fileOutputStream != null) {
89+
try {
90+
fileOutputStream.close();
91+
} catch (Exception e) {
92+
}
93+
fileOutputStream = null;
94+
}
95+
tagImage = null;
96+
srcImage = null;
97+
System.gc();
98+
}
99+
}
100+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
(_)
2+
_ __ _ _ _ _ __ ___ ___ ___
3+
| '_ \| | | | | '_ ` _ \ / _ \ / _ \
4+
| | | | | |_| | | | | | | (_) | (_) |
5+
|_| |_|_|\__,_|_| |_| |_|\___/ \___/ 版本:${spring-boot.formatted-version}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package net.codingme.banner;
2+
3+
import org.junit.Test;
4+
import org.junit.runner.RunWith;
5+
import org.springframework.boot.test.context.SpringBootTest;
6+
import org.springframework.test.context.junit4.SpringRunner;
7+
8+
@RunWith(SpringRunner.class)
9+
@SpringBootTest
10+
public class BannerApplicationTests {
11+
12+
@Test
13+
public void contextLoads() {}
14+
15+
}

0 commit comments

Comments
 (0)