【验证码】使用Java生成验证码(字符只扭曲了一次)

  • 作者: 凯哥Java(公众号:凯哥Java)
  • 工作小总结
  • 时间:2023-06-12 11:30
  • 1581人已阅读
简介 需求:随机生成的长度不少于4位的字符,应包含字母与数字,并在显示时对其进行扭曲处理,提高识别的难度效果:代码:import javax.imageio.ImageIO;import java.awt.*;import java.awt.image.BufferedImage;import java.io.File;import java.io.IO

🔔🔔好消息!好消息!🔔🔔

 如果您需要注册ChatGPT,想要升级ChatGPT4。凯哥可以代注册ChatGPT账号代升级ChatGPT4

有需要的朋友👉:微信号 kaigejava2022

需求:

随机生成的长度不少于4位的字符,应包含字母与数字,并在显示时对其进行扭曲处理,提高识别的难度

效果:

a947cc223d3646b87631b0c02d1d1498.png

代码:

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.security.SecureRandom;
public class DistortedText {
    public static void main(String[] args) {
        int width = 200;
        int height = 80;
        String randomString = generateRandomString(4);
        String filename = "distorted_text.png";
        drawDistortedText(width, height, randomString, filename);
    }

    private static String generateRandomString(int length) {
        String lettersAndDigits = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        StringBuilder sb = new StringBuilder();
        SecureRandom random = new SecureRandom();
        for (int i = 0; i < length; i++) {
            sb.append(lettersAndDigits.charAt(random.nextInt(lettersAndDigits.length())));
        }
        return sb.toString();
    }

    private static void drawDistortedText(int width, int height, String text, String filename) {
        SecureRandom random = new SecureRandom();
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics2D graphics2D = image.createGraphics();

        // 设置抗锯齿
        graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        // 填充背景颜色
        graphics2D.setColor(Color.WHITE);
        graphics2D.fillRect(0, 0, width, height);

        // 选择字体和大小
        Font font = new Font("Arial", Font.BOLD, 28);
        graphics2D.setFont(font);

        // 绘制字符串
        int textWidth = graphics2D.getFontMetrics().stringWidth(text);
        int textHeight = graphics2D.getFontMetrics().getHeight();
        graphics2D.setColor(Color.BLACK);
        graphics2D.drawString(text, (width - textWidth) / 2, (height - textHeight) / 2 + textHeight);

        // 扭曲变换
        int xs = random.nextInt(width / 8);
        int ys = 0;
        int xe = width - random.nextInt(width / 8);
        int ye = height;
        int x1 = random.nextInt(width / 8);
        int y1 = height - random.nextInt(height / 8);
        int x2 = width - random.nextInt(width / 8);
        int y2 = height - random.nextInt(height / 8);
        int[] xPoints = {xs, xe, x1, x2};
        int[] yPoints = {ys, ye, y1, y2};
        graphics2D.setColor(Color.WHITE);
        graphics2D.fillPolygon(xPoints, yPoints, 4);

        // 保存为文件
        try {
            ImageIO.write(image, "png", new File(filename));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

说明:

这段代码使用了Java的javax.imageio和java.awt库来绘制图形和进行图形变换。它首先生成了一个白色的背景,并选择了一个字体和大小来绘制字符串。然后,它使用graphics2D.getFontMetrics()方法来计算字符串的大小,并将其绘制在画布中央。最后,它使用Graphics2D类提供的fillPolygon()方法进行扭曲变换,并将生成的图片保存到文件中。

提示:述代码中只是简单地使用一次扭曲变换,扭曲效果很难达到理想的效果

需要理想的扭曲,请看下一篇

TopTop