Original address: http://my.oschina.net/u/1757031/blog/488322
ImportJava.awt.Color;ImportJava.awt.Font;ImportJava.awt.Graphics;ImportJava.awt.image.BufferedImage;Importjava.io.IOException;ImportJava.util.Random;ImportJavax.imageio.ImageIO;ImportJavax.servlet.ServletOutputStream;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse;Importjavax.servlet.http.HttpSession;ImportOrg.springframework.stereotype.Controller;Importorg.springframework.web.bind.annotation.RequestMapping; @Controller Public classCodecontroller {Private intwidth = 90;//define the width of the picture Private intheight = 20;//defines the height of a picture Private intCodecount = 4;//define the number of verification codes displayed on the image Private intxx = 15; Private intFontheight = 18; Private intCodey = 16; Char[] codesequence = {' A ', ' B ', ' C ', ' D ', ' E ', ' F ', ' G ', ' H ', ' I ', ' J ', ' K ', ' L ', ' M ', ' N ', ' O ', ' P ', ' Q ', ' R ', ' S ', ' T ', ' U ', ' V ', ' W ', ' X ', ' Y ', ' Z ', ' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ' }; @RequestMapping ("/code") Public voidGetCode (httpservletrequest req, HttpServletResponse resp)throwsIOException {//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 < 40; i++) { intx =random.nextint (width); inty =random.nextint (height); 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 (); 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 (36)]); //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 ("Code", 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 (); } }
SPRINGMVC Verification Code Program