変換:javaを使ってデジタル認証コードを生成します.

5817 ワード

原文の転載は以下の通りですhttp://747017186.iteye.com/blog/2275867
 
転載内容:
1、検証コード生成の基礎類:
package com.hljw.health.plat.action.portalpage;
import java.awt.Color;  
import java.awt.Graphics2D;  
import java.awt.geom.AffineTransform;  
import java.util.Random;  
  
/** 
 *          
 *  
 * @author huangjunhua 
 *  
 */  
public class IdentifyingCode {  
    /** 
     *         。 
     */  
    private int width = 80;  
    /** 
     *         。 
     */  
    private int height = 40;  
    /** 
     *       。 
     */  
    private Random random = new Random();  
      
    public IdentifyingCode(){}  
    /** 
     *        
     * @param fc        
     * @param bc        
     * @return  Color  , Color   RGB   。 
     */  
    public Color getRandomColor(int fc, int bc) {  
        if (fc > 255)  
            fc = 200;  
        if (bc > 255)  
            bc = 255;  
        int r = fc + random.nextInt(bc - fc);  
        int g = fc + random.nextInt(bc - fc);  
        int b = fc + random.nextInt(bc - fc);  
        return new Color(r, g, b);  
    }  
      
    /** 
     *       
     * @param g Graphics2D  ,       
     * @param nums         
     */  
    public void drawRandomLines(Graphics2D g ,int nums ){  
        g.setColor(this.getRandomColor(160, 200)) ;  
        for(int i=0 ; i1f)  
                scaleSize = 1f ;  
            trans.scale(scaleSize, scaleSize) ;  
            g.setTransform(trans) ;  
            g.drawString(temp, 15*i+18, 30) ;//         
              
            strbuf.append(temp) ;  
        }  
        g.dispose() ;  
        return strbuf.toString() ;  
    }  
    public int getWidth() {  
        return width;  
    }  
    public void setWidth(int width) {  
        this.width = width;  
    }  
    public int getHeight() {  
        return height;  
    }  
    public void setHeight(int height) {  
        this.height = height;  
    }  
}     
2、ページ要求で認証コードピクチャを生成するためのservletを専門に担当する:
package com.hljw.health.plat.action.portalpage;
import java.awt.Font;  
import java.awt.Graphics2D;  
import java.awt.image.BufferedImage;  
import java.io.IOException;  
  
import javax.imageio.ImageIO;  
import javax.servlet.ServletException;  
import javax.servlet.http.HttpServlet;  
import javax.servlet.http.HttpServletRequest;  
import javax.servlet.http.HttpServletResponse;  
  
@SuppressWarnings("serial")  
public class PictureCheckCode extends HttpServlet {  
      
    public PictureCheckCode() {  
        super();  
    }  
      
    public void init() throws ServletException {  
        super.init() ;  
    }  
    public void destroy() {  
        super.destroy();   
    }  
  
    public void doGet(HttpServletRequest request, HttpServletResponse response)  
            throws ServletException, IOException {  
        doPost(request, response) ;  
  
    }  
  
    public void doPost(HttpServletRequest request, HttpServletResponse response)  
            throws ServletException, IOException {  
        //         
        response.setHeader("Pragma", "No-cache");  
        response.setHeader("Cache-Control", "No-cache");  
        response.setDateHeader("Expires", 0) ;  
        //           
        response.setContentType("image/jpeg") ;  
        IdentifyingCode idCode = new IdentifyingCode();  
        BufferedImage image =new BufferedImage(idCode.getWidth() , idCode.getHeight() , BufferedImage.TYPE_INT_BGR) ;  
        Graphics2D g = image.createGraphics() ;  
        //        
        Font myFont = new Font("  " , Font.BOLD , 25) ;  
        //      
        g.setFont(myFont) ;  
          
        g.setColor(idCode.getRandomColor(200 , 250)) ;  
        //      
        g.fillRect(0, 0, idCode.getWidth() , idCode.getHeight()) ;  
          
        g.setColor(idCode.getRandomColor(180, 200)) ;  
        idCode.drawRandomLines(g, 160) ;  
        String verifyCode=idCode.drawRandomString(4, g) ;  
        System.out.println("**************"+verifyCode);
        request.getSession().setAttribute("verifyCode", verifyCode);//       session  
        g.dispose() ;  
        ImageIO.write(image, "JPEG", response.getOutputStream()) ;  
    }  
}  
3、web.xmlファイルの構成:

  
	PictureCheckCode  
	com.hljw.health.plat.action.portalpage.PictureCheckCode  
  
  
	PictureCheckCode  
	/PictureCheckCode.action  
4、JSPページ要求:
ページ構造:

 
JS:
$(function(){	
	reflushVerify();//     
});

//     
function reflushVerify(){
	var imgsrc="PictureCheckCode?random="+Math.random();//                   ,            ,       ,      
	$("#JS_captcha").attr("src",imgsrc);
}