验证码

图片验证码简单示例

1、ImageCode.java

package com.example.entity;

import java.awt.image.BufferedImage;

/**
 * Description:图片验证码类
 * Package:com.example.entity
 *
 * @author lightbc
 * @version 1.0
 */
public class ImageCode {

    private BufferedImage image;

    public ImageCode(BufferedImage image){
        this.image=image;
    }

    public BufferedImage getImage() {
        return image;
    }

    public void setImage(BufferedImage image) {
        this.image = image;
    }
}

2、ImageCodeUtil.java

package com.example.utils;

import com.example.entity.ImageCode;

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

/**
 * Description:图片二维码处理工具类
 * Package:com.example.utils
 *
 * @author lightbc
 * @version 1.0
 */
public class ImageCodeUtil {

    //随机验证码取值范围
    private static String CODE_RANDOM_RANGE = "0123456789QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm";

    //图片验证码的宽
    private static int IMAGE_CODE_WIDTH = 100;

    //图片验证码的高
    private static int IMAGE_CODE_HEIGHT = 50;

    //验证码的长度
    private static int IMAGE_CODE_LEN = 6;

    private Random random = new Random();

    /**
     * 创建图片验证码
     * @return imagecode对象
     */
    public ImageCode createCode(){
        String sRandom;
        BufferedImage image = new BufferedImage(IMAGE_CODE_WIDTH,IMAGE_CODE_HEIGHT,BufferedImage.TYPE_INT_RGB);
        Graphics2D g2 = image.createGraphics();
        //设置图片验证码背景色为白色
        g2.setBackground(Color.white);
        g2.fillRect(0,0,IMAGE_CODE_WIDTH,IMAGE_CODE_HEIGHT);
        g2.setFont(new Font("宋体",Font.ITALIC,30));
        for(int i=0;i<IMAGE_CODE_LEN;i++){
            int randomIndex = random.nextInt(CODE_RANDOM_RANGE.length());
            sRandom=String.valueOf(CODE_RANDOM_RANGE.charAt(randomIndex));
            g2.setColor(createRandomColor(0,100));
            g2.drawString(sRandom,15*i+5,35);
        }
        //在图片验证码中间划条横线
        g2.drawLine(0,IMAGE_CODE_HEIGHT/2,IMAGE_CODE_WIDTH,IMAGE_CODE_HEIGHT/2);
        return new ImageCode(image);
    }

    /**
     * 生成随机RGB颜色
     * @param bc 最小值
     * @param ac 最大值
     * @return color
     */
    public Color createRandomColor(int bc,int ac){
        int r = ac+random.nextInt(ac-bc);
        int g = ac+random.nextInt(ac-bc);
        int b = ac+random.nextInt(ac-bc);
        return new Color(r,g,b);
    }
}

3、ImageCodeController.java

package com.example.controller;

import com.example.entity.ImageCode;
import com.example.utils.ImageCodeUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Description:图片验证码控制器
 * Package:com.example.controller
 *
 * @author lightbc
 * @version 1.0
 */
@Controller
@RequestMapping("/auth/ic")
public class ImageCodeController {

    @GetMapping(value = "/code/index")
    public String codeIndex(){
        return "code";
    }

    @GetMapping(value = "/get/image/code")
    @ResponseBody
    public void getImageCode(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ImageCode imageCode = new ImageCodeUtil().createCode();
        ImageIO.write(imageCode.getImage(),"JPEG",response.getOutputStream());
    }

}

4、效果展示

 

 

posted @ 2021-10-18 21:22  lightbc  阅读(286)  评论(0编辑  收藏  举报