乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      登陸注冊使用的驗證碼(java生成)

       feimishiwo 2014-08-13
      1. package com.design.util;  
      2. import java.awt.Color;  
      3. import java.awt.Font;  
      4. import java.awt.Graphics;  
      5. import java.awt.image.BufferedImage;  
      6. import java.io.ByteArrayInputStream;  
      7. import java.io.ByteArrayOutputStream;  
      8. import java.util.Random;  
      9. import javax.imageio.ImageIO;  
      10. import javax.imageio.stream.ImageOutputStream;  
      11.   
      12. /** 
      13.  * 驗證碼類,主要生成幾種不同類型的驗證碼  
      14.  * 第一種:簡單驗證碼,4位隨機數(shù)字  
      15.  * 第二種:英文字符加數(shù)字的驗證碼  
      16.  * 第三種:像鐵路訂票系統(tǒng)一樣的驗證碼,肆+?=21 
      17.  *  
      18.  */  
      19. public class VerifyCode {  
      20.     private ByteArrayInputStream image;// 圖像  
      21.     private String str;// 驗證碼  
      22.     private static final int WIDTH = 80;  
      23.     private static final int HEIGHT = 20;  
      24.   
      25.     public static void main(String[] arg) {  
      26.         VerifyCode vcu = VerifyCode.Instance();  
      27.         System.err.println(vcu.getVerificationCodeValue());  
      28.     }  
      29.   
      30.     /** 
      31.      * 功能:獲取一個驗證碼類的實例 
      32.      *  
      33.      * @return 
      34.      */  
      35.     public static VerifyCode Instance() {  
      36.         return new VerifyCode();  
      37.     }  
      38.   
      39.     private VerifyCode() {  
      40.         BufferedImage image = new BufferedImage(WIDTH, HEIGHT,  
      41.                 BufferedImage.TYPE_INT_RGB);  
      42.         int randomNum = new Random().nextInt(3);  
      43.         if (randomNum == 0) {  
      44.             initNumVerificationCode(image);  
      45.         } else if (randomNum == 1) {  
      46.             initLetterAndNumVerificationCode(image);  
      47.         } else {  
      48.             initChinesePlusNumVerificationCode(image);  
      49.         }  
      50.     }  
      51.   
      52.     /** 
      53.      * 功能:設置第一種驗證碼的屬性 
      54.      */  
      55.     public void initNumVerificationCode(BufferedImage image) {  
      56.   
      57.         Random random = new Random(); // 生成隨機類  
      58.         Graphics g = initImage(image, random);  
      59.         String sRand = "";  
      60.         for (int i = 0; i < 4; i++) {  
      61.             String rand = String.valueOf(random.nextInt(10));  
      62.             sRand += rand;  
      63.             // 將認證碼顯示到圖象中  
      64.             g.setColor(new Color(20 + random.nextInt(110), 20 + random  
      65.                     .nextInt(110), 20 + random.nextInt(110)));  
      66.             // 調(diào)用函數(shù)出來的顏色相同,可能是因為種子太接近,所以只能直接生成  
      67.             g.drawString(rand, 13 * i + 6, 16);  
      68.         }  
      69.         this.setStr(sRand);/* 賦值驗證碼 */  
      70.         // 圖象生效  
      71.         g.dispose();  
      72.         this.setImage(drawImage(image));  
      73.     }  
      74.   
      75.     /** 
      76.      * 功能:設置第二種驗證碼屬性 
      77.      */  
      78.     public void initLetterAndNumVerificationCode(BufferedImage image) {  
      79.   
      80.         Random random = new Random(); // 生成隨機類  
      81.         Graphics g = initImage(image, random);  
      82.         String[] letter = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J",  
      83.                 "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V",  
      84.                 "W", "X", "Y", "Z" };  
      85.         String sRand = "";  
      86.         for (int i = 0; i < 4; i++) {  
      87.             String tempRand = "";  
      88.             if (random.nextBoolean()) {  
      89.                 tempRand = String.valueOf(random.nextInt(10));  
      90.             } else {  
      91.                 tempRand = letter[random.nextInt(25)];  
      92.                 if (random.nextBoolean()) {// 隨機將該字母變成小寫  
      93.                     tempRand = tempRand.toLowerCase();  
      94.                 }  
      95.             }  
      96.             sRand += tempRand;  
      97.             g.setColor(new Color(20 + random.nextInt(10), 20 + random  
      98.                     .nextInt(110), 20 + random.nextInt(110)));  
      99.             g.drawString(tempRand, 13 * i + 6, 16);  
      100.         }  
      101.         this.setStr(sRand);/* 賦值驗證碼 */  
      102.         g.dispose(); // 圖象生效  
      103.         this.setImage(drawImage(image));  
      104.     }  
      105.   
      106.     /** 
      107.      * 功能:設置第三種驗證碼屬性 即:肆+?=24 
      108.      */  
      109.     public void initChinesePlusNumVerificationCode(BufferedImage image) {  
      110.         String[] cn = { "壹", "貳", "叁", "肆", "伍", "陸", "柒", "捌", "玖", "拾" };  
      111.         Random random = new Random(); // 生成隨機類  
      112.         Graphics g = initImage(image, random);  
      113.         int x = random.nextInt(10) + 1;  
      114.         int y = random.nextInt(30);  
      115.         this.setStr(String.valueOf(y));  
      116.         g.setFont(new Font("楷體", Font.PLAIN, 14));// 設定字體  
      117.         g.setColor(new Color(20 + random.nextInt(10), 20 + random.nextInt(110),  
      118.                 20 + random.nextInt(110)));  
      119.         g.drawString(cn[x - 1], 1 * 1 + 6, 16);  
      120.         g.drawString("+", 22, 16);  
      121.         g.drawString("?", 35, 16);  
      122.         g.drawString("=", 48, 16);  
      123.         g.drawString(String.valueOf(x + y), 61, 16);  
      124.         g.dispose(); // 圖象生效  
      125.         this.setImage(drawImage(image));  
      126.   
      127.     }  
      128.   
      129.     public Graphics initImage(BufferedImage image, Random random) {  
      130.         Graphics g = image.getGraphics(); // 獲取圖形上下文  
      131.         g.setColor(getRandColor(200, 250));// 設定背景色  
      132.         g.fillRect(0, 0, WIDTH, HEIGHT);  
      133.         g.setFont(new Font("Times New Roman", Font.PLAIN, 14));// 設定字體  
      134.         g.setColor(getRandColor(160, 200)); // 隨機產(chǎn)生165條干擾線,使圖象中的認證碼不易被其它程序探測到  
      135.         for (int i = 0; i < 165; i++) {  
      136.             int x = random.nextInt(WIDTH);  
      137.             int y = random.nextInt(HEIGHT);  
      138.             int xl = random.nextInt(12);  
      139.             int yl = random.nextInt(12);  
      140.             g.drawLine(x, y, x + xl, y + yl);  
      141.         }  
      142.         return g;  
      143.     }  
      144.   
      145.     public ByteArrayInputStream drawImage(BufferedImage image) {  
      146.         ByteArrayInputStream input = null;  
      147.         ByteArrayOutputStream output = new ByteArrayOutputStream();  
      148.         try {  
      149.             ImageOutputStream imageOut = ImageIO  
      150.                     .createImageOutputStream(output);  
      151.             ImageIO.write(image, "JPEG", imageOut);  
      152.             imageOut.close();  
      153.             input = new ByteArrayInputStream(output.toByteArray());  
      154.         } catch (Exception e) {  
      155.             System.out.println("驗證碼圖片產(chǎn)生出現(xiàn)錯誤:" + e.toString());  
      156.         }  
      157.         return input;  
      158.     }  
      159.   
      160.     /* 
      161.      * 功能:給定范圍獲得隨機顏色 
      162.      */  
      163.     private Color getRandColor(int fc, int bc) {  
      164.         Random random = new Random();  
      165.         if (fc > 255)  
      166.             fc = 255;  
      167.         if (bc > 255)  
      168.             bc = 255;  
      169.         int r = fc + random.nextInt(bc - fc);  
      170.         int g = fc + random.nextInt(bc - fc);  
      171.         int b = fc + random.nextInt(bc - fc);  
      172.         return new Color(r, g, b);  
      173.     }  
      174.   
      175.     /** 
      176.      * 功能:獲取驗證碼的字符串值 
      177.      *  
      178.      * @return 
      179.      */  
      180.     public String getVerificationCodeValue() {  
      181.         return this.getStr();  
      182.     }  
      183.   
      184.     /** 
      185.      * 功能:取得驗證碼圖片 
      186.      *  
      187.      * @return 
      188.      */  
      189.     public ByteArrayInputStream getImage() {  
      190.         return this.image;  
      191.     }  
      192.   
      193.     public String getStr() {  
      194.         return str;  
      195.     }  
      196.   
      197.     public void setStr(String str) {  
      198.         this.str = str;  
      199.     }  
      200.   
      201.     public void setImage(ByteArrayInputStream image) {  
      202.         this.image = image;  
      203.     }  
      204. }  

      2.然后是將驗證碼發(fā)送到客戶端的Action代碼:

      1. package com.design.action;  
      2.   
      3. import java.io.InputStream;  
      4.   
      5. import org.apache.struts2.convention.annotation.Result;  
      6.   
      7. import com.design.util.VerifyCode;  
      8. import com.opensymphony.xwork2.ActionContext;  
      9. @Result(name="success",  
      10. type="stream",  
      11. params={"inputName","inputStream"})  
      12. public class VerifycodeAction extends ActionSupport{  
      13.       
      14.     private InputStream inputStream;  
      15.       
      16.     public String execute(){  
      17.         VerifyCode verifyCode= VerifyCode.Instance();  
      18.         this.inputStream = verifyCode.getImage();  
      19.         ActionContext.getContext().getSession().put("yzm", verifyCode.getVerificationCodeValue());  
      20.         return SUCCESS;  
      21.           
      22.     }  
      23.   
      24.     public InputStream getInputStream() {  
      25.         return inputStream;  
      26.     }  
      27.   
      28.     public void setInputStream(InputStream inputStream) {  
      29.         this.inputStream = inputStream;  
      30.     }  
      31. }  

      3.然后在Jsp頁面里面使用<img/>標簽就可以看到驗證碼了,并可以使用Js實現(xiàn)更換,代碼如下:

      1. <script type="text/javascript">       
      2.                 function changeValidateCode(obj) {  
      3.                     obj.src="verifycode";  
      4.                 }  
      5.         </script>  
      6.         <tr>  
      7.             <th>驗證碼:</th>  
      8.             <td valign="top"><input type="text" onfocus="this.value=''" class="login-inp" name="yzm" value="輸入以下圖片提示的驗證碼"/></td>  
      9.         </tr>  
      10.         <tr>  
      11.             <th>驗證碼圖片:</th>  
      12.             <td valign="top"><img src="verifycode.action" width="80" height="30" onclick="changeValidateCode(this)"/><span>點擊圖片換驗證碼</span></td>  
      13.         </tr>  



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

        0條評論

        發(fā)表

        請遵守用戶 評論公約

        類似文章 更多