码迷,mamicode.com
首页 > 其他好文 > 详细

验证码

时间:2015-05-14 22:06:50      阅读:170      评论:0      收藏:0      [点我收藏+]

标签:java 验证码

今天用wf框架写了个验证码 记录下来备用
public class ImageCreateController  extends BaseController {
	
	@Path("/imageCreater")
	@GET
	public ActionResult handleRequest() throws IOException{
		HttpServletResponse response=beat().getResponse();
		HttpServletRequest  request=beat().getRequest();
		HttpSession session=request.getSession();
		response.setDateHeader("Expires",0); 
		response.setHeader("Cache-Control","no-store, no-cache, must-revalidate"); 
		response.addHeader("Cache-Control","post-check=0, pre-check=0");
		response.setHeader("Pragma","no-cache");
	    response.setContentType("image/jpeg");
	    BufferedImage bi=null;
	    String text=createText();
	    bi=createImage(text);
	    session.setAttribute("identifyCode", text);
	    ServletOutputStream out= response.getOutputStream();
	    ImageIO.write(bi,"jpg",out);
	    
	    try {
			out.flush();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally{
			out.close();
		}
	    return null;
	}
   public String createText(){
	   String text="";
	   for(int i=0;i<4;i++){
		   int t=(int) (Math.random()*10);
		   text+=t;
	   }
	   return text;
   }
   public BufferedImage createImage(String text){
	   int width = 100;   
       int height = 40;
       BufferedImage image =new BufferedImage(width,height,BufferedImage.TYPE_INT_ARGB);
       Graphics2D g = image.createGraphics() ;  
       Font myFont = new Font("黑体" , Font.BOLD , 16) ; 
       g.setFont(myFont) ; 
       g.setColor(getRandomColor(200 , 250));  
       g.fillRect(0, 0, 100 , 40) ;  
       drawRandomLines(g, 160,100,40) ;  
       drawString(text, g) ;  
	   return image;
   }
   public Color getRandomColor(int fc, int bc) {  
	   Random random = new Random();   
	   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);  
   }  
   public void drawRandomLines(Graphics2D g ,int nums,int width,int height){  
	     Random random = new Random();  
	   g.setColor(this.getRandomColor(160, 200)) ;  
       for(int i=0 ; i<nums ; i++){  
           int x1 = random.nextInt(width) ;  
           int y1 = random.nextInt(height);  
           int x2 = random.nextInt(12) ;  
           int y2 = random.nextInt(12) ;  
           g.drawLine(x1, y1, x2, y2) ;  
       }  
   } 
   public void drawString(String text,Graphics2D g){
	   Random random = new Random();  
	   Color color = new Color(20+random.nextInt(20) , 20+random.nextInt(20) ,20+random.nextInt(20) );  
       g.setColor(color) ; 
       AffineTransform trans = new AffineTransform();  
       trans.rotate(random.nextInt(45)*3.14/180, 23, 7) ;  
       //缩放文字  
       float scaleSize = random.nextFloat() + 0.8f ;  
       if(scaleSize>1f)  
           scaleSize = 1f ;  
       trans.scale(scaleSize, scaleSize) ;  
       g.setTransform(trans) ;  
       g.drawString(text, 33, 14) ;  
       g.dispose();
   }
   

技术分享

验证码

标签:java 验证码

原文地址:http://blog.csdn.net/majun_guang/article/details/45725519

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!