验证码

今天用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();
   }
   

大笑
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值