当前位置:网站首页>根据昵称首字母生成头像
根据昵称首字母生成头像
2022-07-29 19:58:00 【starriesWEB】
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/** * 根据姓名创建图片 */
public class AvatarUtils {
/** * @throws IOException * @throws **/
public static void main(String[] args) throws IOException {
String s = "体";
long l1 = System.currentTimeMillis();
generateImg(s, "C:\\Users\\starry\\Desktop", s);
long l2 = System.currentTimeMillis();
System.out.println(l2 - l1);
}
/** * 绘制字体头像 * * @param name 生成头像的用户名 * @param outputPath 图片保存的位置 * @param outputName 保存图片的文件名 * @throws IOException */
public static void generateImg(String name, String outputPath, String outputName) throws IOException {
int width = 168;
int height = 168;
int nameLen = name.length();
if (nameLen > 1) {
name = name.substring(0, 1);
}
BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = (Graphics2D) bi.getGraphics();
g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
g2.setBackground(new Color(76, 148, 232));
g2.clearRect(0, 0, width, height);
g2.setPaint(Color.WHITE);
Font font;
//中文
if (isChinese(name)) {
font = new Font("微软雅黑", Font.PLAIN, (int) (height * 0.5F));
g2.setFont(font);
g2.drawString(name, width * 0.25F, height * 0.7F);
}
//英文
else {
font = new Font("微软雅黑", Font.PLAIN, (int) (height * 0.55F));
g2.setFont(font);
g2.drawString(name.toUpperCase(), width * 0.3F, height * 0.67F);
}
String filename = outputPath + File.separator + outputName + ".png";
File file = new File(filename);
ImageIO.write(bi, "png", file);
}
/** * 判断字符串是否为中文 * * @param str * @return */
public static boolean isChinese(String str) {
String regEx = "[\\u4e00-\\u9fa5]+";
Pattern p = Pattern.compile(regEx);
Matcher m = p.matcher(str);
if (m.find()) {
return true;
} else {
return false;
}
}
}
转 https://blog.csdn.net/qq_18620233/article/details/124796742
边栏推荐
- 藻酸盐/PEI/DNA复合载体|脂质-鱼精蛋白-DNA复合物|合成方法
- [数学基础]线性代数相关概念学习
- :class数组写法
- ESP8266-Arduino编程实例-LittleFS及数据上传
- es6语法使用默认参数和解构
- 从专业角度分析国内创客教育发展
- 安全浏览器将拥有这些隐藏功能,让你玩转浏览器
- :style中颜色使用函数动态获取赋值
- 磁性层状双金属氢氧化物和酶-DNA复合物|聚乙烯亚胺-DNA复合物(PEI/DNA)|作用机理
- The second growth curve | The driving guide for corporate innovation to break through the stagnation dilemma
猜你喜欢
随机推荐
关于论青少年尽早学少儿编程之说
正则表达式
安全浏览器将拥有这些隐藏功能,让你玩转浏览器
Omni-channel e-commerce | How can well-known domestic cosmeceuticals seize the opportunity to achieve rapid growth?
What are the software development modes (software engineering development mode)
ds1302——斌哥51
单壁碳纳米管-DNA复合物(SWCNT-DNA)|作用机理
[Mathematical Foundation] Learning about concepts related to linear algebra
ESP8266-Arduino编程实例-LittleFS及数据上传
ESP8266-Arduino编程实例-EEPROM读写
断言+异常处理类,代码更简洁了
R language for airbnb data nlp text mining, geography, word cloud visualization, regression GAM model, cross-validation analysis
Detailed explanation of design ideas of webUI test framework
第二增长曲线 | 企业创新突破停滞困境的驱动指南
Chrome——插件推荐
2022中国物流产业大会暨企业家高峰论坛在杭州举办!
uri与url的区别简单理解(uri和url有什么区别)
Mass data query scheme mysql_Mysql massive data storage and solution 2 - Mysql sub-table query massive data... [easy to understand]
Kotlin - Coroutine Scope CoroutineScope, Coroutine Builder CoroutineBuilder, Coroutine Scope Function CoroutineScope Functiom
Internship: use easypoi to import and export excel table data








