|
| 1 | +package run.halo.comment.widget.captcha; |
| 2 | + |
| 3 | +import javax.imageio.ImageIO; |
| 4 | +import java.awt.*; |
| 5 | +import java.awt.image.BufferedImage; |
| 6 | +import java.io.ByteArrayOutputStream; |
| 7 | +import java.io.IOException; |
| 8 | +import java.io.InputStream; |
| 9 | +import java.util.Base64; |
| 10 | +import java.util.Random; |
| 11 | +import lombok.experimental.UtilityClass; |
| 12 | +import org.springframework.util.Assert; |
| 13 | + |
| 14 | +@UtilityClass |
| 15 | +public class CaptchaGenerator { |
| 16 | + private static final String CHAR_STRING = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; |
| 17 | + private static final int WIDTH = 160; |
| 18 | + private static final int HEIGHT = 40; |
| 19 | + private static final int CHAR_LENGTH = 6; |
| 20 | + |
| 21 | + private static final Font customFont; |
| 22 | + |
| 23 | + static { |
| 24 | + customFont = loadArialFont(); |
| 25 | + } |
| 26 | + |
| 27 | + public static BufferedImage generateCaptchaImage(String captchaText) { |
| 28 | + Assert.hasText(captchaText, "Captcha text must not be blank"); |
| 29 | + BufferedImage bufferedImage = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB); |
| 30 | + Graphics2D g2d = bufferedImage.createGraphics(); |
| 31 | + |
| 32 | + // paint white background |
| 33 | + g2d.setColor(Color.WHITE); |
| 34 | + g2d.fillRect(0, 0, WIDTH, HEIGHT); |
| 35 | + |
| 36 | + g2d.setFont(customFont); |
| 37 | + |
| 38 | + // draw captcha text |
| 39 | + Random random = new Random(); |
| 40 | + for (int i = 0; i < captchaText.length(); i++) { |
| 41 | + g2d.setColor(new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256))); |
| 42 | + g2d.drawString(String.valueOf(captchaText.charAt(i)), 20 + i * 24, 30); |
| 43 | + } |
| 44 | + |
| 45 | + // add some noise |
| 46 | + for (int i = 0; i < 10; i++) { |
| 47 | + g2d.setColor(new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256))); |
| 48 | + int x1 = random.nextInt(WIDTH); |
| 49 | + int y1 = random.nextInt(HEIGHT); |
| 50 | + int x2 = random.nextInt(WIDTH); |
| 51 | + int y2 = random.nextInt(HEIGHT); |
| 52 | + g2d.drawLine(x1, y1, x2, y2); |
| 53 | + } |
| 54 | + |
| 55 | + g2d.dispose(); |
| 56 | + return bufferedImage; |
| 57 | + } |
| 58 | + |
| 59 | + private static Font loadArialFont() { |
| 60 | + var fontPath = "/fonts/Arial_Bold.ttf"; |
| 61 | + try (InputStream is = CaptchaGenerator.class.getResourceAsStream(fontPath)) { |
| 62 | + if (is == null) { |
| 63 | + throw new RuntimeException("Cannot load font file for " + fontPath + ", please check if it exists."); |
| 64 | + } |
| 65 | + return Font.createFont(Font.TRUETYPE_FONT, is).deriveFont(Font.BOLD, 24); |
| 66 | + } catch (FontFormatException | IOException e) { |
| 67 | + return new Font("Serif", Font.BOLD, 24); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + public static String generateRandomText() { |
| 72 | + StringBuilder sb = new StringBuilder(CHAR_LENGTH); |
| 73 | + Random random = new Random(); |
| 74 | + for (int i = 0; i < CHAR_LENGTH; i++) { |
| 75 | + sb.append(CHAR_STRING.charAt(random.nextInt(CHAR_STRING.length()))); |
| 76 | + } |
| 77 | + return sb.toString(); |
| 78 | + } |
| 79 | + |
| 80 | + public static String encodeToBase64(BufferedImage image) { |
| 81 | + try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) { |
| 82 | + ImageIO.write(image, "png", outputStream); |
| 83 | + byte[] imageBytes = outputStream.toByteArray(); |
| 84 | + return Base64.getEncoder().encodeToString(imageBytes); |
| 85 | + } catch (IOException e) { |
| 86 | + throw new RuntimeException(e); |
| 87 | + } |
| 88 | + } |
| 89 | +} |
0 commit comments