|
|
@@ -41,21 +41,23 @@ public class AvatarUtil {
|
|
|
int width = 100;
|
|
|
int height = 100;
|
|
|
int nameLen = name.length();
|
|
|
- String nameWritten;
|
|
|
- // 如果用户输入的姓名少于等于2个字符,不用截取
|
|
|
- if (nameLen <= 2) {
|
|
|
- nameWritten = name;
|
|
|
- } else {
|
|
|
- // 如果用户输入的姓名大于等于3个字符,截取后面两位
|
|
|
- String first = name.substring(0, 1);
|
|
|
- if (isChinese(first)) {
|
|
|
- // 截取倒数两位汉字
|
|
|
- nameWritten = name.substring(nameLen - 2);
|
|
|
- } else {
|
|
|
- // 截取前面的两个英文字母
|
|
|
- nameWritten = name.substring(0, 2).toUpperCase();
|
|
|
- }
|
|
|
- }
|
|
|
+
|
|
|
+ name=StrUtil.trim(name);
|
|
|
+ String nameWritten=StrUtil.sub(name,0,1);
|
|
|
+// // 如果用户输入的姓名少于等于2个字符,不用截取
|
|
|
+// if (nameLen == 1) {
|
|
|
+// nameWritten = name;
|
|
|
+// } else {
|
|
|
+// // 如果用户输入的姓名大于等于3个字符,截取后面两位
|
|
|
+// String first = name.substring(0, 1);
|
|
|
+// if (isChinese(first)) {
|
|
|
+// // 截取倒数两位汉字
|
|
|
+// nameWritten = name.substring(nameLen - 2);
|
|
|
+// } else {
|
|
|
+// // 截取前面的两个英文字母
|
|
|
+// nameWritten = name.substring(0, 2).toUpperCase();
|
|
|
+// }
|
|
|
+// }
|
|
|
BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
|
|
|
|
|
|
Graphics2D g2 = (Graphics2D) bi.getGraphics();
|
|
|
@@ -97,17 +99,16 @@ public class AvatarUtil {
|
|
|
if(nameWritten.length() ==1) {
|
|
|
// 中文
|
|
|
if(isChinese(nameWritten)) {
|
|
|
- font = new Font("微软雅黑", Font.PLAIN, 50);
|
|
|
+ font = new Font("华文中宋", Font.PLAIN, 50);
|
|
|
g2.setFont(font);
|
|
|
- g2.drawString(nameWritten, 25, 70);
|
|
|
+ g2.drawString(nameWritten, 25, 65);
|
|
|
}
|
|
|
// 英文
|
|
|
else {
|
|
|
- font = new Font("微软雅黑", Font.PLAIN, 55);
|
|
|
+ font = new Font("华文中宋", Font.PLAIN, 55);
|
|
|
g2.setFont(font);
|
|
|
g2.drawString(nameWritten.toUpperCase(), 33, 67);
|
|
|
}
|
|
|
-
|
|
|
}
|
|
|
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
|
|
BufferedImage rounded = makeRoundedCorner(bi, FILLET);
|
|
|
@@ -173,4 +174,8 @@ public class AvatarUtil {
|
|
|
return output;
|
|
|
}
|
|
|
|
|
|
+ public static void main(String[] args) {
|
|
|
+ System.out.println(generateAvatar("测试1"));
|
|
|
+ }
|
|
|
+
|
|
|
}
|