From cc0f90e86923bf9b7b68b6151b3b03bd02fdd45f Mon Sep 17 00:00:00 2001 From: hellodk34 Date: Sat, 9 Apr 2022 22:42:38 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=88=86=E8=BE=A8=E7=8E=87?= =?UTF-8?q?=E5=8F=82=E6=95=B0=EF=BC=8C=E5=8F=AF=E4=BB=A5=E4=B8=8B=E8=BD=BD?= =?UTF-8?q?=E4=B8=8D=E5=90=8C=E5=88=86=E8=BE=A8=E7=8E=87=E7=9A=84=E5=A4=B4?= =?UTF-8?q?=E5=83=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 25 +++++++++++++---------- src/main/java/MainService.java | 36 +++++++++++++++++++++++++--------- 2 files changed, 42 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index 3d02ea8..5e58174 100644 --- a/README.md +++ b/README.md @@ -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,可能官方文档尚未更新) 程序说明: diff --git a/src/main/java/MainService.java b/src/main/java/MainService.java index 8df6aff..ff9bda1 100644 --- a/src/main/java/MainService.java +++ b/src/main/java/MainService.java @@ -33,11 +33,13 @@ 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); @@ -45,26 +47,38 @@ private static String getNowTime() { 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(); @@ -74,19 +88,19 @@ 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(); @@ -94,6 +108,10 @@ private static String apiUrl(String type) { .append(result) .append("?d=") .append(type); + if (pixel != null) { + sb.append("&s=") + .append(pixel); + } return sb.toString(); } }