验证码生成器

本文介绍了如何创建验证码生成器,包括关键代码段,帮助理解验证码生成过程中的逻辑和技术细节。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

验证码生成器代码

package com.zgd.learnbase.image;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;
import javax.imageio.ImageIO;

/**
 * 生成验证码
 */
public class VerifyCode
{
    /**
     * 验证码高度
     */
    private int height = 35;
    /**
     * 验证码宽度
     */
    private int width = 70;
    /**
     * 生成验证码字符个数
     */
    private int num = 4;

    /**
     * 干扰线个数
     */
    private int lineNum = 3;

    /**
     * 随机数生成对象
     */
    private Random random = new Random();

    /**
     * 字体样式名称: "宋体", "华为楷体", "黑体", "微软雅黑", "楷体", "楷体_GB2312"
     */
    private String[] fontNames = new String[]
    { "宋体", "华为楷体", "黑体", "微软雅黑", "楷体", "楷体_GB2312" };
    /**
     * 显示字符,不包含i,l,o,0易混淆的字符
     */
    private String codes = "23456789abcdefjhjkmnpqrstuvwxyzABCDEFGHJKMNPQRSTUVWXYZ";

    /**
     * 背景颜色
     */
    private Color bgColor = new Color(255, 255, 255);
    /**
     * 生成的文本
     */
    private String text;

    /**
     * 生成随机颜色,不显白
     */
    private Color randomColor()
    {
        int red = random.nextInt(150);
        int green = random.nextInt(150);
        int blue = random.nextInt(150);
        return new Color(red, green, blue);
    }

    /**
     * 生成随机字体
     */
    private Font randomFont()
    {
        int index = random.nextInt(fontNames.length);
        String fontName = fontNames[index];
        // 生成随机样式:0无样式,1粗体,2斜体,3粗体加斜体
        int style = random.nextInt(4);
        // 生成随机字号大小
        int size = random.nextInt(5) + 24;
        return new Font(fontName, style, size);
    }

    /**
     * 画干扰线
     */
    private void drawLine(BufferedImage image)
    {
        Graphics2D g2 = (Graphics2D) image.getGraphics();
        for (int i = 0; i < lineNum; i++)
        {
            // 一条线两个点坐标
            int x1 = random.nextInt(width);
            int y1 = random.nextInt(height);
            int x2 = random.nextInt(width);
            int y2 = random.nextInt(height);
            g2.setStroke(new BasicStroke(1.5f));
            // 设置干扰线颜色
            g2.setColor(Color.blue);
            g2.drawLine(x1, y1, x2, y2);
        }
    }

    /**
     * 随机生成字符
     */
    private char randomChar()
    {
        int index = random.nextInt(codes.length());
        return codes.charAt(index);
    }

    /**
     * 创建图片缓存
     */
    private BufferedImage createImage()
    {
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2 = (Graphics2D) image.getGraphics();
        g2.setColor(this.bgColor);
        g2.fillRect(0, 0, width, height);
        return image;
    }

    /**
     * 得到图片对象
     */
    public BufferedImage getImage()
    {
        BufferedImage image = createImage();
        Graphics2D g2 = (Graphics2D) image.getGraphics();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < num; i++)
        {
            String s = randomChar() + "";
            sb.append(s);
            float x = i * 1.0f * width / num;
            g2.setFont(randomFont());
            g2.setColor(randomColor());
            g2.drawString(s, x, height - 5);
        }
        this.text = sb.toString();
        drawLine(image);
        return image;
    }

    /**
     * 得到图片文本内容
     */
    public String getText()
    {
        return this.text;
    }

    /**
     * 将图片输出到out流中
     */
    public static void output(BufferedImage image, OutputStream out) throws IOException
    {
        ImageIO.write(image, "JPEG", out);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值