Upgraded version of the color verification code generated by JSP-Chinese Character Verification Code (source code download)

Source: Internet
Author: User

Some websites use letters and numbers verification codes. The numbers and letters add up to more than 30. If you are interested, you can still identify them by means.
I saw an article "JSP color verification code" on the Internet. I enhanced it. The content of the generated verification code is Chinese characters, which can be easily applied to website authentication for Chinese netizens.

 

 

Of course, I also see several methods that others may consider:
1. Generate calculation questions, such as 20 + 34 * (23-12) = ?, The user must enter the correct answer to pass the verification. This idea is good, but the user must open the calculator for calculation, increasing the user's difficulty.
2. Ask some common questions. For example, the word "person" consists of several pieces. This is also a good idea. The key is that there must be tens of thousands of questions, and the answer to the questions must be clear, simple and easy to compare. This is also difficult.

The following is the code for the color Chinese Character verification code.

<% @ Page contenttype = "image/JPEG" Import = "Java. AWT. *, Java. AWT. image. *, Java. util. *, javax. imageIO. * "%>
<%!

// Generate random colors
Color getrandcolor (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 = "/u7684/u4e00/u4e86/u662f/u6211/latest/u5728/latest/u6765/latest/u8fd9/latest/u7740/u4e2a/u5730/u5230 /u5927/u91cc/u8bf4/examples/u53bb/examples/u90a3/u8981/examples/u65f6/u8fc7/u51fa/u5c0f/u4e48/u8d77 /users/u90fd/u628a/u597d/u8fd8/u591a/u6ca1/u4e3a/u53c8/u53ef/u5bb6/users/u53ea/users/u6837/users/u60f3/users /users/u8001/users/u4ece/u81ea/u9762/u524d/u5934/u9053/u5b83/u540e/users/u50cf/u89c1/users/u7528/u5979 /u56fd/u52a8/u8fdb/u6210/u56de/examples/u800c/examples/u5c71/u6c11/u5019/u7ecf/u53d1/u5de5 /u5411/u4e8b/u547d/u7ed9/latest/u4e09/u58f0/latest/u773c/u5fd7/u70b9/u5fc3/u6218 /users/u95ee/u4f46/u8eab/u65b9/users/u5403/u505a/u53eb/users/u542c/u9769/users/u5462/users/u5168/users/u56db/users /u6240/latest/u6700/u5149/u4ea7/u60c5/u8def/u5206/u603b/u6761/u767d/latest/u5982/u88ab/u82b1 /u53e3/u653e/u513f/u5e38/queues/u7b2c/queues/u5427/u6587/queues/u600e/u5b9a/u8bb8/u5feb/u660e /u884c/examples/u98de/u5916/u6811/u00009/u6d3b/u90e8/u95e8/u65e0/u5f80/u8239/u671b/examples/Examples /queues/u5458/u673a/u66f4/u4e5d/u60a8/u6bcf/u98ce/u7ea7/u8ddf/u7b11/queues/u76f4/queues/u591c /users/u9636/u8fde/u8f66/u91cd/u4fbf/u6597/users/u54ea/u5316/u592a/users/u793e/users/u58eb/u8005/users/u77f3 /u65e5/u51b3/u767e/queues/u62ff/queues/u672c/u601d/u89e3/queues/u6751/queues/u96be/u65e9/u8bba/u5417 /u6839/u5171/u8ba9/u76f8/u7814/u4eca/latest/u5750/u63a5/latest/u89c9/latest/u53cd/latest/u8bb0/u5c06/latest /users/u4e89/u9886/u6216/users/u5757/users/u52a0/u811a/u7d27/u7231/u7b49/u4e60/u9635/u6015 /queues/u9752/u534a/u706b/u6cd5/u9898/u5efa/u8d76/u4f4d/u5531/queues/u5973/queues/u51c6/queues/u56e2/queues /u79bb/u8272/Jun/u79d1/u5012/u775b/u5229/u4e16/u521a/u4e14/u7531/u9001/Jun/u8868/Jun/u6574 /examples/u54cd/u96ea/u6d41/u672a/u573a/u8be5/u5e76/u5e95/u6df1/u523b/examples/Examples /users/u53e4/users/u544a/users/u62c9/u540d/u5440/u571f/u6e05/u9633/users/u53f2/u6539/users/u753b/u9020/u5634 /u6b64/u6cbb/u5317/u5fc5/latest/u83dc/u722c/u7761/u5174/u5f62/u91cf/u54b1/u89c2 /u82e6/queues/u901a/u51b2/u5408/u7834/u53cb/u5ea6/u672f/u996d/u516c/u65c1/queues/u6781/queues/u67aa/queues /users/u91ce/u575a/users/u6536/users/u81f3/users/u57ce/u52b3/u843d/u94b1/users/u56f4/users/u80dc/u6559/u70ed/u5c55 /u6b4c/u7c7b/u6e10/latest/u6570/latest/u6027/u97f3/latest/u9645/u65e7/latest/u5e2e/u5566/u53d7/u7cfb /examples/u8df3/u975e/u4f55/u725b/u53d6/examples/u6133/examples/u79cd/u88c5/u9876/u6025/u6797/u505c/examples/Examples /u8863/u822c/u62a5/u53f6/u538b/u6162/u53d4/u80cc/u7ec6 ";
// 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 = {"/u5b8b/u4f53", "/u65b0/u5b8b/u4f53", "/u9ed1/u4f53", "/u6977/u4f53 ", "/u96b6/u4e66 "};
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 ());

%>
Source code download

Related Article

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.