Packagecom.lf.testvity;ImportJava.awt.Color;ImportJava.awt.Font;ImportJava.awt.Graphics2D;ImportJava.awt.Robot;ImportJava.awt.image.BufferedImage;ImportJava.io.FileInputStream;ImportJava.io.FileOutputStream;Importjava.io.IOException;ImportJava.util.Random;ImportJavax.imageio.ImageIO;Importjavax.security.auth.message.callback.PrivateKeyCallback.Request;ImportJavax.servlet.http.HttpServletResponse;Importjavax.servlet.http.HttpSession;ImportJavax.sound.midi.Patch;Importorg.junit.Test; Public classVitiry {//number of verification code characters Private Static intCount = 4; //number of lines of interference Private Static intLines = 2; /*** Randomly generated color *@returnColor*/ Private StaticColor Getrandomcolor () {random random=NewRandom (); Color Color=NewColor (Random.nextint (255) +1, Random.nextint (255) +1, Random.nextint (255) +1); returncolor; } /*** Get a four-character string *@returnstring*/ Public Staticstring Getforthword () {string string= "23456789ABCDEFGHIJKMNPQRSTUVWXYZ"; StringBuilder Newstr=NewStringBuilder (""); //randomly gets count numbers, resulting in a string based on count random numbersRandom random =NewRandom (); for(inti = 0; I < count; i++) { intrannum=Random.nextint (String.Length ()); Newstr.append (String.charat (rannum)); } String str=NewString (NEWSTR); returnstr; } /*** Painting Verification code *@returnBufferedImage*/ Public Staticbufferedimage productimage () {intwidth = 70; intHeight = 30; //get the picture bufferBufferedImage bi =Newbufferedimage (width, height, bufferedimage.type_int_rgb); //get it in the drawing environment (this picture of the pen)graphics2d g2 =(graphics2d) bi.getgraphics (); //Set ColorG2.setcolor (Color.White); //fills the whole picture (actually setting the background color)G2.fillrect (0, 0, width, height); //Set FontG2.setfont (NewFont ("Arial", Font.Bold, 25)); //Set ColorG2.setcolor (Vitiry.getrandomcolor ()); //write a string to a pictureG2.drawstring (Vitiry.getforthword (), 7, 25); //draw two lines of interferenceRandom random =NewRandom (); for(inti = 0; i < lines; i++) {G2.drawline (2, Random.nextint (height-10) +10, width-5, Random.nextint (height)); } returnbi; } }
Java Generate Verification Code