图片验证码的生成

类别:Java 点击:0 评论:0 推荐:

1 package util; 2 3 import javax.imageio.ImageIO; 4 import javax.servlet.ServletException; 5 import javax.servlet.http.HttpServlet; 6 import javax.servlet.http.HttpServletRequest; 7 import javax.servlet.http.HttpServletResponse; 8 import java.awt.*; 9 import java.awt.image.BufferedImage; 10 import java.io.IOException; 11 import java.util.Random; 12 13 14 /** 15 * 描述: 16 * 17 * @author $author$ 18 * @version $Revision$ 19 */ 20 public class PicCheckCode extends HttpServlet { 21 22 private Font mFont = new Font("Arial Black", Font.PLAIN, 15); //设置字体 23 private int lineWidth = 2; //干扰线的长度=1.414*lineWidth 24 private int width = 60; //定义图形大小 25 private int height = 20; //定义图形大小 26 private int count = 200; 27 28 /** 29 * 描述: 30 * 31 * @param fc 描述: 32 * @param bc 描述: 33 * 34 * @return 描述: 35 */ 36 private Color getRandColor(int fc, int bc) { //取得给定范围随机颜色 37 38 Random random = new Random(); 39 40 if (fc > 255) { 41 42 fc = 255; 43 } 44 45 if (bc > 255) { 46 47 bc = 255; 48 } 49 50 int r = fc + random.nextInt (bc - fc); 51 int g = fc + random.nextInt (bc - fc); 52 int b = fc + random.nextInt (bc - fc); 53 54 return new Color(r, g, b); 55 } 56 57 //处理post 58 public void doPost(HttpServletRequest request, HttpServletResponse response) 59 throws ServletException, IOException { 60 61 doGet (request, response); 62 } 63 64 /** 65 * 描述: 66 * 67 * @param request 描述: 68 * @param response 描述: 69 * 70 * @throws ServletException 描述: 71 * @throws IOException 描述: 72 */ 73 public void doGet(HttpServletRequest request, HttpServletResponse response) 74 throws ServletException, IOException { 75 76 //response.reset(); 77 //设置页面不缓存 78 response.setHeader ("Pragma", "No-cache"); 79 response.setHeader ("Cache-Control", "no-cache"); 80 response.setDateHeader ("Expires", 0); 81 response.setContentType ("image/gif"); 82 83 // 在内存中创建图象 84 BufferedImage image = new BufferedImage(width, height, 85 BufferedImage.TYPE_INT_RGB); 86 87 // 获取图形上下文 88 Graphics2D g = (Graphics2D) image.getGraphics (); 89 90 //生成随机类 91 Random random = new Random(); 92 93 // 设定背景色 94 g.setColor (getRandColor (200, 250)); //---1 95 96 g.fillRect (0, 0, width, height); 97 98 //设定字体 99 g.setFont (mFont); 100 101 //画边框 102 g.setColor (getRandColor (0, 20)); //---2 103 g.drawRect (0, 0, width - 1, height - 1); 104 105 //随机产生干扰线,使图象中的认证码不易被其它程序探测到 106 for (int i = 0; i < count; i++) { 107 108 g.setColor (getRandColor (150, 200)); //---3 109 110 int x = random.nextInt (width - lineWidth - 1) + 1; //保证画在边框之内 111 int y = random.nextInt (height - lineWidth - 1) + 1; 112 int xl = random.nextInt (lineWidth); 113 int yl = random.nextInt (lineWidth); 114 g.drawLine (x, y, x + xl, y + yl); 115 } 116 117 //取随机产生的认证码(4位数字) 118 String sRand = ""; 119 120 for (int i = 0; i < 4; i++) { 121 122 String rand = String.valueOf (random.nextInt (10)); 123 sRand += rand; 124 125 // 将认证码显示到图象中,调用函数出来的颜色相同,可能是因为种子太接近,所以只能直接生成 126 g.setColor (new Color(20 + random.nextInt (130), 127 20 + random.nextInt (130), 20 + random.nextInt (130))); //--4--50-100 128 129 g.drawString (rand, (13 * i) + 6, 16); 130 131 132 } 133 134 // 将认证码存入SESSION 135 request.getSession ().setAttribute ("getImg", sRand); 136 137 // 图象生效 138 g.dispose (); 139 140 // 输出图象到页面 141 ImageIO.write (image, "PNG", response.getOutputStream ()); 142 } 143 } 144

本文地址:http://com.8s8s.com/it/it13166.htm