Package Xf.web;import java.awt.*;import java.awt.geom.*;import java.awt.image.*;import java.io.*;import java.util.*; Import Javax.servlet.servletexception;import Javax.servlet.http.httpservlet;import Javax.servlet.http.httpservletrequest;import Javax.servlet.http.httpservletresponse;import Javax.servlet.http.httpsession;import Javax.imageio.imageio;public class DrawImage extends HttpServlet {private Stati C Final Long serialversionuid = 1L; Public DrawImage () {super (); } public void Destroy () {Super.destroy (); } public void Init () throws Servletexception {Super.init (); }/* The method's main function is to obtain a randomly generated color */Public color getrandcolor (int s,int e) {random random=new random (); if (s>255) s=255; if (e>255) e=255; int r,g,b; R=s+random.nextint (e-s);//randomly generate R value G=s+random.nextint (e-s) in RGB color,//randomly generate G value b=s+random.nextint (e-s) in RGB color;//random Generates the B-value of the RGB color return new color (R,G,B); } @Override Public VOID Service (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException { Settings do not cache picture Response.setheader ("Pragma", "No-cache"); Response.setheader ("Cache-control", "No-cache"); Response.setdateheader ("Expires", 0); Specifies the generated response picture, must not be missing this sentence, otherwise error. Response.setcontenttype ("Image/jpeg"); The int width=86,height=25;//Specifies the width and height of the generated verification code bufferedimage image=new BufferedImage (width,height,bufferedimage.type_int_r GB);//Create a BufferedImage object that acts as a picture graphics g=image.getgraphics ();//Create a Graphics object that acts like a brush graphics2d g2d= (Gra PHICS2D) g;//Create grapchics2d object random random=new random (); Font mfont=new font ("console", font.bold,16);//define Font style G.setcolor (Getrandcolor (200,250)); G.fillrect (0, 0, width, height);//Draw Background G.setfont (Mfont);//Set Font G.setcolor (Getrandcolor (180,200)); Draw 100 colors and positions are all randomly generated lines that are 2f for (int i=0;i<100;i++) {int x=random.neXtint (width-1); int Y=random.nextint (HEIGHT-1); int X1=random.nextint (6) +1; int Y1=random.nextint (12) +1; Basicstroke bs=new Basicstroke (2f,basicstroke.cap_butt,basicstroke.join_bevel);//Custom Line style line2d line=new Line2D. Double (X,Y,X+X1,Y+Y1); G2d.setstroke (BS); G2d.draw (line);//Draw straight Lines}//output a verification code consisting of English, random numbers. String srand= ""; String ctmp= ""; int itmp=0; The verification code for the development output is four-bit for (int i=0;i<4;i++) {switch (Random.nextint (2)) {Case 0://generates a-Z letter Itmp=random.nextint (26) +65; Ctmp=string.valueof ((char) itmp); Break Default://0-9 string[] rbase={"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"}; int x = Random.nextint (10); Ctmp = Rbase[x]; Break } srand+=ctmp; Color Color=new Color (20+ranDom.nextint (20+random.nextint), Random.nextint (110)); G.setcolor (color); Randomly scales the generated random number and rotates the angular PS. It is recommended that you do not scale and rotate the text, because the picture may not display correctly */* rotate the text to make an angle */graphics2d g2d_word= (graphics2d) G; AffineTransform trans=new affinetransform (); Trans.rotate (() * 3.14/180, * i + 8, 7); G2d_word.settransform (trans); g.DrawString (Ctmp, 15*i+18, 14); } HttpSession Session=request.getsession (true); Session.setattribute ("Randcheckcode", sRand);//Set the value of a variable, you can later use GetAttribute () to obtain and consume it g.dispose ();//release g of system resources occupied Imageio.write (image, "JPEG", Response.getoutputstream ());//Output Picture}}
Copyright notice: All brothers, please feel free to reprint, please indicate who is the brother
Java Verification Code