From 831ae448cf4418c126d96684e188add9207e8215 Mon Sep 17 00:00:00 2001 From: wzp Date: Tue, 24 Dec 2024 16:24:59 +0800 Subject: [PATCH] feat: change the verify code image size to 240x80 --- .../org/blue/club/services/UserServices.java | 1 - .../org/blue/club/utils/VerifyCodeUtils.java | 18 ++++++++++++------ 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/src/main/java/org/blue/club/services/UserServices.java b/src/main/java/org/blue/club/services/UserServices.java index 3c11abb..8c1137c 100644 --- a/src/main/java/org/blue/club/services/UserServices.java +++ b/src/main/java/org/blue/club/services/UserServices.java @@ -186,7 +186,6 @@ public class UserServices { public Result getVerifyCode() { VerifyCodeResponse verifyCodeResponse = verifyCodeUtils.generateVerifyCode(); - //TODO ERROR CANNOT CREATE RESPONSE BODY return Result.success(verifyCodeResponse); } diff --git a/src/main/java/org/blue/club/utils/VerifyCodeUtils.java b/src/main/java/org/blue/club/utils/VerifyCodeUtils.java index be15011..a18b749 100644 --- a/src/main/java/org/blue/club/utils/VerifyCodeUtils.java +++ b/src/main/java/org/blue/club/utils/VerifyCodeUtils.java @@ -20,14 +20,19 @@ import java.util.Random; @Component @RequiredArgsConstructor public class VerifyCodeUtils { - public static final int width = 120; - public static final int height = 40; - public static final int fontSize = 25; + public static final int width = 240; + public static final int height = 80; + public static final int fontSize = 50; public static final Font mainFont = new Font("宋体", Font.BOLD, fontSize); private final RandomUtils randomUtils; private final VerifyDao verifyDao; + private void setToRandomColor(Graphics g) { + Random random = new Random(); + g.setColor(new Color(20 + random.nextInt(120), 20 + random.nextInt(120), 20 + random.nextInt(120))); + } + @SneakyThrows public VerifyCodeResponse generateVerifyCode() { BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); @@ -37,9 +42,10 @@ public class VerifyCodeUtils { int x = 0; for (char c : text.toCharArray()) { Random random = new Random(); - g.setColor(new Color(20 + random.nextInt(120), 20 + random.nextInt(120), 20 + random.nextInt(120))); - g.drawString(c + "", 15 + x * 20, 20 + new Random().nextInt(10)); - for (int j = 0; j < random.nextInt(1, 5); j++) { + setToRandomColor(g); + g.drawString(c + "", 15 + x * 40, 40 + new Random().nextInt(10)); + for (int j = 0; j < random.nextInt(3, 7); j++) { + setToRandomColor(g); g.drawLine(random.nextInt(width), random.nextInt(height), random.nextInt(width), random.nextInt(height)); } x++;