java-simple implementation of verification code generation

Source: Internet
Author: User

 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

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.