天天看點

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);

    } 

    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);  

        }  

    }

JAVA 生成動态驗證碼