java图片验证码_自己封装的一个java图片验证码

1 packagecom.lz.Tools;2

3 importjava.awt.Color;4 importjava.awt.Font;5 importjava.awt.Graphics;6 importjava.awt.Graphics2D;7 importjava.awt.image.BufferedImage;8 importjava.util.Random;9

10 /**

11 * 验证码生成器12 *13 *@authorbojiangzhou14 */

15 public classVCodeGenerator {16

17 /**

18 * 验证码来源19 */

20 final private char[] code ={21 '2', '3', '4', '5', '6', '7', '8', '9',22 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',23 'k', 'm', 'n', 'p', 'q', 'r', 's', 't', 'u', 'v',24 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F',25 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R',26 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'

27 };28 /**

29 * 字体30 */

31 final private String[] fontNames = newString[]{32 "黑体", "宋体", "Courier", "Arial",33 "Verdana", "Times", "Tahoma", "Georgia"};34 /**

35 * 字体样式36 */

37 final private int[] fontStyles = new int[]{38 Font.BOLD, Font.ITALIC|Font.BOLD39 };40

41 /**

42 * 验证码长度43 * 默认4个字符44 */

45 private int vcodeLen = 4;46 /**

47 * 验证码图片字体大小48 * 默认1749 */

50 private int fontsize = 21;51 /**

52 * 验证码图片宽度53 */

54 private int width = (fontsize+1)*vcodeLen+10;55 /**

56 * 验证码图片高度57 */

58 private int height = fontsize+12;59 /**

60 * 干扰线条数61 * 默认3条62 */

63 private int disturbline = 3;64

65

66 publicVCodeGenerator(){}67

68 /**

69 * 指定验证码长度70 *@paramvcodeLen 验证码长度71 */

72 public VCodeGenerator(intvcodeLen) {73 this.vcodeLen =vcodeLen;74 this.width = (fontsize+1)*vcodeLen+10;75 }76

77 /**

78 * 生成验证码图片79 *@paramvcode 要画的验证码80 *@paramdrawline 是否画干扰线81 *@return

82 */

83 public BufferedImage generatorVCodeImage(String vcode, booleandrawline){84 //创建验证码图片

85 BufferedImage vcodeImage = newBufferedImage(width, height, BufferedImage.TYPE_INT_RGB);86 Graphics g =vcodeImage.getGraphics();87 //填充背景色

88 g.setColor(new Color(246, 240, 250));89 g.fillRect(0, 0, width, height);90 if(drawline){91 drawDisturbLine(g);92 }93 //用于生成伪随机数

94 Random ran = newRandom();95 //在图片上画验证码

96 for(int i = 0;i < vcode.length();i++){97 //设置字体

98 g.setFont(newFont(fontNames[ran.nextInt(fontNames.length)], fontStyles[ran.nextInt(fontStyles.length)], fontsize));99 //随机生成颜色

100 g.setColor(getRandomColor());101 //画验证码

102 g.drawString(vcode.charAt(i)+"", i*fontsize+10, fontsize+5);103 }104 //释放此图形的上下文以及它使用的所有系统资源

105 g.dispose();106

107 returnvcodeImage;108 }109 /**

110 * 获得旋转字体的验证码图片111 *@paramvcode112 *@paramdrawline 是否画干扰线113 *@return

114 */

115 public BufferedImage generatorRotateVCodeImage(String vcode, booleandrawline){116 //创建验证码图片

117 BufferedImage rotateVcodeImage = newBufferedImage(width, height, BufferedImage.TYPE_INT_RGB);118 Graphics2D g2d =rotateVcodeImage.createGraphics();119 //填充背景色

120 g2d.setColor(new Color(246, 240, 250));121 g2d.fillRect(0, 0, width, height);122 if(drawline){123 drawDisturbLine(g2d);124 }125 //在图片上画验证码

126 for(int i = 0;i < vcode.length();i++){127 BufferedImage rotateImage =getRotateImage(vcode.charAt(i));128 g2d.drawImage(rotateImage, null, (int) (this.height * 0.7) * i, 0);129 }130 g2d.dispose();131 returnrotateVcodeImage;132 }133 /**

134 * 生成验证码135 *@return验证码136 */

137 publicString generatorVCode(){138 int len =code.length;139 Random ran = newRandom();140 StringBuffer sb = newStringBuffer();141 for(int i = 0;i < vcodeLen;i++){142 int index =ran.nextInt(len);143 sb.append(code[index]);144 }145 returnsb.toString();146 }147 /**

148 * 为验证码图片画一些干扰线149 *@paramg150 */

151 private voiddrawDisturbLine(Graphics g){152 Random ran = newRandom();153 for(int i = 0;i < disturbline;i++){154 int x1 =ran.nextInt(width);155 int y1 =ran.nextInt(height);156 int x2 =ran.nextInt(width);157 int y2 =ran.nextInt(height);158 g.setColor(getRandomColor());159 //画干扰线

160 g.drawLine(x1, y1, x2, y2);161 }162 }163 /**

164 * 获取一张旋转的图片165 *@paramc 要画的字符166 *@return

167 */

168 private BufferedImage getRotateImage(charc){169 BufferedImage rotateImage = newBufferedImage(height, height, BufferedImage.TYPE_INT_ARGB);170 Graphics2D g2d =rotateImage.createGraphics();171 //设置透明度为0

172 g2d.setColor(new Color(255, 255, 255, 0));173 g2d.fillRect(0, 0, height, height);174 Random ran = newRandom();175 g2d.setFont(newFont(fontNames[ran.nextInt(fontNames.length)], fontStyles[ran.nextInt(fontStyles.length)], fontsize));176 g2d.setColor(getRandomColor());177 double theta =getTheta();178 //旋转图片

179 g2d.rotate(theta, height/2, height/2);180 g2d.drawString(Character.toString(c), (height-fontsize)/2, fontsize+5);181 g2d.dispose();182

183 returnrotateImage;184 }185 /**

186 *@return返回一个随机颜色187 */

188 privateColor getRandomColor(){189 Random ran = newRandom();190 return new Color(ran.nextInt(220), ran.nextInt(220), ran.nextInt(220));191 }192 /**

193 *@return角度194 */

195 private doublegetTheta(){196 return ((int) (Math.random()*1000) % 2 == 0 ? -1 : 1)*Math.random();197 }198

199 /**

200 *@return验证码字符个数201 */

202 public intgetVcodeLen() {203 returnvcodeLen;204 }205 /**

206 * 设置验证码字符个数207 *@paramvcodeLen208 */

209 public void setVcodeLen(intvcodeLen) {210 this.width = (fontsize+3)*vcodeLen+10;211 this.vcodeLen =vcodeLen;212 }213 /**

214 *@return字体大小215 */

216 public intgetFontsize() {217 returnfontsize;218 }219 /**

220 * 设置字体大小221 *@paramfontsize222 */

223 public void setFontsize(intfontsize) {224 this.width = (fontsize+3)*vcodeLen+10;225 this.height = fontsize+15;226 this.fontsize =fontsize;227 }228 /**

229 *@return图片宽度230 */

231 public intgetWidth() {232 returnwidth;233 }234 /**

235 * 设置图片宽度236 *@paramwidth237 */

238 public void setWidth(intwidth) {239 this.width =width;240 }241 /**

242 *@return图片高度243 */

244 public intgetHeight() {245 returnheight;246 }247 /**

248 * 设置图片高度249 *@paramheight250 */

251 public void setHeight(intheight) {252 this.height =height;253 }254 /**

255 *@return干扰线条数256 */

257 public intgetDisturbline() {258 returndisturbline;259 }260 /**

261 * 设置干扰线条数262 *@paramdisturbline263 */

264 public void setDisturbline(intdisturbline) {265 this.disturbline =disturbline;266 }267

268 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值