分享

登陆注册使用的验证码(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位随机数字  
  15.  * 第二种:英文字符加数字的验证码  
  16.  * 第三种:像铁路订票系统一样的验证码,肆+?=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.             // 调用函数出来的颜色相同,可能是因为种子太接近,所以只能直接生成  
  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)); // 随机产生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("验证码图片产生出现错误:" + 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.然后是将验证码发送到客户端的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实现更换,代码如下:

  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>  



    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多