java实现图形验证码登录工具类

14 篇文章 0 订阅
3 篇文章 0 订阅
package com.mfexcel.common.utils;

import org.springframework.stereotype.Service;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.security.SecureRandom;
import java.util.Random;

/**
 * description :验证码生成工具类
 *
 * @author wanzhicheng 2020/11/19
 */
@Service
public class VerificationCodeUtil {

    // redis key
    public static final String VERIFICATION_CODE_KEY = "verification_code_key:";

    // 验证码字符集  去除:'l'、'I' 生成图片后辨识度不高
    private static final char[] chars = {
            '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
            'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'm', 'n',
            'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
            'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N',
            'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'
    };

    // 字符数量
    private static final int SIZE = 4;

    // 干扰线数量
    private static final int LINES = 10;

    // 宽度
    private static final int WIDTH = 80;

    // 高度
    private static final int HEIGHT = 40;

    // 字体大小
    private static final int FONT_SIZE = 30;

    /**
     * description : create verification code
     *
     * @return Object[0]:验证码字符串 Object[1]:验证码图片 Object[2]:当前时间戳
     * @author wanzhicheng 2020/11/19
     */
    public static Object[] validateImageHandler() {
        // 1.创建空白图片
        BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
        // 2.获取图片画笔
        Graphics graphic = image.getGraphics();
        // 3.设置画笔颜色
        graphic.setColor(Color.WHITE);
        // 4.绘制矩形背景
        graphic.fillRect(0, 0, WIDTH, HEIGHT);
        // 5.画随机字符
        // Random ran = new Random();
        SecureRandom ran = new SecureRandom();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < SIZE; i++) {
            // 取随机字符索引
            int n = ran.nextInt(chars.length);
            // 设置随机颜色
            graphic.setColor(getRandomColor());
            // 设置字体大小
            graphic.setFont(new Font(null, Font.BOLD + Font.ITALIC, FONT_SIZE));
            // 画字符
            graphic.drawString(chars[n] + "", i * WIDTH / SIZE, HEIGHT * 2 / 3);
            // 记录字符
            sb.append(chars[n]);
        }
        // 6.画干扰线
        for (int i = 0; i < LINES; i++) {
            // 设置随机颜色
            graphic.setColor(getRandomColor());
            // 随机画线
            graphic.drawLine(ran.nextInt(WIDTH), ran.nextInt(HEIGHT),
                    ran.nextInt(WIDTH), ran.nextInt(HEIGHT));
        }
        // 7.返回验证码、图片和生成时间戳
        return new Object[]{sb.toString(), image, System.currentTimeMillis()};
    }

    /**
     * description : 随机色
     *
     * @author wanzhicheng 2020/11/19
     */
    private static Color getRandomColor() {
        SecureRandom ran = new SecureRandom();
        return new Color(ran.nextInt(256), ran.nextInt(256), ran.nextInt(256));
    }

}

  BufferedImage bufferedImage = (BufferedImage) objects[1];

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值