Java实现图片验证码源码
1 /* 2 3 * To change this template, choose Tools | Templates * and open the template in the editor. 4 5 */ 6 7 package com.wind.util; 8 9 import java.awt.BasicStroke; 10 11 import java.awt.Color; import java.awt.Font; 12 13 import java.awt.Graphics; import java.awt.Graphics2D; 14 15 import java.awt.geom.AffineTransform; 16 17 import java.awt.geom.Line2D; 18 19 import java.awt.image.BufferedImage; 20 21 import java.io.ByteArrayInputStream; 22 23 import java.io.ByteArrayOutputStream; 24 25 import java.io.IOException; 26 27 import java.util.Random; 28 29 import javax.imageio.ImageIO; 30 31 import javax.imageio.stream.ImageOutputStream; 32 33 /** 34 35 * 36 37 * @author zyong 38 39 *验证码 40 41 */ 42 43 public class CheckCode { 44 45 /** 46 47 *创建一个随机数对象 48 49 */ 50 51 Random random = new Random(); 52 53 /** 54 55 *生成的字符集 56 57 */ 58 59 private final String character = "0ABC1DEF2GHI3JKL4MNO5PQR6ST7UV8WX9YZ"; 60 61 /** 62 63 *返回生成后的图片字符 64 65 */ 66 67 private String checkCode; 68 69 /** 70 71 * 设置生成图片的宽度,默认为65 72 73 */ 74 75 private int width = 65; 76 77 /** 78 79 * 设置生成图片的高度,默认为25 80 81 */ 82 83 private int height = 25; 84 85 /** 86 87 * 设置图片的类型,默认为BufferedImage.TYPE_INT_RGB 88 89 */ 90 91 private int imageType = BufferedImage.TYPE_INT_RGB; 92 93 /** 94 95 * 96 97 * @return生成图片后的字符 98 99 */ 100 101 public String getCheckCode() { 102 103 return checkCode; 104 105 } 106 107 /** 108 109 * 110 111 * @return图片的高度 112 113 */ 114 115 public int getHeight() { 116 117 return height; 118 119 } 120 121 /** 122 123 * 124 125 * @param height图片的高度 126 127 */ 128 129 public void setHeight(int height) { 130 131 this.height = height; 132 133 } 134 135 /** 136 137 * 138 139 * @return图片的宽度 140 141 */ 142 143 public int getWidth() { 144 145 return width; 146 147 } 148 149 /** 150 151 * 152 153 * @param width图片的宽度 154 155 */ 156 157 public void setWidth(int width) { 158 159 this.width = width; 160 161 } 162 163 /** 164 165 * 166 167 * @return图片类型 168 169 */ 170 171 public int getImageType() { 172 173 return imageType; 174 175 } 176 177 /** 178 179 * 180 181 * @param imageType图片类型 182 183 */ 184 185 public void setImageType(int imageType) { 186 187 this.imageType = imageType; 188 189 } 190 191 /** 192 193 * 194 195 * @return生成一张图片 196 197 */ 198 199 public ByteArrayInputStream buildImage() { 200 201 BufferedImage image = new BufferedImage(this.width, this.height, this.imageType); 202 203 Graphics g = image.getGraphics(); 204 205 Graphics2D g2d = (Graphics2D) g; 206 207 g.setColor(this.getColor(200, 250)); 208 209 g.fillRect(0, 0, this.width, this.height); 210 211 g.setFont(new Font("Times New Roman", Font.BOLD, 17)); 212 213 g.setColor(this.getColor(180, 200)); 214 215 /* 216 217 *绘制100条干扰线 218 219 */ 220 221 for (int i = 0; i < 100; i++) { 222 223 int x1 = random.nextInt(this.width); 224 225 int y1 = random.nextInt(this.height); 226 227 int x2 = random.nextInt(this.width - 3); 228 229 int y2 = random.nextInt(this.height - 3); 230 231 BasicStroke bs = new BasicStroke(2f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL); 232 233 Line2D line = new Line2D.Double(x1, y1, x2, y2); 234 235 g2d.setStroke(bs); 236 237 g2d.draw(line); 238 239 g.setColor(getColor(180, 222)); 240 241 } 242 243 StringBuffer codeStr = new StringBuffer(); 244 245 for (int i = 0; i < 4; i++) { 246 247 char c = character.charAt(random.nextInt(36)); 248 249 codeStr.append(c); 250 251 Color color = new Color(20 + random.nextInt(110), 20 + random.nextInt(110), 20 + random.nextInt(110)); 252 253 g.setColor(color); /*随机文字,旋转文字到指定角度*/ 254 255 AffineTransform trans = new AffineTransform(); 256 257 trans.rotate(random.nextInt(10) * i + 3, 5); 258 259 float scaleSize = random.nextFloat() + 0.5f; 260 261 if (scaleSize < 0.8 || scaleSize > 1.1f) { 262 263 scaleSize = 1f; 264 265 } 266 267 trans.scale(scaleSize, scaleSize); g2d.setTransform(trans); 268 269 g.drawString(String.valueOf(c), 15 * i + 6, 9); 270 271 } 272 273 this.checkCode = codeStr.toString(); g.dispose(); 274 275 ByteArrayInputStream inputStream = null; 276 277 ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); 278 279 try { 280 281 ImageOutputStream imgOutput = ImageIO.createImageOutputStream(outputStream); 282 283 ImageIO.write(image, "JPEG", imgOutput); 284 285 imgOutput.close(); 286 287 inputStream = new ByteArrayInputStream(outputStream.toByteArray()); 288 289 outputStream.close(); 290 291 } catch (IOException e) { 292 293 e.printStackTrace(); 294 295 } 296 297 return inputStream; 298 299 } 300 301 /** 302 303 * 利用随机数,随机生成一个Color颜色的对象 304 305 * @param fc 306 307 * @param bc 308 309 * @return颜色对象 310 311 */ 312 313 private Color getColor(int fc, int bc) { 314 315 if (fc > 255) { 316 317 fc = 255; 318 319 } 320 321 if (bc > 255) { 322 323 bc = 255; 324 325 } 326 327 int r = fc + random.nextInt(bc - fc); 328 329 int g = fc + random.nextInt(bc - fc); 330 331 int b = fc + random.nextInt(bc - fc); 332 333 return new Color(r, g, b); 334 335 } 336 337 }
文章来自: 好喜爱学习网(http://www.haoxiai.net) 网址:http://www.haoxiai.net/bianchengyuyan/javajiaocheng/141822.html