java 生成验证码

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.Random;
import javax.imageio.ImageIO;
  
public class ValidationCode {
  
    // 图形验证码的字符集合,系统将随机从这个字符串中选择一些字符作为验证码
    private static String codeChars = "%#23456789abcdefghkmnpqrstuvwxyzABCDEFGHKLMNPQRSTUVWXYZ";
  
    // 返回一个随机颜色(Color对象)
    private static Color getRandomColor(int minColor, int maxColor) {
        Random random = new Random();
        // 保存minColor最大不会超过255
        if (minColor > 255)
            minColor = 255;
        // 保存minColor最大不会超过255
        if (maxColor > 255)
            maxColor = 255;
        // 获得红色的随机颜色值
        int red = minColor + random.nextInt(maxColor - minColor);
        // 获得绿色的随机颜色值
        int green = minColor + random.nextInt(maxColor - minColor);
        // 获得蓝色的随机颜色值
        int blue = minColor + random.nextInt(maxColor - minColor);
        return new Color(red, green, blue);
    }
  
    protected static void getValidationCode() throws IOException {
        try {
            // 获得验证码集合的长度
            int charsLength = codeChars.length();
            // 设置图形验证码的长和宽(图形的大小)
            int width = 90, height = 30;
            BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
            Graphics g = image.getGraphics();// 获得用于输出文字的Graphics对象
            Random random = new Random();
            g.setColor(getRandomColor(180, 250));// 随机设置要填充的颜色
            g.fillRect(0, 0, width, height);// 填充图形背景
            // 设置初始字体
            g.setFont(new Font("Times New Roman", Font.ITALIC, height));
            g.setColor(getRandomColor(120, 180));// 随机设置字体颜色
            // 用于保存最后随机生成的验证码
            StringBuilder validationCode = new StringBuilder();
            // 验证码的随机字体
            String[] fontNames = { "Times New Roman", "Book antiqua", "Arial" };
            // 随机生成3个到5个验证码
            for (int i = 0; i < 3 + random.nextInt(3); i++) {
                // 随机设置当前验证码的字符的字体
                g.setFont(new Font(fontNames[random.nextInt(3)], Font.ITALIC, height));
                // 随机获得当前验证码的字符
                char codeChar = codeChars.charAt(random.nextInt(charsLength));
                validationCode.append(codeChar);
                // 随机设置当前验证码字符的颜色
                g.setColor(getRandomColor(10, 100));
                // 在图形上输出验证码字符,x和y都是随机生成的
                g.drawString(String.valueOf(codeChar), 16 * i + random.nextInt(7), height - random.nextInt(6));
            }
            File file = new File("d:\\code.png");  
            ImageIO.write(image, "png", file);  
            System.out.println(validationCode.toString());
            //byte[] data = ((DataBufferByte) image.getData().getDataBuffer()).getData();
            g.dispose();
        } catch (Exception e) {
            e.printStackTrace();  
        }
    }
  
    public static void main(String[] args) throws IOException{
        getValidationCode();
    }
}

生成验证码可以使用 Java 提供的 BufferedImage 类来创建图片,并在图片上绘制随机生成验证码。以下是一个简单的 Java 代码示例: ``` import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.image.BufferedImage; import java.util.Random; public class CaptchaGenerator { private static final int WIDTH = 120; // 图片宽度 private static final int HEIGHT = 40; // 图片高度 private static final int CODE_LENGTH = 4; // 验证码长度 private static final String CODE_CHARS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; // 验证码字符集 public static BufferedImage generateCaptcha() { // 创建一个 BufferedImage 对象并设置宽度和高度 BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB); // 获取 Graphics 对象以便在图片上绘制 Graphics g = image.getGraphics(); // 设置背景颜色 g.setColor(Color.WHITE); g.fillRect(0, 0, WIDTH, HEIGHT); // 绘制验证码 Random random = new Random(); StringBuilder code = new StringBuilder(); for (int i = 0; i < CODE_LENGTH; i++) { int index = random.nextInt(CODE_CHARS.length()); char c = CODE_CHARS.charAt(index); code.append(c); g.setColor(new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256))); g.setFont(new Font("Arial", Font.BOLD, 24)); g.drawString(String.valueOf(c), i * 30 + 10, 30); } // 添加噪声点 for (int i = 0; i < 50; i++) { int x = random.nextInt(WIDTH); int y = random.nextInt(HEIGHT); g.setColor(new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256))); g.fillOval(x, y, 2, 2); } // 添加噪声线 for (int i = 0; i < 3; i++) { int x1 = random.nextInt(WIDTH); int y1 = random.nextInt(HEIGHT); int x2 = random.nextInt(WIDTH); int y2 = random.nextInt(HEIGHT); g.setColor(new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256))); g.drawLine(x1, y1, x2, y2); } // 释放 Graphics 对象 g.dispose(); return image; } } ``` 该代码会生成一个宽度为 120、高度为 40 的验证码图片。验证码由 4 个随机字符组成,字符集包括大小写字母和数字。在图片上会添加一些噪声点和噪声线以增加验证码的复杂度。可以调整字符集、验证码长度、噪声点数量和噪声线数量等参数以满足需求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值