Implementation of the image verification code function in java web
When registering website information, you must verify the data verification code. How can we implement the image verification code function?
The general steps are as follows:
1. Create a cache image in the memory
2. Set the background color
3. draw borders
4. Write Letters
5. Draw interference Information
6. image output
Let's just talk about the code.
Package com. lsgjzhuwei. servlet. response; import java. awt. color; import java. awt. font; import java. awt. graphics; import java. awt. image. bufferedImage; import java. io. IOException; import java. util. random; import javax. imageio. imageIO; import javax. servlet. servletException; import javax. servlet. annotation. webServlet; import javax. servlet. http. httpServlet; import javax. servlet. http. httpServletRequest; import javax. servlet. http. httpServletResponse;/*** Servlet implementation class VerificationCode */@ WebServlet (asyncSupported = true, urlPatterns = {"/VerificationCode "}) public class VerificationCode extends HttpServlet {private static final long serialVersionUID = 1L;/*** @ see HttpServlet # HttpServlet () */public VerificationCode () {super (); // TODO Auto-generated constructor stub}/*** @ see HttpServlet # doGet (HttpServletRequest request, response) */protected void doGet (HttpServletRequest request, response) throws ServletException, IOException {// TODO Auto-generated method stubint width = 120; int height = 30; // create a cache image in the memory BufferedImage bufferedImage = new BufferedImage (width, height, BufferedImage. TYPE_INT_RGB); // background color // use graphics to draw the image Graphics graphics = bufferedImage. getGraphics (); // sets the color of graphics. setColor (Color. yellow); // fill in graphics. fillRect (0, 0, width, height); // draw the border graphics. setColor (Color. blue); graphics. drawRect (0, 0, width-1, height-1); // write the letter String content = "Random"; random Random = new Random (); // set the font color graphics. setColor (Color. red); // set the font and size of graphics. setFont (new Font ("", Font. BOLD, 20); int x = 20; int y = 20; for (int I = 0; I <4; I ++) {int index = random. nextInt (content. length (); char letter = content. charAt (index); graphics. drawString (letter + "", x, y); x = x + 20;} // draw the interference line int x1; int x2; int y1; int y2; graphics. setColor (Color. LIGHT_GRAY); for (int I = 0; I <50; I ++) {x1 = random. nextInt (width); x2 = random. nextInt (width); y1 = random. nextInt (height); y2 = random. nextInt (height); graphics. drawLine (x1, y1, x2, y2);} // output the image to the browser // output the memory image to jpg image ImageIO through the browser output stream. write (bufferedImage, "jpg", response. getOutputStream ();}/*** @ see HttpServlet # doPost (HttpServletRequest request, response) */protected void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// TODO Auto-generated method stub }}