验证码工具类

RandomCode工具类

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

/**
 * 获取验证码的工具类
 */
public class RandomCode {
    public static String strCode = null;
    static final char[] CHARS = { '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J',
            'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };
    static Random random = new Random();
    private static int codeLength = 4;
    private static int noisePoint = 100;

    /**
     * 获取四位随机数
     *
     * @return
     */
    public static String getRandomString() {
        StringBuffer buffer = new StringBuffer();
        for (int i = 0; i < codeLength; i++) {
            buffer.append(CHARS[random.nextInt(CHARS.length)]);
        }
        strCode = buffer.toString();
        return strCode;
    }

    /**
     * 获取随机颜色
     *
     * @return
     */
    public static Color getRandomColor() {
        return new Color(random.nextInt(255), random.nextInt(255), random.nextInt(255));
    }

    /**
     * 返回某颜色的反色
     *
     * @param c
     * @return
     */
    public static Color getReverseColor(Color c) {
        return new Color(255 - c.getRed(), 255 - c.getGreen(), 255 - c.getBlue());
    }

    /**
     * 创建图片
     *
     * @return 验证码的流格式
     */
    public BufferedImage createImage() {
        // 获取随机字符串
        String randomString = getRandomString();
        // 设置图片的宽度
        int width = 80;
        // 高度
        int height = 30;
        // 获取随机颜色,作为背景色
        Color color = getRandomColor();
        // 获取反色,用于前景色
        Color reverse = getReverseColor(color);

        // 创建一个彩色图片
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        // 获取绘制对象
        Graphics2D g = image.createGraphics();
        // 设置字体
        g.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 23));
        // 设置颜色
        g.setColor(color);
        // 绘制背景
        g.fillRect(0, 0, width, height);
        // 设置颜色
        g.setColor(reverse);
        g.drawString(randomString, 5, 23);

        // 最多绘制100个噪音点
        for (int i = 0, n = random.nextInt(noisePoint); i < n; i++) {
            g.drawRect(random.nextInt(width), random.nextInt(height), 1, 1);
        }

        // 返回验证码图片的流格式
        // ByteArrayInputStream bais = convertImageToStream(image);
        return image;

    }
}

Controller层

@RestController
@RequestMapping("/v2")
public class ZipController {
    /**
     * 获取验证码
     * @param response
     * @param request
     */
    @GetMapping("/getGifCode")
    public void getGifCode(HttpServletResponse response, HttpServletRequest request) {
        RandomCode rc = new RandomCode();
        BufferedImage image = rc.createImage();
        String randomcode = RandomCode.strCode;
        //把生成的验证码放到session域中
        request.getSession().setAttribute("_code", randomcode.toLowerCase());
        try {
            ServletOutputStream outputStream = response.getOutputStream();
            ImageIO.write(image, "JPEG", outputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

前端

<div class="in">
      <div class="in-label">验证码</div>
      <div class="in-text flex flex-c-b">
          <input id="verifyCode" class="text small" placeholder="请输入验证码" type="text"/>
          <img id="refreshCode" src="${ctx }/merchant/${exhibitionId}/getGifCode" class="code cursor"  alt=""/>
     </div>
     </div>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值