Image Verification Code JSP

Source: Internet
Author: User

The image verification code used in the project some time ago. There are a lot of open-source image verification, written in jquery. But later I did not adopt it. I adopted the simplest one and shared it with you.

<% @ Page Language = "Java" contenttype = "image/JPEG" pageencoding = "UTF-8" %> <% @ page contenttype = "image/JPEG" Import = "Java. AWT. *, Java. AWT. image. *, Java. util. *, javax. imageIO. * "%> <% @ page import =" Java. io. outputstream "%> <%! Color getrandcolor (int fc, int BC) {// obtain the random color random = new random (); 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) ;}%> <% Try {// set the page to not cache response. setheader ("Pragma", "No-Cache"); response. setheader ("cache-control", "No-Cache"); response. setdateheader ("expires", 0); // create an image in the memory int width = 60, Height = 20; bufferedimage image = new bufferedimage (width, height, bufferedimage. type_int_rgb); outputstream OS = response. getoutputstream (); // obtain the image context graphics G = image. getgraphics (); // generate random class random = new random (); // set the background color G. setcolor (getrandcolor (200,250); G. fillrect (0, 0, width, height); // set the font G. setfont (new font ("Times New Roman", Font. plain, 18); // draw a border // G. setcolor (new color (); // G. drawrect (155, width-1, height-1); // generates random interference lines, making the identification code in the image hard to be detected by other programs. setcolor (getrandcolor (160,200); For (INT I = 0; I <155; I ++) {int x = random. nextint (width); int y = random. nextint (height); int XL = random. nextint (12); int yl = random. nextint (12); G. drawline (X, Y, x + XL, Y + yl);} // obtain the random ID code (4 digits) string srand = ""; for (INT I = 0; I <4; I ++) {// string Rand = string. valueof (random. nextint (10); // number // string Rand = string. valueof (char) (math. round (math. random () * 25 + 65); // uppercase http://www.itstudy.cn/string Rand = string. valueof (char) (math. round (math. random () * 25 + 97); // lowercase srand + = rand; // display the ID code to the image G. setcolor (new color (20 + random. nextint (110), 20 + random. nextint (110), 20 + random. nextint (110); // The color from the call function is the same, probably because the seed is too close, so you can only generate G directly. drawstring (RAND, 13 * I + 6, 16);} // Save the authentication code to the session. setattribute ("RAND", srand); // image effective G. dispose (); // output the image to the ImageIO page. write (image, "Jpeg", OS); OS. flush (); OS. close (); OS = NULL; response. flushbuffer (); out. clear (); out = pagecontext. pushbody ();} catch (illegalstateexception e) {system. out. println (E. getmessage (); E. printstacktrace () ;}%>

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.