Implementation of the image verification code function in java web

Source: Internet
Author: User

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 }}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.