JSP graphic verification code-Chinese Characters

Source: Internet
Author: User

<% @ Page contentType = "image/jpeg" import = "java. awt. *, java. awt. image. *, java. util. *, javax. imageio. * "%>
  
<%!
// Create by smallnest
// Email: smallnest@gmail.com
// Website: www.kuaff.com
// Generate random colors
Color getRandColor (Random random, int fc, int bc ){
If (fc> 255) fc = 255;
If (bc> 255) bc = 255;
Int r = fc + random. nextInt (bc-fc );
Int g = fc + random. nextInt (bc-fc );
Int B = fc + random. nextInt (bc-fc );
Return new Color (r, g, B );
}
%>
  
<%
// Set the page not to cache
Response. setHeader ("Pragma", "No-cache ");
Response. setHeader ("Cache-Control", "no-cache ");
Response. setDateHeader ("Expires", 0 );
// Set the length and width of the image
Int width = 176, height = 30;
// Set Alternative Chinese characters to remove some indecent Chinese Characters
String base = "bytes ";
// Length of alternative Chinese Characters
Int length = base. length ();
// Create memory image
BufferedImage image = new BufferedImage (width, height, BufferedImage. TYPE_INT_RGB );
// Obtain the image Context
Graphics g = image. getGraphics ();
// Create a random class instance
Random random = new Random ();
// Set the background color of the image (because it is a background, so it is light)
G. setColor (getRandColor (random, 200,250 ));
G. fillRect (0, 0, width, height );
// Alternative font
String [] fontTypes = {
"U5b8bu4f53", "u65b0u5b8bu4f53", "u9ed1u4f53", "u6977u4f53", "u96b6u4e66 "};
Int fontTypesLength = fontTypes. length;
// Add noise to the image background
G. setColor (getRandColor (random, 160,200 ));
G. setFont (new Font ("Times New Roman", Font. PLAIN, 14 ));
For (int I = 0; I <6; I ++ ){
G. drawString ("************************************* ********", 0, 5 * (I + 2 ));
}
// Obtain the Random verification code (6 Chinese characters)
// Save the Generated Chinese character string
String sRand = "";
For (int I = 0; I <6; I ++ ){
Int start = random. nextInt (length );
String rand = base. substring (start, start + 1 );
SRand + = rand;
// Set the font color
G. setColor (getRandColor (random, 10,150 ));
// Set the font
G. setFont (new Font (fontTypes [random. nextInt (fontTypesLength)], Font. BOLD, 18 + random. nextInt (6 )));
// Draw the Chinese character to the image
G. drawString (rand, 24 * I + 10 + random. nextInt (8), 24 );
}
// Save the authentication code to the session
Session. setAttribute ("rand", sRand );
G. dispose ();
// Output the image to the page
ImageIO. write (image, "JPEG", response. getOutputStream ());
%>

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.