Java Generation Captcha Gadget

Source: Internet
Author: User

Inadvertently see a gadget class that generates a simple verification code:

Tool Class Code:

ImportJava.awt.BasicStroke;ImportJava.awt.Color;ImportJava.awt.Font;ImportJava.awt.Graphics2D;ImportJava.awt.image.BufferedImage;Importjava.io.IOException;ImportJava.io.OutputStream;ImportJava.util.Random;ImportJavax.imageio.ImageIO; Public classVerifycode {Private intW = 70; Private intH = 35; PrivateRandom r =NewRandom (); PrivateString[] FontNames = {"Arial", "Chinese Italic", "Black Body", "Chinese New Wei", "Chinese Script", "Microsoft Ya Hei", "italics _gb2312"}; PrivateString codes = "23456789abcdefghjkmnopqrstuvwxyzABCDEFGHJKMNPQRSTUVWXYZ"; PrivateColor BgColor =NewColor (255, 255, 255); PrivateString text; PrivateColor Randomcolor () {intRed = R.nextint (150); intGreen = R.nextint (150); intBlue = R.nextint (150); return NewColor (red, green, blue); }        PrivateFont Randomfont () {intindex =R.nextint (fontnames.length); String FontName=Fontnames[index]; intstyle = R.nextint (4); intSize = R.nextint (5) + 24; return NewFont (fontname, style, size); }        Private voidDrawLine (bufferedimage image) {//Draw 5 lines of interference into the picture        intnum = 5; Graphics2D G2=(graphics2d) image.getgraphics ();  for(inti = 0; i < num; i++) {            intX1 =R.nextint (W); intY1 =r.nextint (h); intx2 =R.nextint (W); inty2 =r.nextint (h); G2.setstroke (NewBasicstroke (1.5F));             G2.setcolor (Randomcolor ());        G2.drawline (x1, y1, x2, y2); }    }        Private CharRandomchar () {intindex =R.nextint (Codes.length ()); returnCodes.charat (index); }        Privatebufferedimage createimage () {bufferedimage image=NewBufferedImage (W, H, Bufferedimage.type_int_rgb); Graphics2D G2=(graphics2d) image.getgraphics (); G2.setcolor ( This. BgColor); G2.fillrect (0, 0, W, h); returnimage; }         Publicbufferedimage getImage () {bufferedimage image=createimage (); Graphics2D G2=(graphics2d) image.getgraphics (); StringBuilder SB=NewStringBuilder (); //Draw 4 characters into a picture         for(inti = 0; I < 4; i++) {String s= Randomchar () + "";             Sb.append (s); floatx = i * 1.0F * W/4;             G2.setfont (Randomfont ());             G2.setcolor (Randomcolor ()); G2.drawstring (S, x, H-5); }         This. Text =sb.tostring ();         DrawLine (image); returnimage; }         PublicString GetText () {returntext; }         Public Static voidoutput (bufferedimage image, outputstream out)throwsIOException {imageio.write (image,"JPEG", out); }}
View Code

Test code:

ImportJava.awt.image.BufferedImage;ImportJava.io.FileOutputStream;Importorg.junit.Test; Public classDemo2 {@Test Public voidFUN1 ()throwsException {verifycode Verifycode=NewVerifycode (); BufferedImage Bi=Verifycode.getimage ();        System.out.println (Verifycode.gettext ()); Verifycode.output (BI,NewFileOutputStream ("F:/b.jpg")); }}
View Code

Operation Result:

Java Generation Captcha Gadget

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.