Skip to content

Commit

Permalink
增加分辨率参数,可以下载不同分辨率的头像
Browse files Browse the repository at this point in the history
  • Loading branch information
hellodk34 committed Apr 9, 2022
1 parent 43ef88a commit cc0f90e
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 19 deletions.
25 changes: 15 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,34 +1,39 @@
# avatars

gravatar 风格头像快速生成器。基于随机邮箱生成以下类型的头像
gravatar 风格头像快速生成器。基于随机邮箱生成随机头像。

- identicon style: a geometric pattern based on an email hash
- retro style: awesome generated, 8-bit arcade-style pixelated faces
- robohash style: a generated robot with different colors, faces, etc
- 支持下载任意数量头像
- 支持不同风格头像
- 支持设置头像分辨率,满足不同尺寸头像的需求

![20220408210043](https://pic.dogimg.com/2022/04/08/62503207ea4c5.png)
![20220409223739](https://pic.dogimg.com/2022/04/09/62519a3fc5831.png)

# 前言

本项目之前用了一个 [API](https://api.prodless.com/avatar.png) ,但是现在该 API 已经停止服务了。现借助 gravatar 官方的服务写了此程序。个人感觉现在注册的网站越来越多,很多网站注册后默认不提供头像,需要用户自行上传,我经常为找不到合适的头像发愁,每个网站使用相同的头像又觉得泄露了隐私,于是我就产生了这样的需求,生成这些静态文件后保存在本地文件夹中,以后设置头像时随便挑选一个喜欢的即可。

# 版本日志

- `v1.0.0` 版本过于久远,由于原 API 失效,现已不提供 jar 包下载。released on 2021-09-26
- `v1.0.1` 基于官方 API 实现,可以下载多种风格的头像。released on 2022-04-08
- `v1.0.2` 增加分辨率参数,可以下载不同分辨率的头像。released on 2022-04-09

# 使用说明

![20220408211743](https://pic.dogimg.com/2022/04/08/62503603930d3.png)
![20220409224058](https://pic.dogimg.com/2022/04/09/62519b06c85a5.png)

```
java -jar app.jar saveFolder downloadNumber TYPE
java -jar /path/to/app.jar SAVE_FOLDER DOWNLOAD_NUMBER TYPE PIXEL
```

参数说明:
程序依次接收 4 个参数,分别是 `保存路径``欲下载数量``风格类型``分辨率`参数说明:

1. `SAVE_FOLDER`: 准备保存的文件夹
1. Windows CMD 或 Powershell 使用 `d:\aaa` 这种形式,Windows GitBash 使用 `/d/aaa` 这种形式,Windows WSL 使用 `/mnt/d/aaa` 这种形式(tips: Windows 路径名不区分大小写)
2. macOS、Linux 使用 `/mnt/ssd/aaa` 这种形式
2. `DOWNLOAD_NUMBER`: 下载的头像数量
3. `TYPE`: 风格类型,取值有 identicon | retro | robohash

比如在 Windows 10 cmd 下执行:`java -jar d:\app.jar d:\aaa 10 robohash` 将会在 D 盘的 aaa 文件夹下保存 10 张 robohash 风格的头像文件。
4. `PIXEL`: 支持 1 ~ 2048 之间的整数值(官方文档当中说最大是 1024,经测试最大应该是 2048,可能官方文档尚未更新)

程序说明:

Expand Down
36 changes: 27 additions & 9 deletions src/main/java/MainService.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,38 +33,52 @@ public class MainService {

private static MockNeat mockNeat;

private static Integer pixel = null;

static {
mockNeat = MockNeat.secure();
}

private static String getNowTime() {
private String getNowTime() {
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd.HH-mm-ss");
return sdf.format(date);
}

public static void main(String[] args) {
int argsLength = args.length;
if (argsLength != 3) {
System.out.println("Usage: java -jar app.jar SAVE_FOLDER DOWNLOAD_NUMBER TYPE" + System.lineSeparator() +
"Example: java -jar app.jar d:\\aaa 10 identicon" + System.lineSeparator() +
if (argsLength != 3 && argsLength != 4) {
System.out.println("Usage: java -jar /path/to/app.jar SAVE_FOLDER DOWNLOAD_NUMBER TYPE PIXEL" + System.lineSeparator() + System.lineSeparator() +
"Example: java -jar d:\\app.jar d:\\aaa 10 identicon 240" + System.lineSeparator() + System.lineSeparator() +
"SAVE_FOLDER: the folder path you want to store these images" + System.lineSeparator() +
"DOWNLOAD_NUMBER: number you want to save to local disk" + System.lineSeparator() +
"TYPE: identicon | retro | robohash" + System.lineSeparator() +
" identicon: a geometric pattern based on an email hash" + System.lineSeparator() +
" retro: awesome generated, 8-bit arcade-style pixelated faces" + System.lineSeparator() +
" robohash: a generated robot with different colors, faces, etc"
" robohash: a generated robot with different colors, faces, etc" + System.lineSeparator() +
"PIXEL(optional): a number between 1 and 2048, default is 80 if you did not specify this parameter"
);
return;
}
String folder = args[0];
Integer LOOP_COUNT = Integer.parseInt(args[1]);
String type = args[2];

if (argsLength == 4) {
pixel = Integer.parseInt(args[3]);
if (pixel < 1 || pixel > 2048) {
System.out.println("PIXEL should be between 1 and 2048.");
return;
}
}
MainService mainService = new MainService();
Date now = new Date();
Timer timer = new Timer();
TimerTask task = new TimerTask() {
@Override
public void run() {
if (seq < LOOP_COUNT) {
foo(folder, seq, type);
mainService.foo(folder, seq, type, pixel);
++seq;
} else {
timer.cancel();
Expand All @@ -74,26 +88,30 @@ public void run() {
timer.schedule(task, now, period);
}

private static void foo(String folder, Integer seq, String type) {
private void foo(String folder, Integer seq, String type, Integer pixel) {
Path path = Paths.get(folder);
StringBuilder sb = new StringBuilder();
String nowTime = getNowTime();
sb.append(path).append("/").append(nowTime).append(".png");
File file = new File(sb.toString());
HttpRequest request = HttpRequest.get(apiUrl(type));
HttpRequest request = HttpRequest.get(apiUrl(type, pixel));
HttpResponse response = request.timeout(40000).execute();
response.writeBody(file);
System.out.println("Current output file sequence is " + (seq + 1) + ", fileName is " + nowTime + ".png");
}

private static String apiUrl(String type) {
private String apiUrl(String type, Integer pixel) {
String fakeEmail = mockNeat.emails().val().trim().toLowerCase();
String result = DigestUtils.md5Hex(fakeEmail).toLowerCase();
StringBuilder sb = new StringBuilder();
sb.append(serverUrl)
.append(result)
.append("?d=")
.append(type);
if (pixel != null) {
sb.append("&s=")
.append(pixel);
}
return sb.toString();
}
}

0 comments on commit cc0f90e

Please sign in to comment.