Packagedemo;ImportJava.awt.BasicStroke;ImportJava.awt.Color;ImportJava.awt.Font;ImportJava.awt.Graphics2D;ImportJava.awt.image.BufferedImage;ImportJava.io.File;Importjava.io.IOException;ImportJava.util.Random;ImportJavax.imageio.ImageIO;/*** Verification Code * *@authorWeirdo-world **/ Public classDemo1 { Public Static voidMain (string[] args) { for(inti = 0; I < 10; i++) {Verificationcode (6); } } //Verification Code Generation Public Static voidVerificationcode (intN) {String str= "abcdefghigklmnopqrstuvwxyzABCDEFGHIGKLMNOPQRSTUVWXYZ0123456789"; Char[] ch =New Char[n]; intw = n * 20 + 10; intH = 40; Random R=NewRandom (); BufferedImage img=NewBufferedImage (W, H, Bufferedimage.type_int_rgb);//Create image width heightGraphics2D g =(graphics2d) img.getgraphics (); G.setcolor (color.white);//Color SettingsG.fillrect (0, 0, W, h);//fills the specified rectangle intx = 10; G.setfont (NewFont ("Microsoft Jas Black", Font.Bold, 15));//font Settings for(inti = 0; I < n; i++) {G.setcolor (NewColor (R.nextint (255), R.nextint (255), R.nextint (255));//Random ColorCh[i] =Str.charat (R.nextint (Str.length ())); inty = R.nextint (20) + 15; g.DrawString (String.valueof (Ch[i]), x, y); X+ = 20; } //Alpha-Numeric interference intnn = R.nextint (n * 4) + 10; Char[] CHS =New Char[NN]; intXX = 10; for(inti = 0; i < nn; i++) {G.setcolor (NewColor (R.nextint (255), R.nextint (255), R.nextint (255), R.nextint (100) + 10)); Chs[i]=Str.charat (R.nextint (Str.length ())); inty = R.nextint (20) + 10; g.DrawString (String.valueof (Chs[i]), XX, y); XX+ = 8; } //Line Interference for(inti = 0; i < nn; i++) {G.setstroke (NewBasicstroke (R.nextint (3))); G.setcolor (NewColor (R.nextint (255), R.nextint (255), R.nextint (255), R.nextint (100) + 10)); intX1 =R.nextint (W); intY1 =r.nextint (h); intx2 =R.nextint (W); inty2 =r.nextint (h); G.drawline (x1, y1, x2, y2);// } Try{imageio.write (img,"JPG",NewFile ("g:/image/tmp/" +NewString (CH) + ". jpg")); } Catch(IOException e) {e.printstacktrace (); } }}
java-simple implementation of verification code generation