java的图片验证码
1.验证码生成工具类
1 package cn.bd.jboa.util; 2 3 import java.awt.Color; 4 import java.awt.Font; 5 import java.awt.Graphics; 6 import java.awt.image.BufferedImage; 7 import java.io.ByteArrayInputStream; 8 import java.io.ByteArrayOutputStream; 9 import java.util.Random; 10 import javax.imageio.ImageIO; 11 import javax.imageio.stream.ImageOutputStream; 12 13 public class RandomNumUtil { 14 private ByteArrayInputStream image;// 图像 15 private String str;// 验证码 16 17 private RandomNumUtil() { 18 init();// 初始化属性 19 } 20 21 /* 22 * 取得RandomNumUtil实例 23 */ 24 public static RandomNumUtil Instance() { 25 return new RandomNumUtil(); 26 } 27 28 /* 29 * 取得验证码图片 30 */ 31 public ByteArrayInputStream getImage() { 32 return this.image; 33 } 34 35 /* 36 * 取得图片的验证码 37 */ 38 public String getString() { 39 return this.str; 40 } 41 42 private void init() { 43 // 在内存中创建图象 44 int width = 85, height = 20; 45 BufferedImage image = new BufferedImage(width, height, 46 BufferedImage.TYPE_INT_RGB); 47 // 获取图形上下文 48 Graphics g = image.getGraphics(); 49 // 生成随机类 50 Random random = new Random(); 51 // 设定背景色 52 g.setColor(getRandColor(200, 250)); 53 g.fillRect(0, 0, width, height); 54 // 设定字体 55 g.setFont(new Font("Times New Roman", Font.PLAIN, 18)); 56 // 随机产生155条干扰线,使图象中的认证码不易被其它程序探测到 57 g.setColor(getRandColor(160, 200)); 58 for (int i = 0; i < 155; i++) { 59 int x = random.nextInt(width); 60 int y = random.nextInt(height); 61 int xl = random.nextInt(12); 62 int yl = random.nextInt(12); 63 g.drawLine(x, y, x + xl, y + yl); 64 } 65 // 取随机产生的认证码(6位数字) 66 String sRand = ""; 67 for (int i = 0; i < 6; i++) { 68 String rand = String.valueOf(random.nextInt(10)); 69 sRand += rand; 70 // 将认证码显示到图象中 71 g.setColor(new Color(20 + random.nextInt(110), 20 + random 72 .nextInt(110), 20 + random.nextInt(110))); 73 // 调用函数出来的颜色相同,可能是因为种子太接近,所以只能直接生成 74 g.drawString(rand, 13 * i + 6, 16); 75 } 76 // 赋值验证码 77 this.str = sRand; 78 79 // 图象生效 80 g.dispose(); 81 ByteArrayInputStream input = null; 82 ByteArrayOutputStream output = new ByteArrayOutputStream(); 83 try { 84 ImageOutputStream imageOut = ImageIO 85 .createImageOutputStream(output); 86 ImageIO.write(image, "JPEG", imageOut); 87 imageOut.close(); 88 input = new ByteArrayInputStream(output.toByteArray()); 89 } catch (Exception e) { 90 System.out.println("验证码图片产生出现错误:" + e.toString()); 91 } 92 93 this.image = input;/* 赋值图像 */ 94 } 95 96 /* 97 * 给定范围获得随机颜色 98 */ 99 private Color getRandColor(int fc, int bc) { 100 Random random = new Random(); 101 if (fc > 255) 102 fc = 255; 103 if (bc > 255) 104 bc = 255; 105 int r = fc + random.nextInt(bc - fc); 106 int g = fc + random.nextInt(bc - fc); 107 int b = fc + random.nextInt(bc - fc); 108 return new Color(r, g, b); 109 } 110 }
2.action
1 package cn.bd.jboa.action; 2 3 import java.io.ByteArrayInputStream; 4 import cn.bd.jboa.util.RandomNumUtil; 5 import com.opensymphony.xwork2.ActionContext; 6 import com.opensymphony.xwork2.ActionSupport; 7 8 /** 9 * 验证码 10 * @author TaoXianXue 11 * 12 */ 13 public class RandomAction extends ActionSupport { 14 15 private static final long serialVersionUID = 1L; 16 private ByteArrayInputStream inputStream; 17 18 // 生成验证码方法 19 public String execute() throws Exception { 20 RandomNumUtil rdnu = RandomNumUtil.Instance(); 21 this.setInputStream(rdnu.getImage());/// 取得带有随机字符串的图片 22 ActionContext.getContext().getSession().put("random", rdnu.getString());//把验证码放入HttpSession中 23 return "random"; 24 } 25 26 public void setInputStream(ByteArrayInputStream inputStream) { 27 this.inputStream = inputStream; 28 } 29 30 public ByteArrayInputStream getInputStream() { 31 return inputStream; 32 } 33 }
3.struts.xml
1 <!-- 验证码操作Action配置 --> 2 <package name="random" extends="json-default"> 3 <!-- Random验证码 --> 4 <action name="random" class="randomAction"> 5 <result type="stream" name="random"> 6 <param name="contentType">image/jpeg</param> 7 <param name="inputName">inputStream</param> 8 </result> 9 </action> 10 </package>
4.jsp页面
1 <input type="image" src="random.action" onclick="this.src=this.src+'?'" title="点击图片刷新验证码" />