Skip to content

Commit ea62efe

Browse files
author
yindz
authored
Merge pull request #63 from yindz/dev
Dev
2 parents 29cf2be + 16c47a2 commit ea62efe

9 files changed

+75
-177
lines changed

pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>com.apifan.common</groupId>
88
<artifactId>common-random</artifactId>
9-
<version>1.0.16</version>
9+
<version>1.0.17</version>
1010
<packaging>jar</packaging>
1111
<name>common-random</name>
1212
<description>An easy-to-use random data generator.</description>

src/main/java/com/apifan/common/random/source/DateTimeSource.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ public Date randomDate(int year, int month, int dayOfMonth) {
288288
*/
289289
public LocalDateTime randomPastTime(int maxDays) {
290290
Preconditions.checkArgument(maxDays >= 1, "最大日期间隔必须大于0");
291-
return randomPastTime(LocalDateTime.now(), maxDays * 86400);
291+
return randomPastTime(LocalDateTime.now(), maxDays * 86400L);
292292
}
293293

294294
/**
@@ -334,7 +334,7 @@ public Date randomPastDate(LocalDateTime base, long maxSeconds) {
334334
*/
335335
public LocalDateTime randomFutureTime(int maxDays) {
336336
Preconditions.checkArgument(maxDays >= 1, "最大日期间隔必须大于0");
337-
return randomFutureTime(LocalDateTime.now(), maxDays * 86400);
337+
return randomFutureTime(LocalDateTime.now(), maxDays * 86400L);
338338
}
339339

340340
/**

src/main/java/com/apifan/common/random/source/PersonInfoSource.java

-102
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,8 @@
1717
import org.slf4j.Logger;
1818
import org.slf4j.LoggerFactory;
1919

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;
2520
import java.time.LocalDate;
26-
import java.util.List;
2721
import java.util.*;
28-
import java.util.concurrent.ConcurrentHashMap;
2922

3023
/**
3124
* 个人信息数据源
@@ -35,11 +28,6 @@
3528
public class PersonInfoSource {
3629
private static final Logger logger = LoggerFactory.getLogger(PersonInfoSource.class);
3730

38-
/**
39-
* 默认字体
40-
*/
41-
private static final Font defaultFont = new Font("Dialog", Font.PLAIN, 78);
42-
4331
/**
4432
* 姓名图片可选颜色
4533
*/
@@ -86,11 +74,6 @@ public class PersonInfoSource {
8674
checkNumMap.put(10, "2");
8775
}
8876

89-
/**
90-
* 字体对象缓存
91-
*/
92-
private static Map<String, Font> fontMap = new ConcurrentHashMap<>();
93-
9477
/**
9578
* 常见中文姓氏
9679
*/
@@ -479,52 +462,6 @@ public String randomCreditCardNo(CreditCardType type) {
479462
return FinancialSource.getInstance().randomCreditCardNo(type);
480463
}
481464

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-
528465
/**
529466
* 随机性别
530467
*
@@ -534,45 +471,6 @@ public int randomGender() {
534471
return RandomUtils.nextInt(0, 2);
535472
}
536473

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-
576474
/**
577475
* 生成随机身份证号码
578476
*

src/main/java/com/apifan/common/random/util/ResourceUtils.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ public static <T> List<T> getRandomElement(List<T> elementList, int number) {
123123
public static String getRandomString(List<String> elementList, int n) {
124124
List<String> randomElement = getRandomElement(elementList, n);
125125
StringBuilder sb = new StringBuilder();
126-
randomElement.stream().forEach(sb::append);
126+
randomElement.forEach(sb::append);
127127
return sb.toString();
128128
}
129129

+12-12
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
奥格斯堡
2-
勒沃库森
31
拜仁慕尼黑
4-
多特蒙德
5-
门兴格拉德巴赫
6-
法兰克福
7-
斯图加特
2+
门兴
3+
柏林联合
4+
美因茨
85
弗赖堡
9-
比勒费尔德
10-
柏林赫塔
116
霍芬海姆
12-
美因茨
7+
多特蒙德
138
科隆
14-
莱比锡红牛
9+
云达不莱梅
10+
奥格斯堡
11+
RB莱比锡
12+
斯图加特
13+
沙尔克04
1514
沃尔夫斯堡
16-
柏林联
17-
菲尔特
15+
法兰克福
16+
柏林赫塔
17+
勒沃库森
1818
波鸿
+15-15
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
1-
毕尔巴鄂竞技
2-
马德里竞技
1+
比利亚雷亚尔
2+
皇家马德里
3+
皇家贝蒂斯
34
奥萨苏纳
4-
马洛卡
5-
阿拉维斯
65
巴塞罗那
7-
赫塔菲
8-
格拉纳达
9-
莱万特
106
巴列卡诺
11-
维戈塞尔塔
12-
加的斯
13-
埃尔切
14-
皇家贝蒂斯
15-
皇家马德里
7+
毕尔巴鄂竞技
8+
赫罗纳
9+
马德里竞技
10+
瓦伦西亚
1611
皇家社会
17-
西班牙人
1812
塞维利亚
19-
瓦伦西亚
20-
比利亚雷亚尔
13+
阿尔梅里亚
14+
马洛卡
15+
西班牙人
16+
塞尔塔
17+
巴拉多利德
18+
埃尔切
19+
加的斯
20+
赫塔费
+15-15
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
1-
洛里昂
2-
昂热
3-
波尔多
4-
布雷斯特
5-
特鲁瓦
6-
梅斯
7-
里尔
8-
里昂
1+
巴黎圣日耳曼
2+
朗斯
93
马赛
4+
里昂
5+
克莱蒙
6+
图卢兹
7+
布雷斯特
8+
洛里昂
9+
雷恩
10+
欧塞尔
1011
摩纳哥
12+
里尔
1113
蒙彼利埃
12-
南特
14+
斯特拉斯堡
1315
尼斯
14-
克莱蒙
15-
巴黎圣日耳曼
16+
南特
17+
昂热
18+
阿雅克肖
1619
兰斯
17-
雷恩
18-
圣埃蒂安
19-
斯特拉斯堡
20-
朗斯
20+
特鲁瓦
+15-15
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
阿森纳
2-
阿斯顿维拉
3-
伯恩利
2+
曼城
3+
利兹联
4+
托特纳姆热刺
5+
布莱顿
6+
纽卡斯尔联
7+
富勒姆
48
布伦特福德
5-
布萊顿
6-
切尔西
79
水晶宫
10+
诺丁汉森林
11+
南安普敦
12+
切尔西
13+
阿斯顿维拉
14+
曼联
15+
伯恩茅斯
16+
利物浦
817
埃弗顿
9-
利兹联
18+
狼队
1019
莱斯特城
11-
利物浦
12-
曼城
13-
曼联
14-
纽卡斯尔联
15-
诺里奇城
16-
南安普顿
17-
托特纳姆热刺
18-
沃特福德
19-
西汉姆联
20-
伍尔弗汉普顿流浪
20+
西汉姆联
+14-14
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
1-
亚特兰大
2-
博洛尼亚
3-
卡利亚里
4-
斯佩齐亚
5-
都灵
6-
佛罗伦萨
7-
赫拉斯维罗纳
8-
热那亚
1+
那不勒斯
92
国际米兰
3+
罗马
104
尤文图斯
11-
拉齐奥
125
AC米兰
13-
那不勒斯
14-
乌迪内斯
15-
罗马
16-
桑普多利亚
6+
亚特兰大
7+
佛罗伦萨
8+
拉齐奥
9+
都灵
1710
萨索洛
11+
斯佩齐亚
12+
博洛尼亚
1813
恩波利
1914
萨勒尼塔纳
20-
威尼斯
15+
乌迪内斯
16+
桑普多利亚
17+
维罗纳
18+
克雷莫内塞
19+
莱切
20+
蒙扎

0 commit comments

Comments
 (0)