100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > java实现验证码图片_java实现验证码图片

java实现验证码图片_java实现验证码图片

时间:2018-08-02 04:49:48

相关推荐

java实现验证码图片_java实现验证码图片

1 packagesip.utils;2 importjava.awt.Color;3 importjava.awt.Graphics2D;4 importjava.awt.geom.AffineTransform;5 importjava.util.Random;6

7 /**

8 * 验证码图片生成器9 *10 *@authorWuZhengFei11 *12 */

13 public classIdentityCode {14 /**

15 * 验证码图片的宽度。16 */

17 private int width = 80;18 /**

19 * 验证码图片的高度。20 */

21 private int height = 23;22 /**

23 * 验证码的数量。24 */

25 private Random random = newRandom();26

27 publicIdentityCode(){}28 /**

29 * 生成随机颜色30 *@paramfc 前景色31 *@parambc 背景色32 *@returnColor对象,此Color对象是RGB形式的。33 */

34 public Color getRandomColor(int fc, intbc) {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 newColor(r, g, b);43 }44

45 /**

46 * 绘制干扰线47 *@paramg Graphics2D对象,用来绘制图像48 *@paramnums 干扰线的条数49 */

50 public void drawRandomLines(Graphics2D g ,intnums ){51 g.setColor(this.getRandomColor(160, 200)) ;52 for(int i=0 ; i

61 /**

62 * 获取随机字符串,63 * 此函数可以产生由大小写字母,汉字,数字组成的字符串64 *@paramlength 随机字符串的长度65 *@return随机字符串66 */

67 public String drawRandomString(intlength , Graphics2D g){68 StringBuffer strbuf = newStringBuffer() ;69 String temp = "";70 //int itmp = 0 ;

71 for(int i=0 ; i

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 = newAffineTransform();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 returnstrbuf.toString() ;134 }135 public intgetWidth() {136 returnwidth;137 }138 public void setWidth(intwidth) {139 this.width =width;140 }141 public intgetHeight() {142 returnheight;143 }144 public void setHeight(intheight) {145 this.height =height;146 }147 public static voidmain(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 = newIdentityCode();156 BufferedImage image =newBufferedImage(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 }

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。