Kaynağa Gözat

头像生成器

linxk 2 hafta önce
ebeveyn
işleme
df09537004

+ 199 - 0
cif-service/src/main/java/com/txz/cif/util/GradientMacaronAvatarGenerator.java

@@ -0,0 +1,199 @@
+package com.txz.cif.util;
+
+import javax.imageio.ImageIO;
+import java.awt.*;
+import java.awt.image.BufferedImage;
+import java.io.File;
+import java.io.IOException;
+import java.util.Random;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Collections;
+
+public class GradientMacaronAvatarGenerator {
+    
+    // 输出目录
+    private static final String OUTPUT_DIR = "gradient_macaron_avatars";
+    // 头像尺寸
+    private static final int AVATAR_SIZE = 200;
+    // 每个字母生成的头像数量
+    private static final int AVATARS_PER_LETTER = 10;
+    // 每次运行使用的基础颜色数量
+    private static final int BASE_COLORS_TO_USE = 15;
+    
+    // 马卡龙色系基础色(用于生成渐变)
+    private static final Color[] MACARON_BASE_COLORS = {
+        new Color(255, 220, 230), new Color(230, 210, 250), new Color(200, 220, 250),
+        new Color(200, 240, 220), new Color(250, 240, 200), new Color(250, 220, 180),
+        new Color(250, 200, 200), new Color(200, 240, 240), new Color(240, 210, 180),
+        new Color(200, 250, 230), new Color(250, 210, 190), new Color(220, 200, 240),
+        new Color(220, 230, 200), new Color(240, 190, 220), new Color(190, 220, 240),
+        new Color(230, 230, 235), new Color(250, 240, 225), new Color(250, 225, 200),
+        new Color(245, 190, 210), new Color(210, 180, 240), new Color(170, 200, 240),
+        new Color(160, 230, 190), new Color(240, 220, 150), new Color(245, 200, 140),
+        new Color(240, 170, 170), new Color(160, 230, 230), new Color(230, 190, 150),
+        new Color(160, 240, 210), new Color(245, 190, 160), new Color(200, 170, 230),
+        new Color(200, 220, 170), new Color(230, 160, 200), new Color(150, 200, 230),
+        new Color(210, 210, 220), new Color(240, 225, 200), new Color(245, 210, 170),
+        new Color(245, 170, 195), new Color(190, 150, 230), new Color(140, 180, 230),
+        new Color(120, 210, 160), new Color(230, 200, 100), new Color(240, 180, 100),
+        new Color(230, 140, 140), new Color(120, 210, 210), new Color(220, 250, 230),
+        new Color(240, 230, 240), new Color(235, 225, 240), new Color(230, 240, 225),
+        new Color(245, 230, 215), new Color(220, 240, 235)
+    };
+    
+    // 本次运行选中的基础色
+    private static Color[] selectedBaseColors;
+    
+    public static void main(String[] args) {
+        // 选择基础色
+        selectBaseColors();
+        
+        // 创建输出目录
+        createOutputDirectory();
+        
+        // 为每个字母生成头像
+        for (char c = 'A'; c <= 'Z'; c++) {
+            generateAvatarsForLetter(c);
+        }
+        
+        System.out.println("渐变头像生成完成!保存路径:" + new File(OUTPUT_DIR).getAbsolutePath());
+    }
+    
+    // 选择基础色
+    private static void selectBaseColors() {
+        List<Color> colorList = new ArrayList<>();
+        Collections.addAll(colorList, MACARON_BASE_COLORS);
+        Collections.shuffle(colorList);
+        selectedBaseColors = new Color[BASE_COLORS_TO_USE];
+        for (int i = 0; i < BASE_COLORS_TO_USE; i++) {
+            selectedBaseColors[i] = colorList.get(i);
+        }
+    }
+    
+    // 为单个字母生成头像
+    private static void generateAvatarsForLetter(char letter) {
+        String letterDir = OUTPUT_DIR + File.separator + letter;
+        new File(letterDir).mkdirs();
+        
+        for (int i = 1; i <= AVATARS_PER_LETTER; i++) {
+            try {
+                BufferedImage avatar = generateGradientAvatar(letter, AVATAR_SIZE);
+                String filePath = letterDir + File.separator + letter + "_gradient_" + i + ".png";
+                ImageIO.write(avatar, "png", new File(filePath));
+                System.out.printf("生成头像: %s%n", filePath);
+            } catch (IOException e) {
+                System.err.printf("生成字母 %c 的第 %d 个头像时出错: %s%n", letter, i, e.getMessage());
+            }
+        }
+    }
+    
+    // 生成渐变背景头像
+    private static BufferedImage generateGradientAvatar(char letter, int size) {
+        BufferedImage image = new BufferedImage(size, size, BufferedImage.TYPE_INT_RGB);
+        Graphics2D g = image.createGraphics();
+        
+        try {
+            // 抗锯齿设置
+            g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
+            g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
+            
+            // 随机选择两种相近色生成渐变
+            Color color1 = getRandomBaseColor();
+            Color color2 = getSimilarColor(color1);
+            
+            // 随机渐变方向(修复了此处的错误)
+            Random random = new Random();
+            int x1 = 0, y1 = 0;
+            int x2, y2;
+            
+            // 随机选择渐变方向
+            int direction = random.nextInt(4);
+            switch (direction) {
+                case 0: // 左上到右下
+                    x2 = size;
+                    y2 = size;
+                    break;
+                case 1: // 右上到左下
+                    x2 = size;
+                    y2 = 0;
+                    break;
+                case 2: // 上到下
+                    x2 = 0;
+                    y2 = size;
+                    break;
+                default: // 左到右
+                    x2 = size;
+                    y2 = 0;
+            }
+            
+            // 创建渐变画笔(修复了常量错误)
+            GradientPaint gradient = new GradientPaint(x1, y1, color1, x2, y2, color2);
+            g.setPaint(gradient);
+            
+            // 绘制带圆角的渐变背景
+            int radius = size / 7;
+            g.fillRoundRect(0, 0, size, size, radius, radius);
+            
+            // 计算文字颜色(确保对比度)
+            Color textColor = getContrastColor(color1, color2);
+            g.setColor(textColor);
+            
+            // 随机字体
+            String[] fonts = {"Arial", "Helvetica", "Verdana", "Georgia"};
+            Font font = new Font(fonts[random.nextInt(fonts.length)], Font.BOLD, size / 2);
+            g.setFont(font);
+            
+            // 文字居中
+            FontMetrics metrics = g.getFontMetrics(font);
+            int textWidth = metrics.charWidth(letter);
+            int textAscent = metrics.getAscent();
+            int x = (size - textWidth) / 2;
+            int y = (size - metrics.getHeight()) / 2 + textAscent;
+            
+            // 绘制字母
+            g.drawString(String.valueOf(letter), x, y);
+            
+            return image;
+        } finally {
+            g.dispose();
+        }
+    }
+    
+    // 获取随机基础色
+    private static Color getRandomBaseColor() {
+        return selectedBaseColors[new Random().nextInt(selectedBaseColors.length)];
+    }
+    
+    // 生成相近色(用于渐变过渡)
+    private static Color getSimilarColor(Color base) {
+        Random random = new Random();
+        int r = clamp(base.getRed() + (random.nextInt(41) - 20));
+        int g = clamp(base.getGreen() + (random.nextInt(41) - 20));
+        int b = clamp(base.getBlue() + (random.nextInt(41) - 20));
+        return new Color(r, g, b);
+    }
+    
+    // 颜色值范围限制
+    private static int clamp(int value) {
+        return Math.min(255, Math.max(200, value)); // 保持马卡龙色的明亮感
+    }
+    
+    // 计算对比文字色
+    private static Color getContrastColor(Color c1, Color c2) {
+        // 取两种颜色的平均值计算亮度
+        int avgR = (c1.getRed() + c2.getRed()) / 2;
+        int avgG = (c1.getGreen() + c2.getGreen()) / 2;
+        int avgB = (c1.getBlue() + c2.getBlue()) / 2;
+        double luminance = (0.299 * avgR + 0.587 * avgG + 0.114 * avgB) / 255;
+        return luminance > 0.82 ? new Color(50, 50, 50) : Color.WHITE;
+    }
+    
+    // 创建输出目录
+    private static void createOutputDirectory() {
+        File dir = new File(OUTPUT_DIR);
+        if (!dir.exists() && !dir.mkdirs()) {
+            throw new RuntimeException("无法创建输出目录: " + dir.getAbsolutePath());
+        }
+    }
+}

+ 2 - 2
cif-service/src/main/java/com/txz/cif/web/bo/UserInfoBO.java

@@ -50,8 +50,8 @@ public class UserInfoBO {
     /**
      * 头像
      */
-    @ApiModelProperty(value="icon")
-    private String icon;
+    @ApiModelProperty(value="头像")
+    private String headPic;
 
     /**
      * 居住地址