当前位置: 首页 > news >正文

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;}}

http://www.lryc.cn/news/123253.html

相关文章:

  • 微信小程序备案流程
  • JavaScript版本ES5/ES6及后续版本
  • 解决Idea 多模块,maven项目是多层级文件夹的子项时无法加入git管理的问题
  • yolo源码注释4——yolo-py
  • 计算机网络中速率和带宽的区别
  • MySQL数据库练习
  • Redis BitMap/HyperLogLog/GEO/布隆过滤器案例
  • POI处理excel,根据XLOOKUP发现部分公式格式不支持问题
  • 第一次PR经历
  • 背上小书包准备面试之TypeScript篇
  • 【Spring】浅谈spring为什么推荐使用构造器注入
  • 在阿里云Linux服务器上部署MySQL数据库流程
  • 实战——OPenPose讲解及代码实现
  • 专注于创意设计,为您的小程序和网站建设带来更多的可能性
  • ATF(TF-A)安全通告 TFV-6 (CVE-2017-5753, CVE-2017-5715, CVE-2017-5754)
  • vue3 基础语法 02
  • 版本控制工具——git
  • 超详细,自动化测试实战-获取配置文件信息(实例源码)
  • spring 2.7.14 cors 设置 allowedOrigins(“*“)通配符 失效怎么解决
  • 一、Go的前景与优势、基础语法
  • shell脚本循环语句
  • 二叉树题目:二叉树的直径
  • 嵌入式:C高级 Day4
  • cmake常用命令(1)——函数相关
  • 阿里三年功能测试的一些感悟
  • React源码解析18(4)------ completeWork的工作流程【mount】
  • Kafka: 详解、使用教程和示例
  • 【LeetCode周赛】LeetCode第358场周赛
  • Node.js学习笔记-04
  • 基于dbn+svr的交通流量预测,dbn详细原理