1. Verification code Generation , we can see is drawn with the Graphics object. Object we have to get the Graphics object
1-1, the acquisition of graphics objects, to be obtained through bufferedimage
<span style= "FONT-SIZE:18PX;" >int width=100;//determines the size of the box
int height=40;
BufferedImage BFI =new bufferedimage (width, height, bufferedimage.type_int_rgb);
1-2, the General Verification Code background box are white
<span style= "FONT-SIZE:18PX;" >//1, set Background (white box)
G.setcolor (color.white);//White Brush
g.fillrect (0, 0, width, height); draw rectangular rectangular frame </span >
1-3, save the data (background verification used) and set the font style (beautiful)
String str= "";//Save Data
Random rom=new Random ();
Set uppercase and bold for a font
1-4, generate specific values, and randomly generated colors
for (int i=0;i<4;i++) {
int num=rom.nextint (10);//generated random number
G.setcolor (New Color (Rom.nextint (256), Rom.nextint (256), Rom.nextint (256));/Set the color of the brush (random)
g.drawstring ("+num, 20*i, 20+rom.nextint (10)); The position of x increases the 20,y coordinates to 201 lines, online or offline
//ps: Location needs to be clarified,
1-5, the general number is easily recognized by other software directly, in order to prevent black. Add a little bit of interference line
Draw some interference lines
for (int i = 0; i < i++) {
g.setcolor (new Color (Rom.nextint (256), Rom.nextint (256), Rom.nextint (256)); /Set the color of the brush (random)
g.drawline (Rom.nextint, Rom.nextint (), Rom.nextint (), Rom.nextint (40));/position is also random, X, The value of Y should not exceed the rectangular frame
1-6. Destroying graphics objects and storing pictures
<span style= "White-space:pre" > </span>g.dispose ()//Destroy Object
Imageio.write (BFI, "JPEG", Res.getoutputstream ());//Picture with a byte stream directly to get PS:: Res is inside the servlet.
So the verification code is generated, so how do we get into the foreground?
2, Concrete realization
Foreground code rendering ():
SRC address source is sent from the background. The path is very interesting.
2-1 Steps
Project inside
The MyEclipse--> src-->new Servlet appears as follows:
Click on----next to the following page:
This configuration is automatically web.xml to the Web-inf folder inside the project. This box inside the value is the foreground src written in the need to access the path,----> Click to complete the line.
It's automatic. Generate the following interface:
Here you can write the previous code. But note that we have to override this method to be effective:
protected void Service (HttpServletRequest req, HttpServletResponse resp)//automatically generate input <span style= "font-family:arial, Helvetica, Sans-serif; " >service, auto-generation </span>
throws Servletexception, IOException {
//TODO auto-generated method stub
Super.service (req, resp);
}
The specific code is as follows:
Package cn.hncu.com.servlet;
Import Java.awt.Color;
Import Java.awt.Font;
Import Java.awt.Graphics;
Import Java.awt.image.BufferedImage;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import Java.util.Random;
Import Javax.imageio.ImageIO;
Import javax.servlet.ServletException;
Import Javax.servlet.ServletRequest;
Import Javax.servlet.ServletResponse;
Import Javax.servlet.http.HttpServlet;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse; public class Imgdemo extends HttpServlet {@Override the public void service (ServletRequest req, servletresponse Res) thr
oWS servletexception, IOException {int width=100;//determines the size of the box int height=40;
BufferedImage BFI =new bufferedimage (width, height, bufferedimage.type_int_rgb); Graphics g=bfi.getgraphics ()///Get Graphics object can draw//1, set background (white box) G.setcolor (color.white);//White Brush g.fillrect (0, 0, Widt h, height),//Draw rectangular rectangular box//2, the specific generation of random number String str= "";//Save Data Random roM=new Random ();
Sets uppercase and coarse g.setfont for the font (new font ("a", font.bold,20)); Draw a specific picture for (int i=0;i<4;i++) {int num=rom.nextint (10);//generated random number G.setcolor (new Color (Rom.nextint (256), ROM.N Extint (256), Rom.nextint (256))//Set the color of the brush (random) g.drawstring ("" +num, 20*i, 20+rom.nextint (10)),//Draw out, x position each increase by 20, The coordinates of Y are 201 lines, online or offline//ps: The position needs to be explicit,}//Draw some interference line for (int i = 0; i < i++) {G.setcolor (new Color (Rom.nexti NT (256), Rom.nextint (256), Rom.nextint (256));//Set the color of the brush (random) G.drawline (Rom.nextint (), Rom.nextint (40),
Rom.nextint (M), Rom.nextint (40));/position is also random, x,y value does not exceed the rectangular box g.dispose (); Imageio.write (BFI, "JPEG", Res.getoutputstream ());//Picture with byte stream directly get}}<span style= "Font-family:arial, Helvetica, sans- Serif Background-color:rgb (255, 255, 255);
> </span>
Foreground code:
<%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%> <% String Path = Request.getcontextpath ()
;
String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/"; %> <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >
For the foreground code you need to explain:
we need to refresh when our verification code is passed over and we can't see it clearly. and the browser has the function of automatic memory, when no new parameters are passed in, the browser will not be refreshed, so we need to manually write a JS control parameters, we know, only time is not change, All of us use time to pass as parameters.
PS: The problem of their own pit for some time: the path of the verification code. The front-end "/" represents the Tomcat directory, within the project, such as Web.xml "/" to indicate the item. That is to say, they have a two-tier catalog.
Finally, attach yourself to the test of the time code and modify the number of the shape of the problem, such as 2d to the effect of more good. Have a very clear record.
Package cn.hncu.com;
Import Java.awt.Color;
Import Java.awt.Font;
Import Java.awt.Graphics;
Import Java.awt.Graphics2D;
Import Java.awt.geom.AffineTransform;
Import Java.awt.image.BufferedImage;
Import java.io.FileNotFoundException;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import Java.util.Random;
Import Javax.imageio.ImageIO;
Import Org.junit.Test;
public class Demoimg {@Test the public void Test () throws exception{String str= "9988";
int width=60;
int height=30;
The Graphics Object BufferedImage bfi=new bufferedimage (width, height, bufferedimage.type_int_bgr) are obtained by BufferedImage object;
Graphics G=bfi.getgraphics ();
g.DrawString (str, 10,10);
G.dispose ()///IO-like shutdown imageio.write (BFI, "JPEG", New FileOutputStream ("f:\\ex\\a.jpg"));
BFI is a canvas that writes the canvas to a file JPEG is the specified file format} @Test public void Test2 () throws exception{int width=100;
int height=40; BufferedImage BFI =new bufferedimage (width, height, bufferedimage.type_int_RGB); Graphics g=bfi.getgraphics ()///Get Graphics object can draw//1, set background (white box) G.setcolor (color.white);//White Brush g.fillrect (0, 0, W
Idth, height);
2, the specific generation of random number String str= "";//Save Data Random rom=new Random ();
Sets uppercase and coarse g.setfont for the font (new font ("a", font.bold,20)); Draw a specific picture for (int i=0;i<4;i++) {int num=rom.nextint (10);//generated random number G.setcolor (new Color (Rom.nextint (256), R Om.nextint (256), Rom.nextint (256));/Set the color of the brush (random) g.drawstring ("+num, 20*i, 20+rom.nextint (10)); The position of x increases the 20,y coordinates to 201 lines, online or offline//ps: The position needs to be explicit,}//Draw some interference line for (int i = 0; i < i++) {G.setcolor (n EW Color (rom.nextint (256), Rom.nextint (256), Rom.nextint (256));//Set the color of the brush (random) G.drawline (rom.nextint (100),
Rom.nextint (), Rom.nextint (), Rom.nextint (40));/position is also random, x,y values do not exceed the rectangular box g.dispose ();
Imageio.write (BFI, "JPEG", New FileOutputStream ("f:\\ex\\b.jpg")); //Draw the changeable case//font can rotate the verification code @Test public void Test3 () throws ioexception{int width= 100;
int height=40;
BufferedImage BFI =new bufferedimage (width, height, bufferedimage.type_int_rgb);
Graphics G=bfi.getgraphics ();
Graphics2D g2d= (graphics2d) G;
Random ROM =new Random (); G2d.setcolor (Color.White)//Set the color of the brush g2d.fillrect (0, 0, width, height);//Draw a white rectangle G2d.setfont ("a", Font.bol
D, 20));
for (int i=0;i<4;i++) {int num=rom.nextint (10);
Rotate, shrink affinetransform aff=new affinetransform (); Aff.rotate (Math.random (), i*18, height-20);/Rotate Aff.scale (0.6+math.random (), 0.6+math.random ());//Zoom G2d.setcolo
R (New Color (Rom.nextint (256), Rom.nextint (256), Rom.nextint (256)));
G2d.settransform (AFF);
G2d.drawstring ("" +num, I*18, height-25);
} g2d.dispose ();
Imageio.write (BFI, "JPEG", New FileOutputStream ("f:\\ex\\c.jpg")); }
}
The above is the basic process of Java generation Verification code, but also with its own test code, I hope that these can help everyone.