Generate an image verification code and a verification code
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. http. HttpServlet;
Import javax. servlet. http. HttpServletRequest;
Import javax. servlet. http. HttpServletResponse;
Public class ImageServ extends HttpServlet {
/**
*
*/
Private static final long serialVersionUID = 1L;
Public void doGet (HttpServletRequest request, HttpServletResponse response)
Throws ServletException, IOException {
DoPost (request, response );
}
Public void doPost (HttpServletRequest request, HttpServletResponse response)
Throws ServletException, IOException {
// Prevent the page from being cached to ensure that a new verification code is generated each time.
Response. setHeader ("Pragma", "No-Cache ");
Response. setHeader ("Cache-Control", "no-Cache ");
Response. setIntHeader ("Expires", 0 );
// Set the response type and response set.
Response. setContentType ("image/jpeg ");
Request. setCharacterEncoding ("UTF-8 ");
Response. setCharacterEncoding ("UTF-8 ");
// Set the image size
Int width = 100;
Int height = 20;
// Create an image
BufferedImage img = new BufferedImage (width, height, BufferedImage. TYPE_INT_RGB );
// Drawing
Graphics g = img. getGraphics ();
G. setColor (getRandomColor (200,250 ));
G. fillRect (1, 1, width-1, height-1 );
G. setColor (new Color (100,100,100 ));
G. drawRect (0, 0, width-1, height-1 );
Random r = new Random ();
// Set the verification code
String str = getRadomString (5 );
Request. getSession (). setAttribute ("judge", str );
// Set the font
G. setFont (new Font ("Arial Black", Font. PLAIN, 16 ));
For (int I = 0; I <str. length (); I ++ ){
G. setColor (new Color (100 + r. nextInt (110), 100 + r. nextInt (110), 100 + r. nextInt (110 )));
G. drawString (str. substring (I, I + 1), I * 15 + 10, 15 );
}
// Draw interference lines
For (int I = 0; I <20; I ++ ){
G. setColor (getRandomColor (120,190 ));
Int x0 = r. nextInt (width)-1;
Int y0 = r. nextInt (height)-1;
Int x1 = r. nextInt (12) + 1;
Int y1 = r. nextInt (6) + 1;
G. drawLine (x0, y0, x0 + x1, y0 + y1 );
}
G. dispose ();
ImageIO. write (img, "JPEG", response. getOutputStream ());
}
/**
* Generate random colors
* @ Param fc
* @ Param bc
* @ Return
*/
Private Color getRandomColor (int fc, int bc ){
If (fc> 255) fc = 255;
If (bc> 255) bc = 255;
Random r = new Random ();
Int red = r. nextInt (Math. abs (fc-bc ));
Int green = r. nextInt (Math. abs (fc-bc ));
Int blue = r. nextInt (Math. abs (fc-bc ));
Return new Color (red, green, blue );
}
/**
* Generate a random string containing letters and numbers
* @ Param size
* @ Return
*/
Private String getRadomString (int size ){
StringBuilder sbu = new StringBuilder ();
Random r = new Random ();
Int count = 0;
While (count <size ){
Int index = r. nextInt ('Z' + 1 );
Char ch = (char) index;
If (Character. isLetterOrDigit (ch )){
Sbu. append (ch );
} Else {
Sbu. append (char) (index % 26 + (r. nextInt () % 2 = 0? 'A': 'A ')));
}
Count ++;
}
Return sbu. toString ();
}
}