验证码生成工具类


验证码生成工具类


import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * java生成的验证码机制,是在指定的字符中随机抽取字符,可以指定字符的个数(验证码个数),
 * 然后通过java.awt.image.BufferedImage;创建画布,拿到 java.awt.Graphics;
 * 画笔在指定的位置画出文字,包括混淆视觉的线条(其实就是一些细小的线条),
 * 然后把呈现的画面生成图片的形式已流的形式打印在指定的窗口或页面中。
 *
 * @author :Carina
 * @method : ValidateCode
 * @date : 2020/10/30 8:25
 */

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

    private int width = 90;     //验证码宽度 默认值:90
    private int height = 40;    //验证码高度 默认值:40
    private int codeCount = 4;  //验证码个数  默认值:4
    private int lineCount = 19; //混淆线个数  默认值:19
    private int  fontSize = 20; //字体大小像素
    //存储session中的key值 默认值:"validateCode"
    private String sessionKey = "validateCode";

    public ValidateCode(){}

    /**
     *
     * @param width 验证码宽度
     * @param height 验证码高度
     * @param fontSize 字体大小像素
     */
    public ValidateCode(int width,int height,int fontSize){
        this.width = width;
        this.height = height;
        this.fontSize = fontSize;
    }

    /**
     *
     * @param width 验证码宽度
     * @param height 验证码高度
     * @param fontSize 字体大小像素
     * @param sessionKey 存储session中的key值
     */
    public ValidateCode(int width,int height,int fontSize,String sessionKey){
        this.width = width;
        this.height = height;
        this.fontSize = fontSize;
        this.sessionKey = sessionKey;
    }

    /**
     *
     * @param width 验证码宽度
     * @param height 验证码高度
     * @param codeCount 验证码个数
     * @param fontSize 字体大小像素
     * @param sessionKey 存储session中的key值
     */
    public ValidateCode(int width,int height,int codeCount,int fontSize,String sessionKey){
        this.width = width;
        this.height = height;
        this.codeCount = codeCount;
        this.fontSize = fontSize;
        this.sessionKey = sessionKey;
    }

    /**
     *
     * @param width 验证码宽度
     * @param height 验证码高度
     * @param codeCount 验证码个数
     * @param lineCount 混淆线个数
     * @param fontSize 字体大小像素
     * @param sessionKey 存储session中的key值
     */
    public ValidateCode(int width,int height,int codeCount,int lineCount,int fontSize,String sessionKey){
        this.width = width;
        this.height = height;
        this.codeCount = codeCount;
        this.lineCount = lineCount;
        this.fontSize = fontSize;
        this.sessionKey = sessionKey;
    }


    char[] codeSequence = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
            'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
            'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };

    /**
     * 具体获取验证码的方法
     * @param time  time为时戳,这样的话可以避免浏览器缓存验证码
     * @throws IOException
     */
    public void getCode(HttpServletRequest request,HttpServletResponse response){
        //定义随机数类
        Random r = new Random();
        //定义存储验证码的类
        StringBuilder builderCode = new StringBuilder();
        //定义画布
        BufferedImage buffImg = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
        //得到画笔
        Graphics g = buffImg.getGraphics();
        //1.设置颜色,画边框
        g.setColor(Color.gray);
        g.drawRect(0,0,width,height);
        //2.设置颜色,填充内部
        g.setColor(Color.white);
        g.fillRect(1,1,width-2,height-2);
        //3.设置干扰线
        // g.setColor(Color.gray);
        for (int i = 0; i < lineCount; i++) {
            int _R = (int)Math.floor(Math.random()*256);
            int _G = (int)Math.floor(Math.random()*256);
            int _B = (int)Math.floor(Math.random()*256);
            g.setColor(new Color(_R, _G, _B, 255));
            g.drawLine(r.nextInt(width),r.nextInt(width),r.nextInt(width),r.nextInt(width));
        }
        //4.设置验证码
        g.setColor(Color.blue);
        //4.1设置验证码字体
        g.setFont(new Font("宋体",Font.BOLD|Font.ITALIC,fontSize));
        for (int i = 0; i < codeCount; i++) {
            char c = codeSequence[r.nextInt(codeSequence.length)];
            builderCode.append(c);
            g.drawString(c+"",((width/codeCount)*i+2),height*4/5);
        }
        try {
             HttpSession session = request.getSession();
            //5.输出到屏幕
            ServletOutputStream sos = response.getOutputStream();
            ImageIO.write(buffImg,"png",sos);
            //6.保存到session中
           
            session.setAttribute(""+sessionKey+"",builderCode.toString());
            //7.禁止图像缓存。
            response.setHeader("Pragma", "no-cache");
            response.setHeader("Cache-Control", "no-cache");
            response.setDateHeader("Expires", 0);
            response.setContentType("image/png");
            //8.关闭sos
            sos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

文章作者: wmg
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 wmg !
  目录