Java code generation image verification code implementation
This section describes an example of using Java code to generate a random image verification code.
Can be directly configured as a Servlet and called directly on the page
Java code randomly generates an image Verification Code
Package com. rchm. util. images;
Import java. awt. Color;
Import java. awt. Font;
Import java. awt. Graphics2D;
Import java. awt. image. BufferedImage;
Import java. io. FileOutputStream;
Import java. io. IOException;
Import java. io. OutputStream;
Import java. util. Random;
Import javax. imageio. ImageIO;
/**
* Verification Code Generator
*/
Public class ValidateCode {
// The image width.
Private int width = 160;
// The Image Height.
Private int height = 40;
// Number of characters in the verification code
Private int codeCount = 5;
// Number of lines interfering with the verification code
Private int line COUNT = 150;
// Verification Code
Private static String code = null;
// Verification Code Image Buffer
Private BufferedImage buffImg = null;
Private char [] codeSequence = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'h ', 'I', 'J', 'k', 'l ',
'M', 'n', 'P', 'Q', 'R', 's', 't', 'U', 'V', 'w ', 'X', 'y ',
'Z', '1', '2', '3', '4', '5', '6', '7', '8 ', '9 '};
Public ValidateCode (){
This. createCode ();
}
/**
*
* @ Param width: Image width
* @ Param height: Image height
*/
Public ValidateCode (int width, int height ){
This. width = width;
This. height = height;
This. createCode ();
}
/**
*
* @ Param width: Image width
* @ Param height: Image height
* @ Param codeCount: number of characters
* @ Param lineCount Number of interfering lines
*/
Public ValidateCode (int width, int height, int codeCount, int lineCount ){
This. width = width;
This. height = height;
This. codeCount = codeCount;
This. lineCount = lineCount;
This. createCode ();
}
Public void createCode (){
Int x = 0, fontHeight = 0, codeY = 0;
Int red = 0, green = 0, blue = 0;
X = width/(codeCount + 2); // The width of each character
FontHeight = height-2; // font height
CodeY = height-4;
// Image buffer
BuffImg = new BufferedImage (width, height, BufferedImage. TYPE_INT_RGB );
Graphics2D g = buffImg. createGraphics ();
// Generate a random number
Random random = new Random ();
// Fill the image in white
G. setColor (Color. WHITE );
G. fillRect (0, 0, width, height );
// Create a font
ImgFontByte imgFont = new ImgFontByte ();
Font font = imgFont. getFont (fontHeight );
G. setFont (font );
For (int I = 0; I
Int xs = random. nextInt (width );
Int ys = random. nextInt (height );
Int xe = xs + random. nextInt (width/8 );
Int ye = ys + random. nextInt (height/8 );
Red = random. nextInt (255 );
Green = random. nextInt (255 );
Blue = random. nextInt (255 );
G. setColor (new Color (red, green, blue ));
G. drawLine (xs, ys, xe, ye );
}
// RandomCode records the randomly generated Verification Code
StringBuffer randomCode = new StringBuffer ();
// Generate a random codeCount verification code.
For (int I = 0; I
String strRand = String. valueOf (codeSequence [random. nextInt (codeSequence. length)]);
// Generate random color values so that the color values of each character are different.
Red = random. nextInt (255 );
Green = random. nextInt (255 );
Blue = random. nextInt (255 );
G. setColor (new Color (red, green, blue ));
G. drawString (strRand, (I + 1) * x, codeY );
// Combine the four random numbers.
RandomCode. append (strRand );
}
// Save the four-digit verification code to the Session.
Code = randomCode. toString ();
}
Public void write (String path) throws IOException {
OutputStream sos = new FileOutputStream (path );
This. write (sos );
}
Public void write (OutputStream sos) throws IOException {
ImageIO. write (buffImg, "png", sos );
Sos. close ();
}
Public BufferedImage getBuffImg (){
Return buffImg;
}
Public static String getCode (){
Return code;
}
}
Use this class in servlet:
Package com. rchm. util. images;
Import java. io. IOException;
Import javax. servlet. ServletException;
Import javax. servlet. http. HttpServlet;
Import javax. servlet. http. HttpServletRequest;
Import javax. servlet. http. HttpServletResponse;
Import javax. servlet. http. HttpSession;
Public class ValidateCodeServlet extends HttpServlet {
Private static final long serialVersionUID = 1L;
Protected void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Response. setContentType ("image/jpeg ");
Response. setHeader ("Pragma", "no-cache ");
Response. setHeader ("Cache-Control", "no-cache ");
Response. setDateHeader ("Expires", 0 );
ValidateCode vCode = new ValidateCode (4,100, 30 );
HttpSession session = request. getSession ();
Session. removeAttribute ("validateCode ");
VCode. write (response. getOutputStream ());
Session. setAttribute ("validateCode", vCode. getCode ());
VCode. write (response. getOutputStream ());
}
}
Configure the Servlet access path in web. xml:
ValidateCodeServlet
Class> com. rchm. util. images. ValidateCodeServletclass>
ValidateCodeServlet