Java web, verification code image generation technology, java Verification Code

Source: Internet
Author: User

Java web, verification code image generation technology, java Verification Code

I happened to know that some website Verification Code images were randomly generated. After hearing this, I made this small example.

The java. awt package is used to generate an image, draw a background, number, and interference line. The BufferedImage is used to generate an image, and then the Graphics object is used to draw the content.

The Code is as follows:

1 package com. util; 2 3 import java. awt. color; 4 import java. awt. graphics; 5 import java. awt. image. bufferedImage; 6 import java. util. random; 7 8 public class ImageCodeUtil {9 10 public static BufferedImage getImage () {11 Random r = new Random (); 12 // 1. create a bufferedimage object 13 BufferedImage image = new BufferedImage (60, 20, BufferedImage. TYPE_INT_RGB); 14 // 2. draw a rectangle 15 Graphics g = image. getGraphics (); 16g. setColor (new Color (233,222,220); 17g. fillRect (0, 0, 60, 20); 18 // 3. draw interference line 19 for (int I = 0; I <200; I ++) {20g. setColor (new Color (40 + r. nextInt (200), 40 + r. nextInt (200), 40 + r. nextInt (200); 21 int x = r. nextInt (50); 22 int y = r. nextInt (20); 23g. drawLine (x, y, x + r. nextInt (20), y + r. nextInt (15); 24} 25 // 4. write four digits 26g. setColor (new Color (23, 22, 20); 27 int checkCode = r. nextInt (9000) + 1000; 28g. drawString (checkCode + "", 15, 15); 29 // closes the Graphics object and releases 30 GB of resources. dispose (); 31 return image; 32} 33 34}

This is to generate an image. In the webpage display, you can use the img tag to request image resources through servlet. The Code is as follows:

1 

Finally, in the servlet code, respond to the browser's request and send the image resources to the browser. The Code is as follows:

Package com. controller; import java. awt. image. bufferedImage; import java. io. IOException; 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; import com. util. imageCodeUtil;/*** Servlet implementation class CheckCodeServlet */@ WebServlet ("/check") public class CheckCodeServlet extends HttpServlet {private static final long serialVersionUID = 1L; /*** @ see HttpServlet # HttpServlet () */public CheckCodeServlet () {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 stub // you can specify the response type. setContentType ("image/jpeg"); // java is used to generate a verification code image. awt package BufferedImage image = ImageCodeUtil. getImage (); // returns ImageIO through outputStream. write (image, "JPEG", response. getOutputStream ());}}

The above verification code is refreshed every time the page is refreshed and requested for service. You can add the js Code registration click event so that you can click the verification code image to change it. The Code is as follows:

Note: Here I use jq

$("img").click(function(){            $(this).attr("src","/ServletTest/check?a="+new Date().getMilliseconds());            });

So far, the verification code has been achieved. For more information about the beautiful display on the page, use css to modify it.

 

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.