17
17
import org .slf4j .Logger ;
18
18
import org .slf4j .LoggerFactory ;
19
19
20
- import javax .imageio .ImageIO ;
21
- import java .awt .*;
22
- import java .awt .image .BufferedImage ;
23
- import java .io .File ;
24
- import java .io .IOException ;
25
20
import java .time .LocalDate ;
26
- import java .util .List ;
27
21
import java .util .*;
28
- import java .util .concurrent .ConcurrentHashMap ;
29
22
30
23
/**
31
24
* 个人信息数据源
35
28
public class PersonInfoSource {
36
29
private static final Logger logger = LoggerFactory .getLogger (PersonInfoSource .class );
37
30
38
- /**
39
- * 默认字体
40
- */
41
- private static final Font defaultFont = new Font ("Dialog" , Font .PLAIN , 78 );
42
-
43
31
/**
44
32
* 姓名图片可选颜色
45
33
*/
@@ -86,11 +74,6 @@ public class PersonInfoSource {
86
74
checkNumMap .put (10 , "2" );
87
75
}
88
76
89
- /**
90
- * 字体对象缓存
91
- */
92
- private static Map <String , Font > fontMap = new ConcurrentHashMap <>();
93
-
94
77
/**
95
78
* 常见中文姓氏
96
79
*/
@@ -479,52 +462,6 @@ public String randomCreditCardNo(CreditCardType type) {
479
462
return FinancialSource .getInstance ().randomCreditCardNo (type );
480
463
}
481
464
482
- /**
483
- * 生成姓名图片文件
484
- *
485
- * @param name 姓名
486
- * @param savePath 图片文件的保存路径
487
- * @param fontPath 第三方字体的路径
488
- * @throws IOException IO异常
489
- */
490
- public void generateNamePicture (String name , String savePath , String fontPath ) throws IOException {
491
- Preconditions .checkArgument (StringUtils .isNotEmpty (name ), "姓名不能为空" );
492
- Preconditions .checkArgument (StringUtils .isNotEmpty (savePath ), "图片保存路径不能为空" );
493
- String text = name ;
494
- if (name .length () > 1 ) {
495
- //长度超过1时,随机取一个字
496
- int i = RandomUtils .nextInt (0 , name .length ());
497
- text = name .substring (i , i + 1 );
498
- }
499
- File file = new File (savePath );
500
- if (file .exists ()) {
501
- throw new IllegalArgumentException ("文件 " + savePath + " 已存在" );
502
- }
503
- int width = 200 , height = 200 ;
504
- BufferedImage bi = new BufferedImage (width , height , BufferedImage .TYPE_INT_RGB );
505
- Graphics2D g2 = (Graphics2D ) bi .getGraphics ();
506
- g2 .setRenderingHint (RenderingHints .KEY_TEXT_ANTIALIASING , RenderingHints .VALUE_TEXT_ANTIALIAS_ON );
507
- g2 .setBackground (getRandomColor ());
508
- g2 .clearRect (0 , 0 , width , height );
509
- //文字为白色
510
- g2 .setPaint (Color .WHITE );
511
- g2 .setFont (getFont (fontPath ));
512
- g2 .drawString (text , 62 , 126 );
513
- ImageIO .write (bi , "png" , file );
514
- logger .info ("{} 的姓名图片已生成到 {}" , name , savePath );
515
- }
516
-
517
- /**
518
- * 生成姓名图片文件(使用默认字体)
519
- *
520
- * @param name 姓名
521
- * @param savePath 图片文件的保存路径
522
- * @throws IOException IO异常
523
- */
524
- public void generateNamePicture (String name , String savePath ) throws IOException {
525
- generateNamePicture (name , savePath , null );
526
- }
527
-
528
465
/**
529
466
* 随机性别
530
467
*
@@ -534,45 +471,6 @@ public int randomGender() {
534
471
return RandomUtils .nextInt (0 , 2 );
535
472
}
536
473
537
- /**
538
- * 获取字体
539
- *
540
- * @param fontPath 字体文件路径
541
- * @return 字体
542
- */
543
- private Font getFont (String fontPath ) {
544
- if (StringUtils .isEmpty (fontPath )) {
545
- logger .warn ("字体文件路径为空! 将使用默认字体 {}" , defaultFont .getName ());
546
- return defaultFont ;
547
- }
548
- File fontFile = new File (fontPath );
549
- if (!fontFile .exists ()) {
550
- logger .warn ("字体文件 {} 不存在! 将使用默认字体 {}" , fontPath , defaultFont .getName ());
551
- return defaultFont ;
552
- }
553
- return fontMap .computeIfAbsent (fontPath , k -> {
554
- try {
555
- Font font = Font .createFont (Font .TRUETYPE_FONT , fontFile ).deriveFont (Float .parseFloat ("78" ));
556
- GraphicsEnvironment ge = GraphicsEnvironment .getLocalGraphicsEnvironment ();
557
- ge .registerFont (font );
558
- return font ;
559
- } catch (FontFormatException | IOException e ) {
560
- logger .error ("初始化字体异常" , e );
561
- return null ;
562
- }
563
- });
564
- }
565
-
566
- /**
567
- * 获取随机颜色
568
- *
569
- * @return 随机颜色
570
- */
571
- private Color getRandomColor () {
572
- String [] color = ResourceUtils .getRandomElement (namePictureColorsList ).split ("," );
573
- return new Color (Integer .parseInt (color [0 ]), Integer .parseInt (color [1 ]), Integer .parseInt (color [2 ]));
574
- }
575
-
576
474
/**
577
475
* 生成随机身份证号码
578
476
*
0 commit comments