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 }

实现验证码功能可以使用Java的AWT和BufferedImage类来生成图像验证码。以下是一个简单的实现示例: ```java import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.image.BufferedImage; import java.util.Random; public class CaptchaGenerator { private static final int WIDTH = 120; // 图片宽度 private static final int HEIGHT = 40; // 图片高度 private static final int CODE_LENGTH = 4; // 验证码长度 private static final String CODES = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; // 验证码字符集 private static final Random random = new Random(); public static BufferedImage generate() { // 创建图像对象 BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB); Graphics g = image.getGraphics(); // 填充背景色 g.setColor(Color.WHITE); g.fillRect(0, 0, WIDTH, HEIGHT); // 生成随机验证码 StringBuilder code = new StringBuilder(); for (int i = 0; i < CODE_LENGTH; i++) { char c = CODES.charAt(random.nextInt(CODES.length())); code.append(c); } // 绘制验证码 g.setColor(Color.BLACK); g.setFont(new Font("Arial", Font.BOLD, 20)); for (int i = 0; i < CODE_LENGTH; i++) { char c = code.charAt(i); g.drawString(String.valueOf(c), 20 + i * 25, 25); } // 添加干扰线 g.setColor(Color.GRAY); for (int i = 0; i < 5; i++) { int x1 = random.nextInt(WIDTH); int y1 = random.nextInt(HEIGHT); int x2 = random.nextInt(WIDTH); int y2 = random.nextInt(HEIGHT); g.drawLine(x1, y1, x2, y2); } // 释放资源 g.dispose(); return image; } } ``` 调用`generate()`方法即可生成一个包含随机验证码的BufferedImage对象。可以将该对象输出到HTTP响应中,以便客户端进行验证。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值