瀏覽代碼

更新头像背景等

imndx 2 年之前
父節點
當前提交
7aee50322c

+ 1 - 1
config/application.properties

@@ -108,4 +108,4 @@ spring.mail.properties.mail.imap.ssl.socketFactory.fallback=false
 spring.mail.to_lists=admin1@wildfirechat.cn,admin2@wildfirechat.cn,admin3@wildfirechat.cn
 
 # 头像背景颜色可选列表,逗号分隔,中间不能有空格
-avatar.bg.corlors=#D1C4E9,#B39DDB,#9575CD,#7E57C2,#673AB7,#5E35B1,#512DA8,#4527A0,#311B92,#B388FF,#7C4DFF,#651FFF,#6200EA,#F3E5F5,#E1BEE7
+avatar.bg.corlors=#D32F2F,#D81B60,#880E4F,#9C27B0,#6A1B9A,#4A148C,#AA00FF,#C51162,#673AB7,#311B92,#651FFF,#5C6BC0,#283593,#1A237E,#304FFE,#1976D2,#0D47A1,#2962FF,#0D47A1,#0277BD,#01579B

+ 2 - 1
src/main/java/cn/wildfirechat/app/avatar/AvatarServiceImpl.java

@@ -75,7 +75,8 @@ public class AvatarServiceImpl implements AvatarService {
         if (!file.exists()) {
             String color = colors[Math.abs(name.hashCode() % len)];
             // 最后一个字符
-            file = new NameAvatarBuilder(color).name(name.substring(name.length() - 1), name).build();
+            String lastChar = name.substring(name.length() - 1).toUpperCase();
+            file = new NameAvatarBuilder(color).name(lastChar, name).build();
         }
         return file;
     }

+ 11 - 12
src/main/java/cn/wildfirechat/app/avatar/NameAvatarBuilder.java

@@ -1,8 +1,6 @@
 package cn.wildfirechat.app.avatar;
 
-import java.awt.Color;
-import java.awt.Graphics2D;
-import java.awt.RenderingHints;
+import java.awt.*;
 import java.awt.image.BufferedImage;
 import java.io.File;
 import java.io.IOException;
@@ -30,15 +28,16 @@ public class NameAvatarBuilder {
 
     public NameAvatarBuilder name(String drawName, String fullName) {
         this.fullName = fullName;
-        int x = templateWidth * 3 / 10;
-        int y = templateHeight * 2 / 3;
-        // templateG2D.translate(0, 100);
-        // g2.rotate(Math.PI / 3);
-        templateG2D.setFont(templateG2D.getFont().deriveFont(40f));
-        templateG2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
-            RenderingHints.VALUE_ANTIALIAS_ON);
-        // templateG2D.setColor(Color.WHITE);
-        templateG2D.setColor(Color.decode("#FFFFFF"));
+        // Get the FontMetrics
+        Font font = templateG2D.getFont().deriveFont(40f);
+        FontMetrics metrics = templateG2D.getFontMetrics(font);
+        // Determine the X coordinate for the text
+        int x = (templateWidth - metrics.stringWidth(drawName)) / 2;
+        // Determine the Y coordinate for the text (note we add the ascent, as in java 2d 0 is top of the screen)
+        int y = ((templateHeight - metrics.getHeight()) / 2) + metrics.getAscent();
+        // Set the font
+        templateG2D.setFont(font);
+        // Draw the String
         templateG2D.drawString(drawName, x, y);
         return this;
     }