JAVA 生成动态验证码

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' }; 

public void getCode(HttpServletRequest req, HttpServletResponse resp) throws Exception {

        int width = 80;// 定义图片的width
        int height = 30;// 定义图片的height
        int fontHeight = 25; // 字体
        
        double rate=1.3;
        StringBuffer randomCode = new StringBuffer();  
        
        Random random = new Random();
     // 定义图像buffer
        BufferedImage buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics gd = buffImg.getGraphics();
        // 随机产生codeCount数字的验证码。  
        for (int i = 0; i < 4; i++) {  
            // 得到随机产生的验证码数字。  
            String strRand = String.valueOf(codeSequence[random.nextInt(36)]);  
            // 将产生的四个随机数组合在一起。  
            randomCode.append(strRand);  
        }  
        
        
        //居中显示  
       
        // 将图像填充为白色
        gd.setColor(Color.WHITE);
        gd.fillRect(0, 0, width, height);
        // 创建字体,字体的大小应该根据图片的高度来定。
        Font font = new Font("Consolas", Font.PLAIN, fontHeight);
        // 设置字体。
        gd.setFont(font);
        // 画边框。
        gd.setColor(Color.WHITE);
        gd.drawRect(0, 0, width - 1, height - 1);
        // 验证码
        String numberCode = randomCode.toString();//RandomCodeUtils.getNumberCode();
        setValidateCodeColor(numberCode, width,height, rate, gd);
        // 设置redis缓存
        //this.setCodeCache(req, numberCode);
        // 禁止图像缓存。
        resp.setHeader("Pragma", "no-cache");
        resp.setHeader("Cache-Control", "no-cache");
        resp.setDateHeader("Expires", 0);
        resp.setContentType("image/jpeg");


        // 将图像输出到Servlet输出流中。
        ServletOutputStream sos = resp.getOutputStream();
        ImageIO.write(buffImg, "jpeg", sos);
        sos.close();
    }
    
    private void setValidateCodeColor(String str,int width,int height,double rate,Graphics g){  
        int x=(int)(width/2-rate*g.getFontMetrics().stringWidth(str)/2);  
        int y=height/2+g.getFontMetrics().getHeight()/3;  
        String tempStr = new String();  
        int orgStringWight=g.getFontMetrics().stringWidth(str);  
        int orgStringLength=str.length();  
        int tempx=x;  
        int tempy=y;  
        int red = 0, green = 0, blue = 0; 
        Random random = new Random(); 
        while(str.length()>0)  
        {  
            tempStr=str.substring(0, 1);  
            str=str.substring(1, str.length()); 
            red = random.nextInt(255);  
            green = random.nextInt(255);  
            blue = random.nextInt(255);
            g.setColor(new Color(red, green, blue)); 
            g.drawString(tempStr, tempx, tempy);  
            tempx=(int)(tempx+(double)orgStringWight/(double)orgStringLength*rate); 
        }  
        setDrawLine(g, width, height);
    } 
    
    /**
     * 设置干扰线
     * @param g
     * @param width
     * @param height
     */
    private void setDrawLine(Graphics g,int width,int height){
        Random random = new Random();
        for (int i = 0; i < 60; i++) {  
            int x = random.nextInt(width);  
            int y = random.nextInt(height);  
            int xl = random.nextInt(12);  
            int yl = random.nextInt(12);  
            g.drawLine(x, y, x + xl, y + yl);  
        }  

    }


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值