Java Verification Code

Source: Internet
Author: User

 Public classVerifycodecontroller {Private intwidth = -;//define the width of the picture    Private intHeight = -;//defines the height of a picture    Private intCodecount =4;//define the number of verification codes displayed on the image    Private intxx = the; Private intFontheight = -; Private intCodey = -; Char[] Codesequence = {'A','B','C','D','E','F','G','H','I','J',             'K','L','M','N','P','Q','R','S','T','U','V','W',             'X','Y','Z','1','2','3','4','5','6','7','8','9' }; @RequestMapping ("/getverifycode")     Public voidGetverifycode (httpservletrequest req, HttpServletResponse resp) throws IOException {//defining the image bufferBufferedImage buffimg =Newbufferedimage (width, height, bufferedimage.type_int_rgb);//graphics2d gd = Buffimg.creategraphics (); //graphics2d gd = (graphics2d) buffimg.getgraphics (); Graphics GD =Buffimg.getgraphics (); //Create a random number generator classRandom random =NewRandom (); //fill the image with whiteGd.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. Font font =NewFont ("Fixedsys", Font.Bold, fontheight); //sets the font. Gd.setfont (font); //draw a border. Gd.setcolor (Color.Black); Gd.drawrect (0,0, Width-1, Height-1); //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);  for(inti =0; I < +; i++) {                 intx =random.nextint (width); inty =random.nextint (height); intXL = Random.nextint ( A); intYL = Random.nextint ( A); 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 (); intRed =0, green =0, blue =0; //random generation of Codecount number verification code.              for(inti =0; i < Codecount; i++) {                 //get a randomly generated captcha number. String 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. Red = Random.nextint (255); Green= Random.nextint (255); Blue= Random.nextint (255); //draws the verification code into the image with a randomly generated color. Gd.setcolor (NewColor (red, green, blue)); Gd.drawstring (Code, (I+1) *xx, Codey); //The resulting four random numbers are grouped together. randomcode.append (code); }             //Save the four-digit verification code to the session. HttpSession session =req.getsession (); System. out. Print (Randomcode); Session.setattribute ("Verifycode", randomcode.tostring ()); //suppresses image caching. Resp.setheader ("Pragma","No-cache"); Resp.setheader ("Cache-control","No-cache"); Resp.setdateheader ("Expires",0); Resp.setcontenttype ("Image/jpeg"); //output the image to the servlet output stream. Servletoutputstream SOS =Resp.getoutputstream (); Imageio.write (buffimg,"JPEG", SOS);             Sos.close (); }

Java Verification 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.