Java CAPTCHA Code

Source: Internet
Author: User

1  Public classVerifycodecontroller {2     3     Private intwidth = 90;//define the width of the picture4     Private intheight = 20;//defines the height of a picture5     Private intCodecount = 4;//define the number of verification codes displayed on the image6     Private intxx = 15; 7     Private intFontheight = 18; 8     Private intCodey = 16; 9     Char[] codesequence = {' A ', ' B ', ' C ', ' D ', ' E ', ' F ', ' G ', ' H ', ' I ', ' J ', Ten' K ', ' L ', ' M ', ' N ', ' P ', ' Q ', ' R ', ' S ', ' T ', ' U ', ' V ', ' W ',  One' X ', ' Y ', ' Z ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ' };  A      -@RequestMapping ("/getverifycode") -      Public voidGetverifycode (httpservletrequest req, HttpServletResponse resp) the                 throwsIOException { -             //defining the image buffer -BufferedImage buffimg =Newbufferedimage (width, height, - Bufferedimage.type_int_rgb);  + //graphics2d gd = Buffimg.creategraphics ();  -             //graphics2d gd = (graphics2d) buffimg.getgraphics ();  +Graphics GD =buffimg.getgraphics (); A             //Create a random number generator class atRandom random =NewRandom (); -             //fill the image with white - Gd.setcolor (color.white); -Gd.fillrect (0, 0, width, height);  -       -             //To create a font, the size of the font should depend on the height of the image.  inFont font =NewFont ("Fixedsys", Font.Bold, fontheight);  -             //sets the font.  to Gd.setfont (font); +       -             //draw a border.  the Gd.setcolor (color.black); *Gd.drawrect (0, 0, width-1, height-1);  $      Panax Notoginseng             //randomly generates 40 lines of interference so that the authentication code in the image is not easily detected by other programs.  - Gd.setcolor (color.black); the              for(inti = 0; I < 40; i++) {  +                 intx =random.nextint (width); A                 inty =random.nextint (height); the                 intXL = Random.nextint (12);  +                 intYL = Random.nextint (12);  -Gd.drawline (x, y, X + xl, y +yl);  $             }  $       -             //The Randomcode is used to save randomly generated verification codes so that users can log in and authenticate.  -StringBuffer Randomcode =NewStringBuffer (); the             intRed = 0, green = 0, blue = 0;  -      Wuyi             //random generation of Codecount number verification code.  the              for(inti = 0; i < Codecount; i++) {  -                 //get a randomly generated captcha number.  WuString Code =string.valueof (Codesequence[random.nextint (codesequence.length)); -                 //generates a random color component to construct a color value so that the output will have a different color value for each digit.  AboutRed = Random.nextint (255);  $Green = Random.nextint (255);  -Blue = Random.nextint (255);  -       -                 //draws the verification code into the image with a randomly generated color.  AGd.setcolor (NewColor (red, green, blue));  +Gd.drawstring (Code, (i + 1) *xx, Codey);  the       -                 //The resulting four random numbers are grouped together.  $ randomcode.append (code); the             }  the             //Save the four-digit verification code to the session.  theHttpSession session =req.getsession (); the System.out.print (randomcode); -Session.setattribute ("Verifycode", randomcode.tostring ());  in       the             //suppresses image caching.  theResp.setheader ("Pragma", "No-cache");  AboutResp.setheader ("Cache-control", "No-cache");  theResp.setdateheader ("Expires", 0);  the       theResp.setcontenttype ("Image/jpeg");  +       -             //output the image to the servlet output stream.  theServletoutputstream SOS =Resp.getoutputstream ();BayiImageio.write (buffimg, "JPEG", SOS);  the sos.close (); the          -}

Java CAPTCHA Code

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.