1. 程式人生 > >java實現驗證碼圖片

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 }