日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

登陸注冊(cè)使用的驗(yàn)證碼(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.  * 驗(yàn)證碼類,主要生成幾種不同類型的驗(yàn)證碼  
  14.  * 第一種:簡(jiǎn)單驗(yàn)證碼,4位隨機(jī)數(shù)字  
  15.  * 第二種:英文字符加數(shù)字的驗(yàn)證碼  
  16.  * 第三種:像鐵路訂票系統(tǒng)一樣的驗(yàn)證碼,肆+?=21 
  17.  *  
  18.  */  
  19. public class VerifyCode {  
  20.     private ByteArrayInputStream image;// 圖像  
  21.     private String str;// 驗(yàn)證碼  
  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.      * 功能:獲取一個(gè)驗(yàn)證碼類的實(shí)例 
  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.      * 功能:設(shè)置第一種驗(yàn)證碼的屬性 
  54.      */  
  55.     public void initNumVerificationCode(BufferedImage image) {  
  56.   
  57.         Random random = new Random(); // 生成隨機(jī)類  
  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.             // 將認(rèn)證碼顯示到圖象中  
  64.             g.setColor(new Color(20 + random.nextInt(110), 20 + random  
  65.                     .nextInt(110), 20 + random.nextInt(110)));  
  66.             // 調(diào)用函數(shù)出來(lái)的顏色相同,可能是因?yàn)榉N子太接近,所以只能直接生成  
  67.             g.drawString(rand, 13 * i + 6, 16);  
  68.         }  
  69.         this.setStr(sRand);/* 賦值驗(yàn)證碼 */  
  70.         // 圖象生效  
  71.         g.dispose();  
  72.         this.setImage(drawImage(image));  
  73.     }  
  74.   
  75.     /** 
  76.      * 功能:設(shè)置第二種驗(yàn)證碼屬性 
  77.      */  
  78.     public void initLetterAndNumVerificationCode(BufferedImage image) {  
  79.   
  80.         Random random = new Random(); // 生成隨機(jī)類  
  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()) {// 隨機(jī)將該字母變成小寫  
  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);/* 賦值驗(yàn)證碼 */  
  102.         g.dispose(); // 圖象生效  
  103.         this.setImage(drawImage(image));  
  104.     }  
  105.   
  106.     /** 
  107.      * 功能:設(shè)置第三種驗(yàn)證碼屬性 即:肆+?=24 
  108.      */  
  109.     public void initChinesePlusNumVerificationCode(BufferedImage image) {  
  110.         String[] cn = { "壹", "貳", "叁", "肆", "伍", "陸", "柒", "捌", "玖", "拾" };  
  111.         Random random = new Random(); // 生成隨機(jī)類  
  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));// 設(shè)定字體  
  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));// 設(shè)定背景色  
  132.         g.fillRect(0, 0, WIDTH, HEIGHT);  
  133.         g.setFont(new Font("Times New Roman", Font.PLAIN, 14));// 設(shè)定字體  
  134.         g.setColor(getRandColor(160, 200)); // 隨機(jī)產(chǎn)生165條干擾線,使圖象中的認(rèn)證碼不易被其它程序探測(cè)到  
  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("驗(yàn)證碼圖片產(chǎn)生出現(xiàn)錯(cuò)誤:" + e.toString());  
  156.         }  
  157.         return input;  
  158.     }  
  159.   
  160.     /* 
  161.      * 功能:給定范圍獲得隨機(jī)顏色 
  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.      * 功能:獲取驗(yàn)證碼的字符串值 
  177.      *  
  178.      * @return 
  179.      */  
  180.     public String getVerificationCodeValue() {  
  181.         return this.getStr();  
  182.     }  
  183.   
  184.     /** 
  185.      * 功能:取得驗(yàn)證碼圖片 
  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.然后是將驗(yàn)證碼發(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頁(yè)面里面使用<img/>標(biāo)簽就可以看到驗(yàn)證碼了,并可以使用Js實(shí)現(xiàn)更換,代碼如下:

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



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

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多