Public classVerifycode {Private intW = 70; Private intH = 35; PrivateRandom r =NewRandom (); PrivateString[] FontNames = {"Arial", "Chinese Italic", "Black Body", "Microsoft Jas", "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) {intnum = 3; 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 (Color.Blue); 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 (); 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); } }
Java implementation of a simple CAPTCHA generator