Generate Random Captcha Tool class

Source: Internet
Author: User

Generate Random Verification code

 PackageCom.web;//Validation code Generation processing classImportJava.awt.BasicStroke;ImportJava.awt.Color;ImportJava.awt.Font;ImportJava.awt.Graphics;ImportJava.awt.Graphics2D;ImportJava.awt.geom.AffineTransform;ImportJava.awt.geom.Line2D;ImportJava.awt.image.BufferedImage;Importjava.io.IOException;ImportJava.util.Random;ImportJavax.imageio.ImageIO;Importjavax.servlet.ServletException;ImportJavax.servlet.http.HttpServlet;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse;Importjavax.servlet.http.HttpSession;Importcom.tools.MD5; Public classPicturecheckcodeextendshttpservlet{Private Static Final LongSerialversionuid = 1L; PrivateRandom rand=NewRandom (); /*** Generate random colors * *@paramstart [int] *@paramend [INT] *@returnColor [Object]*/     PublicColor Getrandcolor (intStartintend) {        intRandnum; if(start>255) start=255; if(end>255) end=255; if(start>end) randnum=start-end; Elserandnum=end-start; intr=start+Rand.nextint (Randnum); intg=start+Rand.nextint (Randnum); intb=start+Rand.nextint (Randnum); return NewColor (R,G,B); }            /*** coloring \ Rotation \ Zoom * *@paramWord text *@paramg Picture Object*/     Public voidColoredandrotation (String Word,intI,graphics g) {        /**Coloring **/G.setcolor (NewColor (20+rand.nextint, 20+rand.nextint), 20+rand.nextint (110))); /**Rotation **/graphics2d g2d=(graphics2d) G; AffineTransform Trans=NewAffineTransform (); Trans.rotate (Rand.nextint (*3.14/180,15*i+8,7)); /**Zoom **/        floatScalesize=rand.nextfloat () +0.8f; if(scalesize>1f) scalesize=1f;        Trans.scale (Scalesize, scalesize);        G2d.settransform (trans); g.DrawString (Word,15*i+20,20); }        /*** Generate 100 lines of interference * *@paramG2d *@paramWidth *@paramHeight*/     Public voidGetrandline (Graphics2D g2d,intWidthintheight) {         for(inti=0;i<100;i++){            intX=rand.nextint (width-1); intY=rand.nextint (height-1); intZ=rand.nextint (6) +1; intW=rand.nextint (12) +1; Basicstroke BS=NewBasicstroke (2f,basicstroke.cap_butt,basicstroke.join_bevel); Line2D Line=NewLine2d.double (x,y,x+z,y+W);            G2d.setstroke (BS);        G2d.draw (line); }    }        /*** Get Random text * *@paramlength [int] captcha size *@paramg [Graphics] Picture Object *@returnString * @case1: A-Z * @case2: Chinese * @default: 0-9*/@SuppressWarnings ("Unused")     PublicString Getrandword (intLength,graphics g) {String Finalword= "", firstword= ""; intTempint=0; String[] Array={                "0", "1", "2", "3",                "4", "5", "6", "7",                "8", "9", "a", "B",                "C", "D", "E", "F"};  for(inti=0;i<length;i++){            Switch(Rand.nextint (2)){                 Case1: Tempint=rand.nextint (26) +65; FirstWord=string.valueof ((Char) tempint);  Break; /*Case 2:int R1,R2,R3,R4; String Strh,strl;//high&low R1=rand.nextint (3) +11;                        Front closed rear open [11,14] if (r1==13) {r2=rand.nextint (7);                        }else{R2=rand.nextint (16);                        } r3=rand.nextint (6) +10;                        if (r3==10) {r4=rand.nextint (15) +1;                        }else if (r3==15) {r4=rand.nextint (15);                        }else{R4=rand.nextint (16);                        } STRH=ARRAY[R1]+ARRAY[R2];                                                STRL=ARRAY[R3]+ARRAY[R4];                        Byte[] Bytes=new byte[2];                        Bytes[0]= (Byte) (Integer.parseint (strh,16)); Bytes[1]= (Byte) (InteGer.parseint (strl,16));                        Firstword=new String (bytes); Break*/                default: Tempint=rand.nextint (10) +48; FirstWord=string.valueof ((Char) tempint);  Break; } Finalword+=FirstWord;  This. Coloredandrotation (Firstword,i, G); }                returnFinalword; }    protected voidService (HttpServletRequest request, httpservletresponse response)throwsservletexception, IOException {//setting does not cache picturesResponse.setheader ("Pragma", "No-cache"); Response.setheader ("Cache-control", "No-cache"); Response.setdateheader ("Expires", 0); //Create a pictureResponse.setcontenttype ("Image/jpeg"); intwidth=100; intHeight=40; BufferedImage Image=NewBufferedImage (WIDTH,HEIGHT,BUFFEREDIMAGE.TYPE_INT_RGB); Graphics g=Image.getgraphics (); Graphics2D g2d=(graphics2d) G; Font Mfont=NewFont ("Arial", font.bold,22); G.setcolor ( This. Getrandcolor (200,250)); G.fillrect (0, 0, width, height);        G.setfont (Mfont); G.setcolor ( This. Getrandcolor (180,200));  This. Getrandline (g2d, width, height); String Randcode= This. Getrandword (4, G); HttpSession Session=request.getsession (); Session.setattribute ("Randcode", Md5.code (Randcode.tolowercase ()));                G.dispose (); Imageio.write (Image,"JPEG", Response.getoutputstream ()); }}

Generate Random Captcha Tool class

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.