验证码工具类

验证码在项目中几乎是再寻常不过的了,本博客只为了使用时的方便特进行一个记录。高手可以忽略,不喜勿喷!

废话不说,直接看工具类

import java.awt.*;
import java.awt.color.ColorSpace;
import java.awt.image.BufferedImage;
import java.util.Random;

/**
 * 验证码工具类
 */
public class CaptchaUtil {

    /**
     * 生成验证码
     * @param width         验证码宽度
     * @param height        验证码高度
     * @param codeCount     验证码个数
     * @param lineCount     干扰线个数
     * @param lineLenght    干扰线长度
     * @return  验证码对象
     */
    public static Captcha createCaptcha(int width, int height, int codeCount, int lineCount, int lineLenght) {
        BufferedImage image = new BufferedImage(width, height, ColorSpace.TYPE_Lab);
        Graphics g = image.getGraphics();
        Random random = new Random();

        // 取颜色区间中较淡的部分
        g.setColor(getRandColor(200, 250));
        g.fillRect(0, 0, width, height);
        g.setFont(new Font("Times New Roman", Font.PLAIN, 20));

        g.setColor(getRandColor(160,200));
        // 干扰线
        for (int i = 0; i < lineCount; ++i) {
            int x = random.nextInt(width);
            int y = random.nextInt(height);
            int xLength = random.nextInt(lineLenght);
            int yLength = random.nextInt(lineLenght);
            g.drawLine(x, y, x + xLength, y + yLength);
        }

        StringBuilder code = new StringBuilder();

        // 生成验证码
        for (int i = 0; i < codeCount; ++i) {
            String rand = String.valueOf(random.nextInt(10));
            code.append(rand);
            g.setColor(new Color(20 + random.nextInt(110), 20 + random.nextInt(110), 20 + random.nextInt(110)));
            g.drawString(rand, (width / codeCount) * i + (width / codeCount / 2),  height / 2 + 9);
        }

        g.dispose();
        return new Captcha(code.toString(), image);
    }

    /**
     * 给定范围内获取颜色值
     */
    private static Color getRandColor(int fc, int bc) {
        Random random = new Random();
        if (fc > 255) {
            fc = 255;
        }

        if (bc > 255) {
            bc = 255;
        }

        int r = fc + random.nextInt(bc - fc);
        int g = fc + random.nextInt(bc - fc);
        int b = fc + random.nextInt(bc - fc);
        return new Color(r, g, b);
    }

    /**
     * 验证码对象
     */
    public static class Captcha {
        private final String code;
        private final BufferedImage image;

        public String getCode() {
            return code;
        }

        public BufferedImage getImage() {
            return image;
        }

        private Captcha(String code, BufferedImage image) {
            this.code = code;
            this.image = image;
        }
    }


}

在controller层的调用

@GetMapping("/captcha")
    public void captcha(HttpServletResponse response) throws IOException {
        //定义图形验证码的长、宽、验证码字符数、干扰元素个数
        CaptchaUtil.Captcha captcha = CaptchaUtil.createCaptcha(140, 38, 4, 10, 30);
        Session session = SecurityUtils.getSubject().getSession();
        session.setAttribute("captcha", captcha.getCode());

        response.setContentType("image/png");
        OutputStream os = response.getOutputStream();
        ImageIO.write(captcha.getImage(), "png", os);
    }

简单的验证码就诞生了,代码拿走直接可以使用哦~~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值