小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

JSF實現驗證碼

 小韋 2010-03-18
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. <a4j:mediaOutput element="img" cacheable="flase" session="false"     
  2.                    rendered="true" createContent="#{validateCodeImageData.paint}"     
  3.                   value="#{validateCodeImageData}" mimeType="image/jpeg">  
  4.  </a4j:mediaOutput>   

 

Xml代碼 復制代碼
  1. <managed-bean>      
  2.             <managed-bean-name>validateCodeImageData</managed-bean-name>      
  3.             <managed-bean-class>      
  4.                        com.jxh.common.view.ImageData      
  5.             </managed-bean-class>      
  6.             <managed-bean-scope>request</managed-bean-scope>      
  7.   </managed-bean>     

    本站是提供個人知識管理的網絡存儲空間,所有內容均由用戶發(fā)布,不代表本站觀點。請注意甄別內容中的聯系方式、誘導購買等信息,謹防詐騙。如發(fā)現有害或侵權內容,請點擊一鍵舉報。
    轉藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約