生成验证码

本文介绍了一种基于Java的验证码生成方法,包括自定义验证码图片的尺寸、颜色及字符等属性,并通过HTTP响应将验证码图片返回给前端。此外,还实现了前端页面中验证码的刷新功能。

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


controller

@RequestMapping("/drawCheckCode")
public void drawCheckCode(HttpServletResponse resp,HttpSession session) throws IOException {
        resp.setContentType("image/jpg");
        int width = 200;
        int height = 30;
        Captcha c = Captcha.getInstance();
        c.set(width, height);
        String checkcode = c.generateCheckcode();
        session.setAttribute("cc", checkcode);
        OutputStream os = resp.getOutputStream();
        ImageIO.write(c.generateCheckImg(checkcode), "jpg", os);
    }

util

package org.konghao.basic.util;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.util.Random;
public class Captcha {
    private int width;
    private int height;
    private int num;
    private String code;
    private static final Random ran = new Random();
    private static Captcha captcha;
    private Captcha(){
        code = "0123456789";
        num = 4;
    }

    public static Captcha getInstance() {
        if(captcha==null) captcha = new Captcha();
        return captcha;
    }

    public void set(int width,int height,int num,String code) {
        this.width = width;
        this.height = height;
        this.setNum(num);
        this.setCode(code);
    }

    public void set(int width,int height) {
        this.width = width;
        this.height = height;
    }

    public int getWidth() {
        return width;
    }
    public void setWidth(int width) {
        this.width = width;
    }
    public int getHeight() {
        return height;
    }
    public void setHeight(int height) {
        this.height = height;
    }
    public int getNum() {
        return num;
    }
    public void setNum(int num) {
        this.num = num;
    }
    public String getCode() {
        return code;
    }
    public void setCode(String code) {
        this.code = code;
    }

    public String generateCheckcode() {
        StringBuffer cc = new StringBuffer();
        for(int i=0;i<num;i++) {
            cc.append(code.charAt(ran.nextInt(code.length())));
        }
        return cc.toString();
    }

    public BufferedImage generateCheckImg(String checkcode) {
        //创建一个图片对象
        BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        //获取图片对象的画笔
        Graphics2D graphic = img.createGraphics();
        graphic.setColor(Color.WHITE);
        graphic.fillRect(0, 0, width, height);
        graphic.setColor(Color.BLACK);
        graphic.drawRect(0, 0, width-1, height-1);
        Font font = new Font("宋体",Font.BOLD+Font.ITALIC,(int)(height*0.8));
        graphic.setFont(font);
        for(int i=0;i<num;i++) {
            graphic.setColor(new Color(ran.nextInt(180),ran.nextInt(180),ran.nextInt(180)));
            graphic.drawString(String.valueOf(checkcode.charAt(i)), i*(width/num)+4, (int)(height*0.8));
        }

        //加一些点
        for(int i=0;i<(width+height);i++) {
            graphic.setColor(new Color(ran.nextInt(255),ran.nextInt(255),ran.nextInt(255)));
            graphic.drawOval(ran.nextInt(width), ran.nextInt(height), 1, 1);
        }

        //加一些线
        for(int i=0;i<4;i++) {
            graphic.setColor(new Color(ran.nextInt(255),ran.nextInt(255),ran.nextInt(255)));
            graphic.drawLine(0, ran.nextInt(height), width, ran.nextInt(height));
        }
        return img;
    }


}

html+JS

<div class="col-sm-10">
   <input id="checkCode" class="form-control" >
   <img id="codeImg" alt="验证码" src="${base}/usr/drawCheckCode"  style=" margin-top: 10px;margin-right: 18px;"/>
   <span style="width:100px;height:30px"><a href="javascript:void(0)" onclick="changeimg()">看不清楚 换一张</a> </span>
</div>
<script type="text/javascript">
function changeimg() {
    var myimg = document.getElementById("codeImg"); 
    now = new Date(); //如果不设定此事件的话会出现只能换一次的bug
    myimg.src="${base}/usr/drawCheckCode?code="+now.getTime();
} 
</script>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值