import java.awt.BasicStroke; import java.awt.Color; import java.awt.Font; import java.awt.GradientPaint; import java.awt.Graphics2D; import java.awt.GraphicsEnvironment; import java.awt.Point; import java.awt.image.BufferedImage; import java.io.IOException; import java.io.OutputStream; import java.util.ArrayList; import java.util.List; import java.util.Locale; import java.util.Random; import javax.imageio.ImageIO; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class GraphicValidator */ public class GraphicValidator extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public GraphicValidator() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse * response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { process(request, response); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse * response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { process(request, response); } private void process(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("image/jpeg"); response.setCharacterEncoding("UTF-8"); List<String> fonts = new ArrayList<String>(); GraphicsEnvironment.getLocalGraphicsEnvironment().preferLocaleFonts(); String[] names = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames(Locale.CHINA); for (String s : names) { char c = s.charAt(0); if (!(Character.isLowerCase(c) || Character.isUpperCase(c))) { fonts.add(s); } } BufferedImage image = new BufferedImage(200, 50, BufferedImage.TYPE_INT_RGB); Graphics2D graphic = image.createGraphics(); char[] numbers = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' }; char[] use = new char[4]; graphic.setColor(new Color(240, 240, 240)); graphic.fillRect(0, 0, 200, 50); for (int i = 0; i < 4; i++) { Point p = new Point(5 + (i * ((int) (Math.random() * 10) + 40)), 40); int size = 0; int[] sizes = new int[20]; for (int j = 0; j < 20; j++) { sizes[j] = 30 + j; } size = sizes[(int) (Math.random() * sizes.length)]; int face = 0; if (Math.random() * 10 > 5) { face = Font.BOLD; } else { face = Font.ITALIC; } use[i] = numbers[(int) (Math.random() * numbers.length)]; graphic.setPaint(new GradientPaint(p.x, p.y, new Color((int) (Math.random() * 256), 0, (int) (Math.random() * 256)), p.x, p.y - size, new Color((int) (Math.random() * 256), (int) (Math.random() * 256), (int) (Math.random() * 256)))); graphic.setFont(new Font(fonts.get((int) (Math.random() * fonts.size())), face, size)); graphic.drawString("" + use[i], p.x, p.y); } String output = new String(use); request.getSession().setAttribute("code", output); graphic.setPaint(null); for (int i = 0; i < 4; i++) { graphic.setColor(new Color((int) (Math.random() * 0x00FFFFFFF))); graphic.drawLine((int) (Math.random() * 200), (int) (Math.random() * 50), (int) (Math.random() * 200), (int) (Math.random() * 50)); } Random random = new Random(); for (int i = 0; i < 88; i++) { int x = random.nextInt(200); int y = random.nextInt(50); graphic.setColor(new Color((int) (Math.random() * 0x00FFFFFFF))); graphic.setStroke(new BasicStroke((float) (Math.random() * 3))); graphic.drawLine(x, y, x, y); } OutputStream ot = response.getOutputStream(); ImageIO.write(image, "JPEG", ot); graphic.dispose(); ot.close(); } }