Use JSP to generate a color Chinese Character Verification Code

Source: Internet
Author: User

<% @ Page Language = "Java" contenttype = "image/JPEG" pageencoding = "UTF-8" %> <br/> <% @ page import = "Java. AWT. color, <br/> JAVA. AWT. font, <br/> JAVA. AWT. graphics, <br/> JAVA. AWT. image. bufferedimage, <br/> javax. imageIO. imageIO, <br/> JAVA. util. random "%> </P> <p> <%! <Br/> // generate a random color. <br/> color getrandcolor (random, int FC, int BC) {<br/> If (FC> 255) fc = 255; <br/> If (BC> 255) BC = 255; <br/> int r = FC + random. nextint (BC-Fc); <br/> int G = FC + random. nextint (BC-Fc); <br/> int B = FC + random. nextint (BC-Fc); <br/> return new color (R, G, B ); <br/>}< br/>%> </P> <p> <% <br/> // set the page to not be cached <br/> response. setheader ("Pragma", "No-Cache"); <br/> response. setheader ("cache-control", "No-Cache"); <br/> response. setdateheader ("expires", 0); </P> <p> // set the image length and width <br/> int width = 176, Height = 30; </P> <p> // set Alternative Chinese characters <br/> string base = "/u7684/u4e00/u4e86/u662f/u6211/u4e0d/u5728/u4eba/u4eec/u6709 /u6765/latest/u8fd9/latest/u7740 "+ <br/>"/u4e2a/u5730/u5230/u5927/u91cc/u8bf4/u5c31/u53bb/u5b50/latest/ examples/u90a3/u8981/Examples "+ <br/>"/u770b/u5929/u65f6/examples/u51fa/examples/u4e48/u8d77/examples/u90fd/u628a/u597d/u8fd8 /u591a/u6ca1 "+ <br/>"/u4e3a/u53c8/u53ef/u5bb6/u5b66/u53ea/u4ee5/u4e3b/latest/u6837/latest/u60f3/latest/ u8001 "+ <br/>"/u4e2d/u5341/u4ece/u81ea/u9762/latest/u5934/u9053/latest/u8d70/u5f88/u50cf/u89c1 "+ <br/> "/u4e24/u7528/u5979/u56fd/u52a8/u8fdb/u6210/u56de/samples/u5bf9/u5f00/u800c/u5df1" + <br/> "/users/u73b0/u5c71/u6c11/u5019/users/u53d1/u5de5/u5411/u4e8b/u547d/users/u6c34/u51e0" + <br/> "/ u4e49/u4e09/u58f0/latest/u9ad8/u624b/u77e5/u7406/u773c/u5fd7/latest/u6218/latest/u95ee "+ <br/> /users/u5403/u505a/u53eb/u5f53/u4f4f/u542c/u9769/users/u5462/users/u5168 "+ <br/>"/u624d/u56db/u5df2/ u6240/examples/u6700/u5149/u4ea7/u60c5/u8def/u5206/u603b/u6761/u767d "+ <br/>"/u8bdd/u4e1c/examples/Examples /u5982/u88ab/u82b1/u53e3/u653e/u513f/u5e38/u6c14/examples/u7b2c "+ <br/>"/examples/u519b/u5427/u6587/examples/ u518d/u679c/u600e/u5b9a/u8bb8/examples/u884c/Examples "+ <br/>"/examples/u98de/u5916/u6811/u00009/examples/u90e8 /users/u5f80/u8239/u671b/u65b0/u5e26/u961f "+ <br/>"/users/u7ad9/u4ee3/u5458/u673a/u66f4/ examples/u60a8/u6bcf/u98ce/u7ea7/u8ddf "+ <br/>"/examples/templates/u5c11/u76f4/u610f/u591c/u6bd4/u9636/Examples /users/u91cd/users/u6597 "+ <br/>"/users/u54ea/u5316/u592a/u6307/u53d8/u793e/u4f3c/u58eb/u8005/users/u77f3/ examples/u65e5/u51b3 "+ <br/>"/u767e/u539f/u62ff/u7fa4/examples/samples/u672c/u601d/u89e3/u7acb/u6cb3/u6751/Examples /u96be "+ <br/>"/u65e9/u8bba/u5417/u6839/u5171/u8ba9/u76f8/u7814/u4eca/users/u5750/u63a5/users" + <br/> "/u4fe1/u89c9/examples/u53cd/examples/u5343/u627e/u4e89/u9886/u6216/u5e08/u7ed3/u5757" + <br /> "/u8dd1/u8c01/u8349/u8d8a/users/u52a0/u811a/u7d27/u7231/users/u9635/u6015/u6708/u9752" + <br/>" /u534a/u706b/u6cd5/u9898/u5efa/u8d76/latest/u5531/u6d77/latest/u5973/latest/u51c6 "+ <br/>"/u5f20/ u56e2/u5c4b/u79bb/u8272/u8138/u7247/u79d1/u5012/u775b/u5229/latest/u521a/latest/u7531 "+ <br/>"/u9001/latest /queues/u8868/u591f/u6574/u8ba4/u54cd/u96ea/u6d41/u672a/u573a/queues "+ <br/>"/u5e76/u5e95/u6df1/queues/ u5e73/latest/u8fd1/u4family/u8f7b/u8bb2/u519c/u53e4 "+ <br/>"/u9ed1/u544a/latest/u62c9/latest/u5440 /users/u6e05/u9633/u7167/u529e/u53f2/u6539/users "+ <br/>"/u753b/u9020/u5634/users/u6cbb/u5317/u5fc5/ examples/u96e8/u7a7f/u5185/u8bc6/examples/u4e1a "+ <br/>"/u83dc/examples/u7761/examples/u5f62/u91cf/u54b1/u89c2 /users/u901a/u51b2/u5408/u7834 "+ <br/>"/u53cb/u5ea6/u672f/u996d/u516c/u65c1/u623f/u6781/users/u67aa/ u8bfb/platform/u91ce "+ <br/>"/u575a/u7a7a/u6536/platform/u81f3/u653f/u57ce/u52b3/u843d/u94b1/platform/u56f4 /users/u80dc/u6559 "+ <br/>"/u70ed/u5c55/u5305/users/u7c7b/u6e10/u5f3a/u6570/u4e61/users/u6027/u97f3/users/ examples/u9645 "+ <br/>"/u65e7/u795e/u5ea7/u7ae0/examples/u7cfb/u4ee4/u8df3/u975e/u4f55/examples/u53d6/Examples "+ <br/>"/users/u68.0/u6389/u5ffd/u79cd/u88c5/u9876/u6025/u6797/u505c/users/u53e5/u533a/u8863/users "+ <br/> "/u62a5/u53f6/u538b/u6162/u53d4/u80cc/u7ec6 "; </P> <p> // length of alternative Chinese characters <br/> int length = base. length (); </P> <p> // create a memory image <br/> bufferedimage image = new bufferedimage (width, height, bufferedimage. type_int_rgb); </P> <p> // obtain the image context <br/> graphics G = image. getgraphics (); </P> <p> // create a random class instance <br/> random = new random (); </P> <p> // set the background color of the image. The background color is lighter. <br/> G. setcolor (getrandcolor (random, 200,250); <br/> G. fillrect (0, 0, width, height); </P> <p> // optional font <br/> string [] fonttypes = {"/u5b8b/u4f53 ", "/u65b0/u5b8b/u4f53", "/u9ed1/u4f53", "/u6977/u4f53", "/u96b6/u4e66"}; <br/> int types = fonttypes. length; </P> <p> // Add noise to the image background <br/> G. setcolor (getrandcolor (random, 160,200); <br/> G. setfont (new font ("Times New Roman", Font. plain, 14); <br/> for (INT I = 0; I <6; I ++) {<br/> G. drawstring ("************************************* ********", 0, 5 * (I + 2 )); <br/>}</P> <p> // obtain the random token of 6 Chinese characters. <br/> string srand = ""; <br/> for (INT I = 0; I <6; I ++) {<br/> int start = random. nextint (length); <br/> string Rand = base. substring (start, start + 1); <br/> srand + = rand; <br/> // set the font color <br/> G. setcolor (getrandcolor (random, 10,150); <br/> // set the font <br/> G. setfont (new font (fonttypes [random. nextint (fonttypeslength)], Font. bold, 18 + random. nextint (6); <br/> // draw the Chinese character on the image <br/> G. drawstring (RAND, 24 * I + 10 + random. nextint (8), 24); <br/>}</P> <p> // Save the authentication code to the session <br/> session. setattribute ("RAND", srand); </P> <p> G. dispose (); </P> <p> // output the image to the page <br/> ImageIO. write (image, "Jpeg", response. getoutputstream (); <br/> %>

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.