Skip to content

Commit

Permalink
弃用控制台打印二维码的方式,改用图片文件显示。
Browse files Browse the repository at this point in the history
  • Loading branch information
hzhilong committed Nov 11, 2024
1 parent db00d1c commit 3c1940f
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 7 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<groupId>top.ybgnb</groupId>
<artifactId>bilibili-backup</artifactId>
<description>哔哩哔哩账号备份</description>
<version>1.0.1</version>
<version>1.0.2</version>

<properties>
<maven.compiler.source>8</maven.compiler.source>
Expand Down
26 changes: 20 additions & 6 deletions src/main/java/top/ybgnb/bilibili/backup/CLIApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import top.ybgnb.bilibili.backup.utils.StringUtils;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
Expand Down Expand Up @@ -143,12 +144,25 @@ private static String loginNewUser() throws BusinessException {
OkHttpClient client = new OkHttpClient.Builder().addInterceptor(
new ThrottlingInterceptor(1000)).build();
LoginService loginService = new LoginService(client);
log.info("正在获取登录二维码(长时间不显示的话,可按一下方向键下刷新屏幕)...");
log.info("可尝试按住【Ctrl+鼠标滚轮键】调整字体为较小状态(方便展示二维码)");
log.info("正在获取登录二维码...");
// log.info("正在获取登录二维码(长时间不显示的话,可按一下方向键下刷新屏幕)...");
// log.info("可尝试按住【Ctrl+鼠标滚轮键】调整字体为较小状态(方便展示二维码)");
QRCode qrCode = loginService.generateQRCode();
log.info("请使用手机哔哩哔哩扫码登录:");
QRUtil.printQR(qrCode.getUrl());
log.info("请使用手机哔哩哔哩扫码登录:");
// QRUtil.printQR(qrCode.getUrl());
// log.info("请使用手机哔哩哔哩扫码登录:");
File dir = new File("qr");
if(!dir.exists()){
dir.mkdir();
}
String filePath = "qr/" + System.currentTimeMillis() + ".png";
File file = QRUtil.writeQRFile(qrCode.getUrl(), filePath);
Runtime runtime = Runtime.getRuntime();
try {
runtime.exec("cmd /c \"" + file.getAbsolutePath()+"\"");
} catch (IOException e) {
throw new BusinessException("打开二维码失败");
}
long startTime = System.currentTimeMillis();
String cookie = null;
while (System.currentTimeMillis() - startTime < 180000 && StringUtils.isEmpty(cookie)) {
Expand Down Expand Up @@ -183,12 +197,12 @@ public void fail(User user) {
}
};
if (BuType.BACKUP.equals(buType)) {
upper = new BilibiliBackup(builders,new User(cookie), userInfoCallback).start();
upper = new BilibiliBackup(builders, new User(cookie), userInfoCallback).start();
} else if (BuType.RESTORE.equals(buType)) {
if (StringUtils.isEmpty(readJsonDir)) {
throw new BusinessException("备份文件为空");
}
upper = new BilibiliRestore(builders,readJsonDir, new User(cookie), userInfoCallback).start();
upper = new BilibiliRestore(builders, readJsonDir, new User(cookie), userInfoCallback).start();
} else if (BuType.READ_ALL_MSG.equals(buType)) {
upper = new BilibiliReadAllMsg(new User(cookie), userInfoCallback).start();
}
Expand Down
29 changes: 29 additions & 0 deletions src/main/java/top/ybgnb/bilibili/backup/utils/QRUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
import com.google.zxing.common.BitMatrix;
import top.ybgnb.bilibili.backup.error.BusinessException;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

Expand Down Expand Up @@ -42,4 +46,29 @@ public static void printQR(String content) throws BusinessException {
throw new BusinessException("生成二维码失败");
}
}

public static File writeQRFile(String content, String filePath) throws BusinessException {
// 设置字符集编码
Map<EncodeHintType, Object> hints = new HashMap<>();
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
//设置二维码白边的范围(此值可能不生效)
hints.put(EncodeHintType.MARGIN, 1);
// 生成二维码矩阵
try {
BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, 256, 256, hints);
BufferedImage bufferedImage = new BufferedImage(256, 256, BufferedImage.TYPE_INT_BGR);
for (int j = 0; j < bitMatrix.getHeight(); j++) {
for (int i = 0; i < bitMatrix.getWidth(); i++) {
bufferedImage.setRGB(i, j, bitMatrix.get(i, j) ? 0x000000 : 0xFFFFFF);
}
}
File codeImgFile = new File(filePath);
ImageIO.write(bufferedImage, "png", codeImgFile);
return codeImgFile;
} catch (WriterException e) {
throw new BusinessException("生成二维码失败");
} catch (IOException e) {
throw new BusinessException("保存二维码失败");
}
}
}

0 comments on commit 3c1940f

Please sign in to comment.