自生成图片验证码Servlet
1 package com.woniuxy.busniess.servlet; 2 3 import java.awt.*; 4 import java.awt.geom.*; 5 import java.awt.image.BufferedImage; 6 import java.io.IOException; 7 import java.util.Random; 8 import javax.imageio.ImageIO; 9 import javax.servlet.ServletException; 10 import javax.servlet.annotation.WebServlet; 11 import javax.servlet.http.HttpServlet; 12 import javax.servlet.http.HttpServletRequest; 13 import javax.servlet.http.HttpServletResponse; 14 import javax.servlet.http.HttpSession; 15 16 17 /** 18 * 图片验证码生成 19 * @author xumin 20 * 21 */ 22 @WebServlet("/PictureCheckCodeServlet") 23 public class PictureCheckCodeServlet extends HttpServlet { 24 25 26 27 28 // 获取随机颜色 29 30 /** 31 * 32 */ 33 private static final long serialVersionUID = 1L; 34 35 36 37 public Color getRandColor(int s, int e) { 38 39 Random random = new Random(); 40 41 if (s > 255) s = 255; 42 43 if (e > 255) e = 255; 44 45 int r = s + random.nextInt(e - s); //随机生成RGB颜色中的r值 46 47 int g = s + random.nextInt(e - s); //随机生成RGB颜色中的g值 48 49 int b = s + random.nextInt(e - s); //随机生成RGB颜色中的b值 50 51 return new Color(r, g, b); 52 53 } 54 55 56 57 58 public void service(HttpServletRequest request, HttpServletResponse response) 59 60 throws ServletException, IOException { 61 62 response.setHeader("Pragma", "No-cache"); 63 64 response.setHeader("Cache-Control", "No-cache"); 65 66 response.setDateHeader("Expires", 0); 67 68 69 // 指定生成的响应是图片 70 71 response.setContentType("image/jpeg"); 72 73 int width = 86; 74 75 int height = 26; 76 77 BufferedImage image = new BufferedImage(width, height, 78 79 BufferedImage.TYPE_INT_RGB); 80 81 Graphics g = image.getGraphics(); 82 83 Graphics2D g2d = (Graphics2D) g; 84 85 Random random = new Random(); 86 87 Font mFont = new Font("黑体", Font.BOLD, 17); 88 89 g.setColor(getRandColor(200, 250)); 90 91 g.fillRect(0, 0, width, height); 92 93 g.setFont(mFont); 94 95 g.setColor(getRandColor(180, 200)); 96 97 98 99 // 画随机的线条 100 101 for (int i = 0; i < 100; i++) { 102 103 int x = random.nextInt(width - 1); 104 105 int y = random.nextInt(height - 1); 106 107 int x1 = random.nextInt(6) + 1; 108 109 int y1 = random.nextInt(12) + 1; 110 111 BasicStroke bs = new BasicStroke(2f, BasicStroke.CAP_BUTT, 112 113 BasicStroke.JOIN_BEVEL); 114 115 Line2D line = new Line2D.Double(x, y, x + x1, y + y1); 116 117 g2d.setStroke(bs); 118 119 g2d.draw(line); 120 121 } 122 123 String sRand = ""; 124 125 126 127 128 // 输出随机的验证文字 129 130 String ctmp = ""; 131 132 int itmp = 0; 133 134 for (int i = 0; i < 4; i++) { 135 136 //random = new Random(new java.util.Date().getTime() + i); 137 138 switch (random.nextInt(3)) { 139 140 case 1: 141 142 itmp = random.nextInt(26) + 65; // 生成A~Z的字母 143 144 ctmp = String.valueOf((char) itmp); 145 if (ctmp.equalsIgnoreCase("o")) { 146 ctmp = "A"; 147 } 148 149 break; 150 151 default: 152 153 itmp = random.nextInt(10) + 48; // 生成0~9的数字 154 155 ctmp = String.valueOf((char) itmp); 156 if (ctmp.equalsIgnoreCase("0")) { 157 ctmp = "1"; 158 } 159 160 break; 161 162 } 163 164 sRand += ctmp; 165 166 Color color = new Color(20 + random.nextInt(110), 20 + random.nextInt(110), 20 + random.nextInt(110)); 167 168 g.setColor(color); 169 170 171 172 173 // 将文字旋转指定角度 174 175 Graphics2D g2d_word = (Graphics2D) g; 176 177 AffineTransform trans = new AffineTransform(); 178 179 trans.rotate(random.nextInt(45) * 3.14 / 180, 15 * i + 8, 7); 180 181 182 183 184 // 缩放文字 185 186 float scaleSize = random.nextFloat() +0.8f; 187 188 if (scaleSize > 1f) scaleSize = 1f; 189 190 trans.scale(scaleSize, scaleSize); 191 192 g2d_word.setTransform(trans); 193 194 g.drawString(ctmp, 15 * i + 18, 14); 195 } 196 197 198 199 200 // 将生成的验证码保存到Session中 201 202 HttpSession session = request.getSession(true); 203 204 session.setAttribute("randCheckCode", sRand); 205 206 g.dispose(); 207 //System.out.println("randCheckCode:"+session.getAttribute("randCheckCode")); 208 209 ImageIO.write(image, "JPEG", response.getOutputStream()); 210 211 } 212 213 214 } 215
版权声明:本文为thelovelybugfly原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。