java实现验证码图片

  1 package sip.utils;
  2 import java.awt.Color;  
  3 import java.awt.Graphics2D;  
  4 import java.awt.geom.AffineTransform;  
  5 import java.util.Random;  
  6   
  7 /** 
  8  * 验证码图片生成器 
  9  *  
 10  * @author WuZhengFei 
 11  *  
 12  */  
 13 public class IdentityCode {  
 14     /** 
 15      * 验证码图片的宽度。 
 16      */  
 17     private int width = 80;  
 18     /** 
 19      * 验证码图片的高度。 
 20      */  
 21     private int height = 23;  
 22     /** 
 23      * 验证码的数量。 
 24      */  
 25     private Random random = new Random();  
 26       
 27     public IdentityCode(){}  
 28     /** 
 29      * 生成随机颜色 
 30      * @param fc    前景色 
 31      * @param bc    背景色 
 32      * @return  Color对象,此Color对象是RGB形式的。 
 33      */  
 34     public Color getRandomColor(int fc, int bc) {  
 35         if (fc > 255)  
 36             fc = 200;  
 37         if (bc > 255)  
 38             bc = 255;  
 39         int r = fc + random.nextInt(bc - fc);  
 40         int g = fc + random.nextInt(bc - fc);  
 41         int b = fc + random.nextInt(bc - fc);  
 42         return new Color(r, g, b);  
 43     }  
 44       
 45     /** 
 46      * 绘制干扰线 
 47      * @param g Graphics2D对象,用来绘制图像 
 48      * @param nums  干扰线的条数 
 49      */  
 50     public void drawRandomLines(Graphics2D g ,int nums ){  
 51         g.setColor(this.getRandomColor(160, 200)) ;  
 52         for(int i=0 ; i<nums ; i++){  
 53             int x1 = random.nextInt(width) ;  
 54             int y1 = random.nextInt(height);  
 55             int x2 = random.nextInt(12) ;  
 56             int y2 = random.nextInt(12) ;  
 57             g.drawLine(x1, y1, x2, y2) ;  
 58         }  
 59     }  
 60       
 61     /** 
 62      * 获取随机字符串, 
 63      *      此函数可以产生由大小写字母,汉字,数字组成的字符串 
 64      * @param length    随机字符串的长度 
 65      * @return  随机字符串 
 66      */  
 67     public String drawRandomString(int length , Graphics2D g){  
 68         StringBuffer strbuf = new StringBuffer() ;  
 69         String temp = "" ;  
 70        // int itmp = 0 ;  
 71         for(int i=0 ; i<length ; i++){  
 72             temp = random.nextInt(length)+"";
 73             /*switch(random.nextInt(5)){  
 74             case 1:     //生成A~Z的字母  
 75                 itmp = random.nextInt(26) + 65 ;  
 76                 temp = String.valueOf((char)itmp);  
 77                 break;  
 78             case 2:  
 79                 itmp = random.nextInt(26) + 97 ;  
 80                 temp = String.valueOf((char)itmp);  
 81             case 3:     //生成汉字  
 82                 String[] rBase = {"0" , "1" , "2" , "3" , "4" , "5" , "6" , "7" ,   
 83                         "8" , "9" , "a" , "b" , "c" , "d" , "e" , "f" };  
 84                 int r1 = random.nextInt(3)+11 ;     //生成第1位的区码  
 85                 String strR1 = rBase[r1] ;      //生成11~14的随机数  
 86                 int r2 ;        //生成第2位的区码  
 87                 if(r1 == 13)  
 88                     r2 = random.nextInt(7) ;    //生成0~7的随机数  
 89                 else  
 90                     r2 = random.nextInt(16) ;   //生成0~16的随机数  
 91                 String strR2 = rBase[r2] ;  
 92                 int r3 = random.nextInt(6) + 10 ;   //生成第1位的位码  
 93                 String strR3 = rBase[r3] ;  
 94                 int r4 ;        //生成第2位的位码  
 95                 if(r3 == 10)  
 96                     r4 = random.nextInt(15) + 1;    //生成1~16的随机数  
 97                 else if(r3 == 15)  
 98                     r4 = random.nextInt(15) ;       //生成0~15的随机数  
 99                 else  
100                     r4 = random.nextInt(16) ;       //生成0~16的随机数  
101                 String strR4 = rBase[r4] ;  
102                 //将生成的机内码转换成数字  
103                 byte[] bytes = new byte[2]   ;        
104                 String strR12 = strR1 + strR2 ;     //将生成的区码保存到字节数组的第1个元素中  
105                 int tempLow = Integer.parseInt(strR12, 16) ;  
106                 bytes[0] = (byte)tempLow;  
107                 String strR34 = strR3 + strR4 ;     //将生成的区码保存到字节数组的第2个元素中  
108                 int tempHigh = Integer.parseInt(strR34, 16) ;  
109                 bytes[1] = (byte)tempHigh;  
110                 temp = new String(bytes);           //根据字节数组生成汉字  
111                 break;  
112             default:  
113                 itmp = random.nextInt(10) + 48 ;  
114                 temp = String.valueOf((char)itmp) ;  
115                 break;  
116             } */ 
117             Color color = new Color(20+random.nextInt(20) , 20+random.nextInt(20) ,20+random.nextInt(20) );  
118             g.setColor(color) ;  
119             //想文字旋转一定的角度  
120             AffineTransform trans = new AffineTransform();  
121             trans.rotate(random.nextInt(45)*3.14/180, 15*i+8, 7) ;  
122             //缩放文字  
123             float scaleSize = random.nextFloat() + 0.8f ;  
124             if(scaleSize>1f)  
125                 scaleSize = 1f ;  
126             trans.scale(scaleSize, scaleSize) ;  
127             g.setTransform(trans) ;  
128             g.drawString(temp, 15*i+18, 14) ;  
129               
130             strbuf.append(temp) ;  
131         }  
132         g.dispose() ;  
133         return strbuf.toString() ;  
134     }  
135     public int getWidth() {  
136         return width;  
137     }  
138     public void setWidth(int width) {  
139         this.width = width;  
140     }  
141     public int getHeight() {  
142         return height;  
143     }  
144     public void setHeight(int height) {  
145         this.height = height;  
146     }  
147     public static void main(String[] arg){
148             //生成验证码    
149             //设置不缓存图片  
150             response.setHeader("Pragma", "No-cache");  
151             response.setHeader("Cache-Control", "No-cache");  
152             response.setDateHeader("Expires", 0) ;  
153             //指定生成的相应图片  
154             response.setContentType("image/jpeg") ;  
155             IdentityCode idCode = new IdentityCode();  
156             BufferedImage image =new BufferedImage(idCode.getWidth() , idCode.getHeight() , BufferedImage.TYPE_INT_BGR) ;  
157             Graphics2D g = image.createGraphics() ;  
158             //定义字体样式  
159             Font myFont = new Font("黑体" , Font.BOLD , 16) ;  
160             //设置字体  
161             g.setFont(myFont) ;  
162               
163             g.setColor(idCode.getRandomColor(200 , 250)) ;  
164             //绘制背景  
165             g.fillRect(0, 0, idCode.getWidth() , idCode.getHeight()) ;  
166               
167             g.setColor(idCode.getRandomColor(180, 200)) ;  
168             idCode.drawRandomLines(g, 160) ;  
169             String randomString = idCode.drawRandomString(4, g) ;  
170             g.dispose() ;  
171             ImageIO.write(image, "JPEG", response.getOutputStream()) ;
172     }
173 }    

猜你喜欢

转载自www.cnblogs.com/gavinwu-blog/p/10088633.html