Original: http://liuguihua0823.iteye.com/blog/1511355
ImportJava.awt.Color;ImportJava.awt.Font;ImportJava.awt.Graphics;ImportJava.awt.image.BufferedImage;Importjava.io.IOException;ImportJava.util.Random;Importjavax.servlet.http.HttpServletRequest;Importorg.apache.struts2.convention.annotation.Action;Importorg.apache.struts2.convention.annotation.InterceptorRefs;ImportOrg.apache.struts2.convention.annotation.ParentPackage;Importcom.rd.p2p.additional.redisCaptcha.util.ResponseUtil; @ParentPackage ("P2p-api") @InterceptorRefs ({@org. APACHE.STRUTS2.CONVENTION.ANNOTATION.INTERCEPTORREF ("Commoncheck") }) Public classTestactionextendsappbaseaction {@Action ("/app/test2") Public voidTest ()throwsioexception{Try { intwidth = 37, height =; String Basestr=Generatecheckcode (Request); BufferedImage Image=Newbufferedimage (width, height, bufferedimage.type_int_rgb); Graphics g=Image.getgraphics (); Random Random=NewRandom (); G.setcolor (Getrandcolor (Random,200, 250)); G.fillrect (0, 0, width, height); String[] Fonttypes= {"\u5b8b\u4f53", "\u65b0\u5b8b\u4f53", "\u9ed1\u4f53", "\u6977\u4f53", "\u96b6\u4e66" }; intFonttypeslength =fonttypes.length; G.setcolor (Getrandcolor (Random,160, 200)); G.setfont (NewFont ("Times New Roman", Font.plain, + random.nextint (6))); for(inti = 0; I < 255; i++) { intx =random.nextint (width); inty =random.nextint (height); intXL = Random.nextint (12); intYL = Random.nextint (12); G.drawline (x, y, x+ XL, Y +yl); } String [] Basechar= Basestr.split (""); for(inti = 0; i < basechar.length; i++) {G.setcolor (Getrandcolor (Random,30, 150)); G.setfont (NewFont (Fonttypes[random.nextint (fonttypeslength)), Font.Bold, + random.nextint (6))); g.DrawString (Basechar[i],* i + 10, 24); } g.dispose (); //Send pictureRESPONSEUTIL.SENDIMG (response, image, "Image/jpeg", "code", "JPG"); } Catch(IllegalStateException e) {System.out.println (E.getmessage ()); E.printstacktrace (); } } PrivateStatic Color Getrandcolor (random random,intFcintBC) { if(FC > 255) FC= 255; if(BC > 255) BC= 255; intr = FC + random.nextint (BC-FC); intg = FC + random.nextint (BC-FC); intB = fc + random.nextint (BC-FC); return NewColor (R, G, b); } Privatestatic String Generatecheckcode (HttpServletRequest request) {random random=NewRandom (); intintTemp; intIntfirst = Random.nextint (100); intIntsec = Random.nextint (100); String Checkcode= ""; intresult = 0; Switch(Random.nextint (6)) { Case0: if(Intfirst <intsec) {IntTemp=Intfirst; Intfirst=intsec; Intsec=intTemp; } Checkcode= Intfirst + "-" + intsec + "=?"; Result= intfirst-intsec; Break; Case1: if(Intfirst <intsec) {IntTemp=Intfirst; Intfirst=intsec; Intsec=intTemp; } Checkcode= Intfirst + "-? = "+ (intfirst-intsec); Result=intsec; Break; Case2: if(Intfirst <intsec) {IntTemp=Intfirst; Intfirst=intsec; Intsec=intTemp; } Checkcode= "? -"+intsec+" = "+ (intfirst-intsec); Result=Intfirst; Break; Case3: Checkcode= Intfirst + "+" + intsec + "=?"; Result= Intfirst +intsec; Break; Case4: Checkcode= Intfirst + "+?" = "+ (intfirst+intsec); Result=intsec; Break; Case5: Checkcode= "? + "+ intsec +" = "+ (intfirst+intsec); Result=Intfirst; Break; } System.out.println ("Result=" +result); Request.getsession (). SetAttribute ("Verify_code", result); returnCheckcode; } }
Java generate question-and-answer verification code picture, support the addition and subtraction algorithm