JSP Chinese Verification Code

Source: Internet
Author: User

<% @ Page pageEncoding = "gb2312" contentType = "image/jpeg" import = "javax. imageio. *, java. util. *, java. awt. image. *, java. awt. * "%>
<%!
// Obtain and generate a random color here
Color getRandColor (Random random, int ff, int cc ){
'If (ff> 255)
Ff = 255;
If (HTTP> 255)
Cc = 255;
Int r = ff + random. nextInt (cc-ff );
Int g = ff + random. nextInt (cc-ff );
Int B = ff + random. nextInt (cc-ff );
Return new Color (r, g, B );
} %>
<%
// Set no cache for the JSP page here
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 = 130;
Int height = 30;
// Set the randomly selected Chinese text. The text content here is too many and is not listed one by one, just for example.
String base = "\ u9752 \ u534a \ u706b \ u6cd5 \ signature \ u5efa \ signature \ u5531 \ signature \ u5973 \ signature \ u4ef6 \ u611f \ u51c6 \ u97f3 \ u7b54 \ u54e5 \ u9645 \ u65e7 \ u795e \ u5ea7 \ u7ae0 \ u538b \ u6162 \ u53d4 \ u80cc \ u7ec6 ";
// Set the number of alternative random Chinese Characters
Int length = base. length ();
// Create a cache Image
BufferedImage image = new BufferedImage (width, height, BufferedImage. TYPE_INT_RGB );
// Obtain the image object
Graphics g = image. getGraphics ();
// Create a random function
Random random = new Random ();
// Set the background color of the image
G. setColor (getRandColor (random, 188,235 ));
G. fillRect (0, 0, width, height );
// Set the random alternative font type
String [] fontTypes = {"\ u5b8b \ u4f53", "\ u65b0 \ u5b8b \ u4f53 ",
"\ U9ed1 \ u4f53", "\ u6977 \ u4f53", "\ u96b6 \ u4e66 "};
Int fontTypesLength = fontTypes. length;
// Add noise on the image background to increase the difficulty of Image Analysis
G. setColor (getRandColor (random, 180,199 ));
G. setFont (new Font ("Times New Roman", Font. PLAIN, 14 ));
For (int I = 0; I <4; I ++ ){
G. drawString ("@*@*@*@*@*@*@*",
0, 5 * (I + 2 ));
}
// Obtain the randomly generated Verification Code (4 Chinese characters)
// Save the Generated Chinese character string
String sRand = "";
For (int I = 0; I <4; I ++ ){
Int start = random. nextInt (length );
String rand = base. substring (start, start + 1 );
SRand + = rand;
// Set the font color of the image.
G. setColor (getRandColor (random, 10,150 ));
// Set the font format
G. setFont (new Font (fontTypes [random. nextInt (fontTypesLength)],
Font. BOLD, 18 + random. nextInt (6 )));
// Draw the Chinese character on the verification Image
G. drawString (rand, 24 * I + 10 + random. nextInt (8), 24 );
}
// Save the verification code to S ession
Session. setAttribute ("rand", sRand );
G. dispose ();
// Output the image to the JSP page
ImageIO. write (image, "JPEG", response. getOutputStream ());
// Close the stream
Out. clear ();
Out = pageContext. pushBody ();
%>
<% @ Page pageEncoding = "gb2312" contentType = "image/jpeg" import = "javax. imageio. *, java. util. *, java. awt. image. *, java. awt. * "%>
<%!
// Obtain and generate a random color here
Color getRandColor (Random random, int ff, int cc ){
'If (ff> 255)
Ff = 255;
If (HTTP> 255)
Cc = 255;
Int r = ff + random. nextInt (cc-ff );
Int g = ff + random. nextInt (cc-ff );
Int B = ff + random. nextInt (cc-ff );
Return new Color (r, g, B );
} %>
<%
// Set no cache for the JSP page here
Response. setHeader ("Pragma", "No-cache ");
Response. setHeader ("Cache-Control", "no-cache ");
Response. setDateHeader ("Expires", 0 );
// Set the length and width of the image to www.2cto.com.
Int width = 130;
Int height = 30;
// Set the randomly selected Chinese text. The text content here is too many and is not listed one by one, just for example.
String base = "\ u9752 \ u534a \ u706b \ u6cd5 \ signature \ u5efa \ signature \ u5531 \ signature \ u5973 \ signature \ u4ef6 \ u611f \ u51c6 \ u97f3 \ u7b54 \ u54e5 \ u9645 \ u65e7 \ u795e \ u5ea7 \ u7ae0 \ u538b \ u6162 \ u53d4 \ u80cc \ u7ec6 ";
// Set the number of alternative random Chinese Characters
Int length = base. length ();
// Create a cache Image
BufferedImage image = new BufferedImage (width, height, BufferedImage. TYPE_INT_RGB );
// Obtain the image object
Graphics g = image. getGraphics ();
// Create a random function
Random random = new Random ();
// Set the background color of the image
G. setColor (getRandColor (random, 188,235 ));
G. fillRect (0, 0, width, height );
// Set the random alternative font type
String [] fontTypes = {"\ u5b8b \ u4f53", "\ u65b0 \ u5b8b \ u4f53 ",
"\ U9ed1 \ u4f53", "\ u6977 \ u4f53", "\ u96b6 \ u4e66 "};
Int fontTypesLength = fontTypes. length;
// Add noise on the image background to increase the difficulty of Image Analysis
G. setColor (getRandColor (random, 180,199 ));
G. setFont (new Font ("Times New Roman", Font. PLAIN, 14 ));
For (int I = 0; I <4; I ++ ){
G. drawString ("@*@*@*@*@*@*@*",
0, 5 * (I + 2 ));
}
// Obtain the randomly generated Verification Code (4 Chinese characters)
// Save the Generated Chinese character string
String sRand = "";
For (int I = 0; I <4; I ++ ){
Int start = random. nextInt (length );
String rand = base. substring (start, start + 1 );
SRand + = rand;
// Set the font color of the image.
G. setColor (getRandColor (random, 10,150 ));
// Set the font format
G. setFont (new Font (fontTypes [random. nextInt (fontTypesLength)],
Font. BOLD, 18 + random. nextInt (6 )));
// Draw the Chinese character on the verification Image
G. drawString (rand, 24 * I + 10 + random. nextInt (8), 24 );
}
// Save the verification code to S ession
Session. setAttribute ("rand", sRand );
G. dispose ();
// Output the image to the JSP page
ImageIO. write (image, "JPEG", response. getOutputStream ());
// Close the stream
Out. clear ();
Out = pageContext. pushBody ();
%>


 

 

 

From sunset hut

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.