Java 根据用户名生成字体头像,仿钉钉、通讯录样式

avatar 2018年02月08日20:23:59 6 6725 views
博主分享免费Java教学视频,B站账号:Java刘哥
在注册登录的时候,一般要给新用户一个头像,想过用那种默认的头像,觉得很丑。 突然想到钉钉的用户默认头像 当然,这种样式在华为手机通讯录里也见过(有些主题有)。 于是,也准备模仿一下。  

先上最终的效果图

   

代码如下

  1. package com.liuyanzhao.chuyun.util;
  2. import java.awt.*;
  3. import java.awt.geom.RoundRectangle2D;
  4. import java.awt.image.BufferedImage;
  5. import java.io.File;
  6. import java.io.IOException;
  7. import java.util.Random;
  8. import java.util.regex.Matcher;
  9. import java.util.regex.Pattern;
  10. import javax.imageio.ImageIO;
  11. /**
  12.  * 根据姓名创建图片
  13.  */
  14. public class CreateNamePicture {
  15.     /**
  16.      * @throws IOException
  17.      * @throws
  18.      **/
  19.     public static void main(String[] args) throws IOException {
  20.         String name = "刘言曌爱罗琪";
  21.         generateImg(name, "/Users/liuyanzhao/Desktop/temp", name);
  22.     }
  23.     /**
  24.      * 绘制字体头像
  25.      * 如果是英文名,只显示首字母大写
  26.      * 如果是中文名,只显示最后两个字
  27.      * @param name
  28.      * @param outputPath
  29.      * @param outputName
  30.      * @throws IOException
  31.      */
  32.     public static void generateImg(String name, String outputPath, String outputName)
  33.             throws IOException {
  34.         int width = 100;
  35.         int height = 100;
  36.         int nameLen = name.length();
  37.         String nameWritten;
  38.         //如果用户输入的姓名少于等于2个字符,不用截取
  39.         if (nameLen <= 2) {
  40.             nameWritten = name;
  41.         } else {
  42.             //如果用户输入的姓名大于等于3个字符,截取后面两位
  43.             String first = name.substring(01);
  44.             if (isChinese(first)) {
  45.                 //截取倒数两位汉字
  46.                 nameWritten = name.substring(nameLen - 2);
  47.             } else {
  48.                 //截取前面的两个英文字母
  49.                 nameWritten = name.substring(02).toUpperCase();
  50.             }
  51.         }
  52.         String filename = outputPath + File.separator + outputName + ".jpg";
  53.         File file = new File(filename);
  54.         //Font font = new Font("微软雅黑", Font.PLAIN, 30);
  55.         BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
  56.         Graphics2D g2 = (Graphics2D) bi.getGraphics();
  57.         g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
  58.                 RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
  59.         g2.setBackground(getRandomColor());
  60.         g2.clearRect(00, width, height);
  61.         g2.setPaint(Color.WHITE);
  62.         Font font = null;
  63.         //两个字及以上
  64.         if(nameWritten.length() >= 2) {
  65.             font = new Font("微软雅黑", Font.PLAIN, 30);
  66.             g2.setFont(font);
  67.             String firstWritten = nameWritten.substring(01);
  68.             String secondWritten = nameWritten.substring(12);
  69.             //两个中文 如 言曌
  70.             if (isChinese(firstWritten) && isChinese(secondWritten)) {
  71.                 g2.drawString(nameWritten, 2060);
  72.             }
  73.             //首中次英 如 罗Q
  74.             else if (isChinese(firstWritten) && !isChinese(secondWritten)) {
  75.                 g2.drawString(nameWritten, 2760);
  76.                 //首英,如 AB
  77.             } else {
  78.                 nameWritten = nameWritten.substring(0,1);
  79.             }
  80.         }
  81.         //一个字
  82.         if(nameWritten.length() ==1) {
  83.             //中文
  84.             if(isChinese(nameWritten)) {
  85.                 font = new Font("微软雅黑", Font.PLAIN, 50);
  86.                 g2.setFont(font);
  87.                 g2.drawString(nameWritten, 2570);
  88.             }
  89.             //英文
  90.             else {
  91.                 font = new Font("微软雅黑", Font.PLAIN, 55);
  92.                 g2.setFont(font);
  93.                 g2.drawString(nameWritten.toUpperCase(), 3367);
  94.             }
  95.         }
  96.         BufferedImage rounded = makeRoundedCorner(bi, 99);
  97.         ImageIO.write(rounded, "png", file);
  98.     }
  99.     /**
  100.      * 判断字符串是否为中文
  101.      * @param str
  102.      * @return
  103.      */
  104.     public static boolean isChinese(String str) {
  105.         String regEx = "[\\u4e00-\\u9fa5]+";
  106.         Pattern p = Pattern.compile(regEx);
  107.         Matcher m = p.matcher(str);
  108.         if (m.find())
  109.             return true;
  110.         else
  111.             return false;
  112.     }
  113.     /**
  114.      * 获得随机颜色
  115.      * @return
  116.      */
  117.     private static Color getRandomColor() {
  118.         String[] beautifulColors =
  119.                 new String[]{"232,221,203""205,179,128""3,101,100""3,54,73""3,22,52",
  120.                         "237,222,139""251,178,23""96,143,159""1,77,103""254,67,101""252,157,154",
  121.                         "249,205,173""200,200,169""131,175,155""229,187,129""161,23,21""34,8,7",
  122.                         "118,77,57""17,63,61""60,79,57""95,92,51""179,214,110""248,147,29",
  123.                         "227,160,93""178,190,126""114,111,238""56,13,49""89,61,67""250,218,141",
  124.                         "3,38,58""179,168,150""222,125,44""20,68,106""130,57,53""137,190,178",
  125.                         "201,186,131""222,211,140""222,156,83""23,44,60""39,72,98""153,80,84",
  126.                         "217,104,49""230,179,61""174,221,129""107,194,53""6,128,67""38,157,128",
  127.                         "178,200,187""69,137,148""117,121,71""114,83,52""87,105,60""82,75,46",
  128.                         "171,92,37""100,107,48""98,65,24""54,37,17""137,157,192""250,227,113",
  129.                         "29,131,8""220,87,18""29,191,151""35,235,185""213,26,33""160,191,124",
  130.                         "101,147,74""64,116,52""255,150,128""255,94,72""38,188,213""167,220,224",
  131.                         "1,165,175""179,214,110""248,147,29""230,155,3""209,73,78""62,188,202",
  132.                         "224,160,158""161,47,47""0,90,171""107,194,53""174,221,129""6,128,67",
  133.                         "38,157,128""201,138,131""220,162,151""137,157,192""175,215,237""92,167,186",
  134.                         "255,66,93""147,224,255""247,68,97""185,227,217"};
  135.         int len = beautifulColors.length;
  136.         Random random = new Random();
  137.         String[] color = beautifulColors[random.nextInt(len)].split(",");
  138.         return new Color(Integer.parseInt(color[0]), Integer.parseInt(color[1]),
  139.                 Integer.parseInt(color[2]));
  140.     }
  141.     /**
  142.      * 图片做圆角处理
  143.      * @param image
  144.      * @param cornerRadius
  145.      * @return
  146.      */
  147.     public static BufferedImage makeRoundedCorner(BufferedImage image, int cornerRadius) {
  148.         int w = image.getWidth();
  149.         int h = image.getHeight();
  150.         BufferedImage output = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
  151.         Graphics2D g2 = output.createGraphics();
  152.         g2.setComposite(AlphaComposite.Src);
  153.         g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  154.         g2.setColor(Color.WHITE);
  155.         g2.fill(new RoundRectangle2D.Float(00, w, h, cornerRadius, cornerRadius));
  156.         g2.setComposite(AlphaComposite.SrcAtop);
  157.         g2.drawImage(image, 00null);
  158.         g2.dispose();
  159.         return output;
  160.     }
  161. }
  代码下载地址:https://github.com/saysky/CreateNamePicture 本文地址:https://liuyanzhao.com/7498.html
  • 微信
  • 交流学习,有偿服务
  • weinxin
  • 博客/Java交流群
  • 资源分享,问题解决,技术交流。群号:590480292
  • weinxin
avatar

发表评论

avatar 登录者:匿名
匿名评论,评论回复后会有邮件通知

  

已通过评论:0   待审核评论数:0