1 Importjava.awt.*;2 ImportJava.awt.image.BufferedImage;3 ImportJava.io.*;4 ImportJava.util.Random;5 ImportJavax.imageio.ImageIO;6 7 Public classValidationcode {8 9 //The character set of the graphic verification Code, the system will randomly select some characters from this string as the verification codeTen Private StaticString codechars = "% #23456789abcdefghkmnpqrstuvwxyzABCDEFGHKLMNPQRSTUVWXYZ"; One A //returns a random color (color object) - Private StaticColor Getrandomcolor (intMincolor,intMaxcolor) { -Random random =NewRandom (); the //save Mincolor Max no more than 255 - if(Mincolor > 255) -Mincolor = 255; - //save Mincolor Max no more than 255 + if(Maxcolor > 255) -Maxcolor = 255; + //get a red random color value A intRed = Mincolor + random.nextint (Maxcolor-mincolor); at //get a green random color value - intGreen = Mincolor + random.nextint (Maxcolor-mincolor); - //get a blue random color value - intBlue = Mincolor + random.nextint (Maxcolor-mincolor); - return NewColor (red, green, blue); - } in - protected Static voidGetvalidationcode ()throwsIOException { to Try { + //get the length of the Verification Code collection - intCharslength =codechars.length (); the //set the length and width of the graphics verification Code (size of the graphic) * intwidth = all, height = 30; $BufferedImage image =Newbufferedimage (width, height, bufferedimage.type_int_rgb);Panax NotoginsengGraphics g = image.getgraphics ();//get a Graphics object for output text -Random random =NewRandom (); theG.setcolor (Getrandomcolor (180, 250));//randomly set the color to fill +G.fillrect (0, 0, width, height);//Fill graphic Background A //set the initial font theG.setfont (NewFont ("Times New Roman", font.italic, height)); +G.setcolor (Getrandomcolor (120, 180));//randomly set font color - //used to save the last randomly generated verification code $StringBuilder Validationcode =NewStringBuilder (); $ //random font for verification codes -String[] FontNames = {"Times New Roman", "Book Antiqua", "Arial" }; - //randomly generate 3 to 5 verification codes the for(inti = 0; I < 3 + random.nextint (3); i++) { - //randomly set the font of the character of the current CaptchaWuyiG.setfont (NewFont (Fontnames[random.nextint (3)] (font.italic, height)); the //randomly get the character of the current verification code - CharCodechar =Codechars.charat (Random.nextint (charslength)); Wu validationcode.append (Codechar); - //randomly sets the color of the current CAPTCHA character AboutG.setcolor (Getrandomcolor (10, 100)); $ //output the captcha characters on the graph, and X and Y are randomly generated -g.DrawString (String.valueof (Codechar), i + random.nextint (7), Height-random.nextint (6)); - } -File File =NewFile ("D:\\code.png"); AImageio.write (Image, "PNG", file); + System.out.println (validationcode.tostring ()); the //byte[] data = ((DataBufferByte) Image.getdata (). Getdatabuffer ()). GetData (); - g.dispose (); $}Catch(Exception e) { the E.printstacktrace (); the } the } the - Public Static voidMain (string[] args)throwsioexception{ in Getvalidationcode (); the } the}
Java Generate Verification Code