Skip to content

Commit

Permalink
生成头像时,使用自定义字体,防止字体确实导致乱码
Browse files Browse the repository at this point in the history
  • Loading branch information
imndx committed Jul 26, 2023
1 parent c9e1548 commit 93ddd36
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 5 deletions.
22 changes: 18 additions & 4 deletions src/main/java/cn/wildfirechat/app/avatar/GroupAvatarUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
import java.io.DataInputStream;
import java.io.File;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;

Expand Down Expand Up @@ -147,12 +149,15 @@ public static void getCombinationOfHead(List<URL> paths, File targetFile)
* @param bb 比例不对时是否需要补白
*/
private static BufferedImage resize2(URL filePath, int height, int width,
boolean bb) throws URISyntaxException {
boolean bb) {
DataInputStream dis = null;
try {
double ratio = 0; // 缩放比例

DataInputStream dis = new DataInputStream(filePath.openStream());
URLConnection urlConnection = filePath.openConnection();
urlConnection.setConnectTimeout(5 * 1000);
urlConnection.setReadTimeout(5 * 1000);
dis = new DataInputStream(urlConnection.getInputStream());

double ratio = 0; // 缩放比例
//File f = new File(dis);
BufferedImage bi = ImageIO.read(dis);
Image itemp = bi.getScaledInstance(width, height,
Expand Down Expand Up @@ -191,6 +196,15 @@ private static BufferedImage resize2(URL filePath, int height, int width,
return (BufferedImage) itemp;
} catch (IOException e) {
e.printStackTrace();
} finally {
if (dis != null) {
try {
dis.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}

}
return null;
}
Expand Down
18 changes: 17 additions & 1 deletion src/main/java/cn/wildfirechat/app/avatar/NameAvatarBuilder.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package cn.wildfirechat.app.avatar;

import org.springframework.core.io.ClassPathResource;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;

import javax.imageio.ImageIO;

Expand All @@ -16,6 +19,8 @@ public class NameAvatarBuilder {

private String fullName;

private static volatile Font font;

public NameAvatarBuilder(String bgRGB) {
templateImage = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB);
templateG2D = templateImage.createGraphics();
Expand All @@ -28,7 +33,18 @@ public NameAvatarBuilder(String bgRGB) {
public NameAvatarBuilder name(String drawName, String fullName) {
this.fullName = fullName;
// Get the FontMetrics
Font font = templateG2D.getFont().deriveFont(40f);
// 加载自定义字体
if (font == null) {
try (InputStream inputStream = new ClassPathResource("fonts/simhei.ttf").getInputStream()) {
// 加载自定义字体
Font customFont = Font.createFont(Font.TRUETYPE_FONT, inputStream);
// 设置字体样式
font = customFont.deriveFont(Font.PLAIN, 40);
} catch (IOException | FontFormatException e) {
e.printStackTrace();
}
}

FontMetrics metrics = templateG2D.getFontMetrics(font);
// Determine the X coordinate for the text
int x = (templateWidth - metrics.stringWidth(drawName)) / 2;
Expand Down
Binary file added src/main/resources/fonts/simhei.ttf
Binary file not shown.

0 comments on commit 93ddd36

Please sign in to comment.