Java实现根据姓名生成头像(钉钉样式)

头像生成器代码如下:

java 复制代码
package com.hua.util;

import org.apache.commons.lang3.StringUtils;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Random;
import java.util.UUID;

/**
 * @author: zhengzhonghua
 * @date: 2023/3/16 13:23
 * @description: 头像生成器
 */
public class HeadGeneration {
    /**
     * @author: zhengzhonghua
     * @date: 2023/3/16 13:24
     * @description: 头像生成
     * @param name 用户名称
     * @return 访问图片的路径 localhost:8082/img/
     */
    public static String generateImg(String name) throws IOException {
        int nameLen = name.length();
        //定义最后在图片上显示的姓名
        String nameWritten;
        if (nameLen <= 2) {
            nameWritten = name;
        } else {
            //如果用户姓名大于三位,截取后两位
            nameWritten = StringUtils.right(name, 2);
        }
        String uuid = UUID.randomUUID().toString().replace("-","");
        //文件名(路径+uuid+.jpg)
        String fileName = Const.HEAD_IMAGE_URL + uuid+".jpg";
        File file = new File(fileName);
        //生成图片
        BufferedImage bi = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2 = (Graphics2D) bi.getGraphics();
        g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        g2.setBackground(getRandomColor());
        g2.clearRect(0, 0, 100, 100);
        g2.setPaint(Color.WHITE);
        Font font = null;
        // 两个字及以上
        if (nameWritten.length() >= 2) {
            font = new Font("微软雅黑", Font.PLAIN, 30);
            g2.setFont(font);
            g2.drawString(nameWritten, 20, 60);
        }
        // 一个字
        if (nameWritten.length() == 1) {
            // 中文
            font = new Font("微软雅黑", Font.PLAIN, 50);
            g2.setFont(font);
            g2.drawString(nameWritten, 25, 70);

        }
        //图片做圆角处理
        BufferedImage rounded = makeRoundedCorner(bi, Const.HEAD_IMAGE_Radius);
        ImageIO.write(rounded, "png", file);
        return Const.serverUrl+uuid+".jpg";
    }

    /**
     * @author: zhengzhonghua
     * @date: 2023/3/16 13:32
     * @description: 生成随机颜色
     */
    private static Color getRandomColor() {
        String[] beautifulColors = new String[]{"2,168,250"};
        int len = beautifulColors.length;
        Random random = new Random();
        String[] color = beautifulColors[random.nextInt(len)].split(",");
        return new Color(Integer.parseInt(color[0]), Integer.parseInt(color[1]), Integer.parseInt(color[2]));
    }

    /**
     * @author: zhengzhonghua
     * @date: 2023/3/16 13:39
     * @description: 图片做圆角处理
     */
    public static BufferedImage makeRoundedCorner(BufferedImage image, int cornerRadius) {
        int w = image.getWidth();
        int h = image.getHeight();
        BufferedImage output = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2 = output.createGraphics();
        g2.setComposite(AlphaComposite.Src);
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2.setColor(Color.WHITE);
        g2.fill(new RoundRectangle2D.Float(0, 0, w, h, cornerRadius, cornerRadius));
        g2.setComposite(AlphaComposite.SrcAtop);
        g2.drawImage(image, 0, 0, null);
        g2.dispose();
        return output;
    }

}
相关推荐
掐指一算乀缺钱16 分钟前
SpringBoot 数据库表结构文档生成
java·数据库·spring boot·后端·spring
晚睡早起₍˄·͈༝·͈˄*₎◞ ̑̑21 分钟前
苍穹外卖学习笔记(七)
java·windows·笔记·学习·mybatis
就这个java爽!27 分钟前
JAVA网络编程【基于TCP和UDP协议】超详细!!!
java·开发语言·网络·tcp/ip·udp·eclipse·idea
一叶飘零_sweeeet31 分钟前
为什么 Feign 要用 HTTP 而不是 RPC?
java·网络协议·http·spring cloud·rpc·feign
懒洋洋大魔王1 小时前
7.Java高级编程 多线程
java·开发语言·jvm
茶馆大橘1 小时前
【黑马点评】已解决java.lang.NullPointerException异常
java·开发语言
星辰@Sea1 小时前
服务注册中心对比及使用场景分析
java·云原生
马剑威(威哥爱编程)1 小时前
除了递归算法,要如何优化实现文件搜索功能
java·开发语言·算法·递归算法·威哥爱编程·memoization
bug菌¹1 小时前
滚雪球学SpringCloud[4.1讲]: Spring Cloud Gateway详解
java·spring cloud·微服务
MuseLss1 小时前
HashMap高频面试知识点
java·开发语言·哈希算法