CAPTCHA Generator:
1 package com.lz.Tools; 2 3 Import Java.awt.Color; 4 Import Java.awt.Font; 5 Import Java.awt.Graphics; 6 Import Java.awt.Graphics2D; 7 Import Java.awt.image.BufferedImage; 8 Import Java.util.Random; 9 10/** 11 * CAPTCHA Generator * * @author Bojiangzhou */public class Vcodegenerator {16 17/** 18 * Verification Code Source */FINAL private char[] Code = {2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ', ' a ' , ' B ', ' C ', ' d ', ' e ', ' f ', ' g ', ' h ', ' I ', ' j ', ' K ', ' m ', ' n ', ' P ', ' Q ', ' R ', ' s ', ' t ', ' u ', ' V ', ' W ' ', ' x ', ' y ', ' z ', ' A ', ' B ', ' C ', ' D ', ' E ', ' F ', ' G ', ' H ', ' J ', ' K ', ' L ', ' M ', ' N ', ' P ', ' Q ', ' R ', and ' S ' ', ' T ', ' U ', ' V ', ' W ', ' X ', ' Y ', ' Z ' 27}; 28/** 29 * Font */FINAL private string[] FontNames = new string[]{32 "black Body", "Arial", "Cou RieR "," Arial "," Verdana "," Times "," Tahoma "," Georgia "}; 34/** 35 * Font style * */PNS Final Private Int[] Fontstyles = new int[]{Font.Bold, font.italic| Font.Bold 39}; 40 41/** 42 * Verification Code length 43 * Default 4 characters * */the private int vcodelen = 4; 46/** 47 * Verification Code picture Font size 48 * Default * * * * * * * * * * int fontsize = 21; 51/** 52 * Verification Code picture width of * */si private int width = (fontsize+1) *vcodelen+10; 55/** 56 * Verification Code Picture Height: */+/--private int height = fontsize+12; 59/** 60 * Number of interfering lines 61 * Default 3 Bar */Disturbline int = 3; Vcodegenerator () {} 67 68/** 69 * Specify Verification code length * @param vcodelen Verification Code length 71 * * Vcodegenerator (int vcodelen) {this.vcodelen = Vcodelen; this.width = (fontsize +1) *vcodelen+10; 75} 76 77/** 78 * Generate Verification code Picture of * @param vcode to draw the verification code * * @param drawline whether to draw the interference line Bayi * @re Turn/BufferedImage generatorvcodeimage (String Vcode, boOlean DrawLine) {84//Create Verification code picture BufferedImage Vcodeimage = new BufferedImage (width, height, bufferedimage . TYPE_INT_RGB); Vcodeimage.getgraphics Graphics g = (); 87//Fill background color g.setcolor (new color (246, 240, 250)); G.fillrect (0, 0, width, height); if (drawline) {drawdisturbline (g); 92} 93//For generating pseudo-random numbers 94 random ran = new Random (); 95//Draw the verification code in the picture for (int i = 0;i < Vcode.length (); i++) {97//Set Font 98 G.setfont ( New Font (Fontnames[ran.nextint (fontnames.length), Fontstyles[ran.nextint (Fontstyles.length)], fontsize)); 99//Randomly generated color g.setcolor (Getrandomcolor ()); 101//Draw Verification Code 102 g.drawstring (VCO De.charat (i) + "", i*fontsize+10, fontsize+5); 103}104//Releases the context of this graphic and all system resources it uses. G.dispose (); 106 107 return vcodeimage;108}109/**110 * Get the verification code picture of the rotated font 111 * @parAm vcode112 * @param drawline whether to draw interference line 113 * @return114 */115 public bufferedimage Generatorrotatevcodeima GE (String vcode, Boolean drawline) {116//create Authenticode picture 117 BufferedImage rotatevcodeimage = new BufferedImage (wid Th, height, bufferedimage.type_int_rgb); 118 graphics2d g2d = Rotatevcodeimage.creategraphics (); 119//Fill background color G2d.setcolor (New Color (246, +)), 121 g2d.fillrect (0, 0, width, height), 122 if (DrawLine) { 123 Drawdisturbline (g2d); 124}125//Draw a verification code on the picture 126 for (int i = 0;i < Vcode.length (); i+ +) {127 BufferedImage rotateimage = Getrotateimage (Vcode.charat (i)); G2d.drawimage (Rotateimage, NULL, (int) (This.height * 0.7) * I, 0); 129}130 G2d.dispose (); 131 return rotatevcodeimage;132 }133/**134 * Generate Verification Code 135 * @return Verification Code 136 */137 public String Generatorvcode () {138 int len = code.length;139 Random ran = new random (), stringbuffer sb = new StringBuffer (); 141 for (int i = 0;i < vcodelen;i++) {142 int index = Ran.nextint (len); 143 sb.append (Code[index]); 144}145 return sb.to String (); 146}147/**148 * Draw some interference lines for the CAPTCHA image 149 * @param g */151 private void Drawdisturbline (Gr Aphics g) {Random ran = new random (); 153 for (int i = 0;i < disturbline;i++) {154 int x1 = Ran.nextint (width); 155 int y1 = ran.nextint (height); 156 int x2 = ran.nextint (width); 157 int y2 = ran.nextint (height); 158 g.setcolor (Getrandomcolor ()); 159//Draw Interference line G.drawl INE (x1, y1, x2, y2); 161}162}163/**164 * Get a rotated picture 165 * @param c characters to be drawn 166 * @return167 */168 Private BufferedImage Getrotateimage (char c) {169 BufferedImage rotateimage = new BufferedImage (height , height, bufferedimage.TYPE_INT_ARGB); Graphics2D g2d = Rotateimage.creategraphics (); 171//Set Transparency to 0172 G2d.setcolor (new Color (255, 255, 255, 0)) 173 g2d.fillrect (0, 0, height, height); 174 random ran = new random (); 175 G2d.setfont (New Font (Fontnames[ran.nextint (fontnames.length)), Fontstyles[ran.nextint (Fontstyles.length)], FontSize)); 176 G2d.setcolor (Getrandomcolor ()); 177 Double theta = Gettheta (); 178//Rotate Picture 179 G2d.rotate (Theta, HEIGHT/2, HEIGHT/2); G2d.drawstring (Character.tostring (c), (Height-fontsize)/2, fontsize+5); 1 Bayi G2d.dispose (); 182 183 return rotateimage;184}185/**186 * @return return a random color 187 */188 private Color Getrandomcolor () {189 random ran = new random (), return new Color (Ran.nextint ( (Ran.nextint), Ran.nextint (220)); 191}192/**193 * @return Angle 194 */195 private Double Gettheta () {196 return ((int) (Math. Random () *1000)% 2 = = 0? -1:1) *math.random () 197}198 199/**200 * @return number of CAPTCHA characters 201 */202 public int Getvcodelen () {203 return vcodelen;204}205/**206 * Set number of verification code characters 207 * @param vcodeLen208 */209 public void SE Tvcodelen (int vcodelen) {this.width = (fontsize+3) *vcodelen+10;211 This.vcodelen = vcodelen;212}2 /**214 * @return Font size 215 */216 public int getfontsize () {217 return fontsize;218}219 /**220 * Set Font size 221 * @param fontsize222 */223 public void Setfontsize (int. fontsize) {224 this. width = (fontsize+3) *vcodelen+10;225 this.height = fontsize+15;226 this.fontsize = fontsize;227}228 /**229 * @return Picture width */231 public int getwidth () {232 return width;233}234/**235 * Set Picture width 236 * @param width237 */238 public void setwidth (int width) {239 This.width = width;240}241/**242 * @return Picture height 243 */244 public int getheight () {245 return height;246}247 /**248 * Set Picture height 249 * @param height */251 public void setheight (int height) {252 this.h eight = height;253}254/**255 * @return number of interfering lines, */257 public int getdisturbline () {258 ret Urn disturbline;259}260/**261 * Set number of interfering lines 262 * @param disturbline263 */264 public void Setdistu Rbline (int disturbline) {265 this.disturbline = disturbline;266}267 268}
Get the verification code in the servlet
1 package com.lz.Servlet; 2 3 Import Java.awt.image.BufferedImage; 4 Import java.io.IOException; 5 6 Import Javax.imageio.ImageIO; 7 Import javax.servlet.ServletException; 8 Import Javax.servlet.http.HttpServlet; 9 Import javax.servlet.http.httpservletrequest;10 Import javax.servlet.http.httpservletresponse;11 Import com.lz.tools.vcodegenerator;13 14 15 16/**17 * Get Verification code * @author bojiangzhou19 *20 */21 public class Getvcodeservlet Extends HttpServlet {All private static final long serialversionuid = 3259532010990625726l;24 public void DoG ET (httpservletrequest request, httpservletresponse response) throws Servletexception, IOException {26//Verification code cannot be cached 27 Response.setheader ("Expires", "1"), Response.setheader ("Cache-control", "NO-CAHCE"); response . SetHeader ("Pragma", "no-cache"); Vcodegenerator VCG = new Vcodegenerator (); 32//Get Verification Code 33 String Vcode = Vcg.generatorvcode (); 34//Get Verification Code picture 35// BufferedImage vcodeimage = Vcg.generatorrotatevcodeimage (Vcode, true); BufferedImage vcodeimage = vcg.ge Neratorvcodeimage (Vcode, true); 37//Save verification code to session domain object, request.getsession (). SetAttribute ("Vcode", Vcode); 39//Output Verification code picture Imageio.write (vcodeimage, "gif", Response.getoutputstream ()); 41}42 43}
The front desk in the img tag with SRC to obtain a verification code image can be
Ok!!!
Download source file
A Java picture verification code in its own package