JSF实现验证码

来源:百度文库 编辑:神马文学网 时间:2024/04/28 13:48:35
Java代码
  1. public class ImageData implements Serializable {   
  2.   
  3.     private static final long serialVersionUID = -8161565799237268271L;   
  4.     private BufferedImage image;   
  5.     Integer width = 60;   
  6.     Integer height = 20;   
  7.     Color bankgroud = new Color(0xFFFFFF);   
  8.     Color drawColor = new Color(0x000000);   
  9.    Font textFont = new Font("Comic Sans ms", Font.PLAIN, 18);   
  10.    String str = "0,a,2,x,e,5,j,7,l,9,1,b,c,d,4,f,g,h,i,6,k,8,m,n,o,p,q,r,s,t,u,v,w,3,y,z";     
  11.     Random random = new Random();   
  12.     public void paint(OutputStream out, Object data) throws IOException {   
  13.         if (data instanceof ImageData) {   
  14.             // create the image   
  15.             image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);   
  16.             Graphics graphics = image.getGraphics();   
  17.             graphics.setColor(bankgroud);   
  18.             graphics.fillRect(00, width, height);    
  19.             graphics.setColor(drawColor);   
  20.             graphics.drawRect(00, width - 1, height - 1);   
  21.             graphics.setFont(textFont);   
  22.             //绘制干扰线   
  23.             for (int i = 0; i < 160; i++) {   
  24.                 int x = random.nextInt(width);   
  25.                 int y = random.nextInt(height);   
  26.                 int xl = random.nextInt(12);   
  27.                 int yl = random.nextInt(12);   
  28.                 graphics.setColor(getRandColor(100,250));   
  29.                 graphics.drawLine(x, y, x + xl, y + yl);   
  30.             }   
  31.                        //绘制验证码   
  32.             graphics.setColor(drawColor);   
  33.             String code="" ;   
  34.             String[] array = str.split(",");   
  35.             for (int i = 0; i < 4; i++) {                           
  36.                 String rand = array[random.nextInt(36)];   
  37.                 code += rand;      
  38.                 graphics.drawString(rand, 13 * i + 616);   
  39.             }      
  40.                   FacesUtil.getSession().setAttribute("code", code);   
  41.             graphics.dispose();   
  42.         }   
  43.         ImageIO.write(image, "JPEG", out);   
  44.     }      
  45.   
  46.     private Color getRandColor(int fc, int bc) {   
  47.         Random random = new Random();   
  48.         if (fc > 255)   
  49.             fc = 255;   
  50.         if (bc > 255)   
  51.             bc = 255;   
  52.         int r = fc + random.nextInt(bc - fc);   
  53.         int g = fc + random.nextInt(bc - fc);   
  54.         int b = fc + random.nextInt(bc - fc);   
  55.         return new Color(r, g, b);   
  56.     }   
  57.   
  58. }  

 

Html代码
  1.  element="img" cacheable="flase" session="false"     
  2.                    rendered="true" createContent="#{validateCodeImageData.paint}"     
  3.                   value="#{validateCodeImageData}" mimeType="image/jpeg">  
  4.     

 

Xml代码
  1.       
  2.             validateCodeImageData      
  3.                   
  4.                        com.jxh.common.view.ImageData      
  5.                   
  6.             request      
  7.