字符验证码的创建(BufferedImage,随机Random,ImageIO传递图片,使用Math.random()实现局部刷新,attr,removeAttr,setInterval定时器)

本文介绍了字符验证码的生成过程,包括使用BufferedImage、Random类以及ImageIO进行图片处理。通过Math.random()实现局部刷新效果,并探讨了JavaScript中的attr、removeAttr和setInterval定时器在验证码交互中的应用。
摘要由CSDN通过智能技术生成

验证码的作用:区分用户是计算机还是人;验证码有图片、拖拽、短信等各种形式,其中图片形式最为常见:

工具类:

public class VerifyCodeUtils {
   

    /**
     * 创建验证码
     * @param width 图片宽度
     * @param height 图片高度
     * @param length 字符数
     * @param fontName 字体名
     * @return
     */
    public static BufferedImage createValidate(int width, int height, int length, String fontName,
                                               HttpServletRequest request) {
   
        HttpSession session = request.getSession();
        // BufferedImage类是具有缓冲区的Image类,Image类是用于描述图像信息的类
        // 创建一个不带透明色的BufferedImage对象
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

        // 产生Image对象的Graphics对象,该对象可以在图像上进行各种绘制操作
        Graphics graphics = image.createGraphics();
        graphics.fillRect(0, 0, width, height);// 图片大小

        // 绘制边框
        graphics.setColor(Color.LIGHT_GRAY);
        graphics.drawRect(0, 0, width - 1, height - 1);
        graphics.drawRect(1, 1, width - 2, height - 2);

        // 生成指定长度的[a-z]的随机字符串, 包含大小写
        String str = RandomStringUtils.randomAlphabetic(length);
        char[] chs = str.toCharArray(
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值