1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
| @WebServlet("/VerificationCode") public class VerificationCode extends HttpServlet{ protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException { int width =120; int height =30; BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics graphics = bi.getGraphics(); graphics.setColor(Color.white); graphics.fillRect(0, 0, width, height); graphics.setColor(Color.red); Random ran = new Random(); Color[] colors = {Color.blue, Color.green, Color.red}; for(int i=0; i<5; i++) { graphics.setColor(colors[ran.nextInt(3)]); int x1 = ran.nextInt(width); int y1 = ran.nextInt(height); int x2 = ran.nextInt(width); int y2 = ran.nextInt(height); graphics.drawLine(x1, y1, x2, y2); } graphics.setColor(Color.black); int left = 25; for(int i=0; i<4; i++) { int r = ran.nextInt(10); graphics.drawString(r+"", left, 20); left+=20; } resp.setContentType("image/jpeg"); ImageIO.write(bi, "JPEG", resp.getOutputStream()); } }
|