The steps of java generating captcha are summarized

  • 2020-07-21 08:10:44
  • OfStack

1, serialVersionUID

    private static final long serialVersionUID = -8501285780349046114L;

Java's serialization mechanism validates version 1 conformance by determining the serialVersionUID of the class at run time.

Equivalent to an java class ID card. Mainly used for version control.

2, BufferedImage class

The BufferedImage subclass describes Image with an accessible image data buffer.

TYPE_INT_RGB

Represents an image that has an 8-bit RGB color component of a composite integer pixel.

- getGraphics ()

This method returns Graphics2D, but here for backward compatibility.

3, request. getParameter ()

The data passed by the request.getParameter () method is passed from the Web client to the Web server, requesting data on behalf of HTTP.

The request.getParameter () method returns data of type String.

4, String... excludeProperty refers to the variable parameter, which means that multiple String objects can be passed in when this method is called. (Variable parameter: it is suitable for the case of uncertain number of parameters and certain type. java treats the variable parameter as an array.

Note: Variable parameters must be in the last item eg: private String drawRandomNum(Graphics2D g,String... createTypeFlag))

5, Graphics class

The Graphics class is an abstract base class for all graphics contexts, allowing applications to draw on components (already implemented on various devices) as well as closed screen images.

[Steps] :

1. Create 1 image in memory;

BufferedImage bi = new BufferedImage(WIDTH,HEIGHT,BufferedImage.TYPE_INT_RGB);

-- BufferedImage subclass describes Image with an accessible image data buffer.

TYPE_INT_RGB represents an image that has an 8-bit RGB color component that composes integer pixels.

- getGraphics ()

This method returns Graphics2D, but here for backward compatibility.

2. Get pictures;

Graphics g = bi.getGraphics();

3. Set the background color of the picture;

setBackGround(g);

4. Set the border of the picture;

setBorder(g);

5. Draw interference lines on the picture;

drawRandomLine(g);

6. Write random Numbers on the picture;

String random = drawRandomNum((Graphics2D) g,createTypeFlag); // createTypeFlag passed by client

7. Save the random number in session;

request.getSession().setAttribute("checkcode",random);

8. Set the response header to inform the browser to open in the form of picture;

response. setContentType (" image/jpeg "); // Equivalent to res[onse.setHeader(" ES167en-ES168en ","image/jpeg");

9. Set the response header to control the browser not to cache;


  response.setDateHeader("expries",-1);
  response.setHeader("Cache-Control","no-cache");
  response.setHeader("Pragma","no-cache");

- setDateHeader

  public void setDateHeader(java.lang.String name,long date)

Parameters:

name - the name of the header to set

date - the assigned date value

- setHeader

            public void setHeader(java.lang.String name, java.lang.String value)

Parameters:

name - the name of the header

value - the header value If it contains octet string, it should be encoded according to RFC 2047 (http://www.ietf.org/rfc/rfc2047.txt)

Write the image to your browser;

        ImageIO.write(bi,"jpg",response.getOutputStream());

- parameters:

im - RenderedImage to write.

formatName - String contains informal names in a format.

output - OutputStream where data will be written.

Throws:

IllegalArgumentException - If any parameter is null.

IOException - If an error occurs during writing.

I hope you found this article helpful


Related articles: