Java生成图片验证码方法

生成图片验证码的代码如下 :

package com.czxy.changgou.controller;

import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * @description:
 * @author: ZhaoRenHui
 * @date: Created in 2020/6/23 9:51
 * @version: 1.0
 * @modified By:
 */
@RestController
@RequestMapping("/veritycode")
public class VerityCodeController {

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    /**
     * @description  生成验证码图片
     * @author ZhaoRenHui
     * @date 2020/6/23 9:54
     * @param username  将数据添加到redis使用,可不选
     * @param response  将生成的图片写出所需response对象
     * @return void
     */
    @GetMapping
    public void verifyCode(String username, HttpServletResponse response) throws IOException {
        //1 绘制图片
        //字体只显示大写,去掉了1,0,i,o几个容易混淆的字符
        String VERIFY_CODES = "23456789ABCDEFGHJKLMNPQRSTUVWXYZ";

        int IMG_WIDTH = 72;
        int IMG_HEIGTH = 27;
        Random random = new Random();
        //创建图片
        BufferedImage image = new BufferedImage(IMG_WIDTH, IMG_HEIGTH, BufferedImage.TYPE_INT_RGB);
        //画板
        Graphics g = image.getGraphics();
        //填充背景
        g.setColor(Color.WHITE);
        g.fillRect(1,1,IMG_WIDTH-2,IMG_HEIGTH-2);

        //设置字体
        g.setFont(new Font("楷体",Font.BOLD,25));

        /** #1 提供变量保存随机字符数据 */
        StringBuilder sb = new StringBuilder();

        //绘制4个字符
        for(int i = 1 ; i <= 4 ; i ++){
            //随机颜色
            g.setColor(new Color(random.nextInt(255),random.nextInt(255),random.nextInt(255)));
            //随机生成4个字符
            int len = random.nextInt(VERIFY_CODES.length());
            String str = VERIFY_CODES.substring(len,len+1);

            /** #2 存放随机字符串 */
            sb.append( str );

            g.drawString(str, IMG_WIDTH / 6 * i , 22 );
        }
        /** #3 获得随机字符串 */
        String randomStr = sb.toString();

        /** #4 将结果保存到redis */
        String key = "login" + username;
        stringRedisTemplate.opsForValue().set(key, randomStr , 5 , TimeUnit.MINUTES);


        // 生成随机干扰线
        for (int i = 0; i < 30; i++) {
            //随机颜色
            g.setColor(new Color(random.nextInt(255),random.nextInt(255),random.nextInt(255)));
            int x = random.nextInt(IMG_WIDTH - 1);
            int y = random.nextInt(IMG_HEIGTH - 1);
            int x1 = random.nextInt(12) + 1;
            int y1 = random.nextInt(6) + 1;
            g.drawLine(x, y, x - x1, y - y1);
        }


        //2 将图片响应给浏览器
        ImageIO.write(image,"jpeg", response.getOutputStream());
    }
}

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值