转:使用java生成数字验证码

原文转载自: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);
}

你可能感兴趣的:(Java)