import java.awt.BasicStroke; import java.awt.Color; import Java.awt.Font; import
Java.awt.Graphics2D;
Import Java.awt.image.BufferedImage;
Import java.io.FileNotFoundException;
Import java.io.IOException;
Import Java.io.OutputStream;
Import Java.util.Random;
Import Javax.imageio.ImageIO; public class Verifycode {
private int w = 70;
private int h = 35;
Private Random R = new Random ();
Definition has those fonts private string[] FontNames = {"XXFarEastFont-Arial", "XXFarEastFont-Arial", "Boldface", "Microsoft Ya-hei", "italics _gb2312"};
Define random characters with those captcha code private String codes = "23456789abcdefghjkmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ";
Generate background color private color bgcolor = new color (250, 250, 250);
Used for the GetText method to obtain the generated CAPTCHA text private String text;
Generate random color private color randomcolor () {int red = R.nextint (150);
int green = R.nextint (150);
int blue = R.nextint (150);
return new Color (red, green, blue);
//Generate random Font private font randomfont () {int index = R.nextint (fontnames.length);
String fontname = Fontnames[index];
int style = R.nextint (4);
int size = R.nextint (5) + 24;
return new Font (FontName, style, size);
///Draw interference line private void DrawLine (bufferedimage image) {int num = 3;
graphics2d g2 = (graphics2d) image.getgraphics ();
for (int i = 0; i < num i++) {int x1 = r.nextint (w);
int y1 = R.nextint (h); int x2= R.nextint (w);
int y2 = r.nextint (h);
G2.setstroke (New Basicstroke (1.5F));/Do not know G2.setcolor (Color.Blue);
G2.drawline (x1, y1, x2, y2); }///Get the random number within the length of the codes and use Charat to get the character of the codes in the random number position private char Randomchar () {int index = R.nextint (Codes.length ()
);
Return Codes.charat (index); //Create a CAPTCHA picture public bufferedimage createimage () {bufferedimage image = new BufferedImage (W, H, BufferedImage.)
TYPE_INT_RGB);
graphics2d g2 = (graphics2d) image.getgraphics ();
StringBuilder sb = new StringBuilder ();
Draw four characters for (int i = 0; i < 4; i++) {String s = Randomchar () + "";
Sb.append (s);
float x = i * 1.0F * W/4;
G2.setfont (Randomfont ());
G2.setcolor (Randomcolor ());
G2.drawstring (S, x, h-5);
} This.text = Sb.tostring ();
DrawLine (image);
Returns the picture return image;
///Get the verification code of the text is used to check with the user input authentication code with public String GetText () {return text; }//Define output object and direction of output public static void output (BufferedImage bi, OutputStream fos) throws FileNotFoundException, IOException {imageio.write (bi, "JPEG", FOS); }
}
Above, a verification code is generated.
Write a test to generate the specified captcha image jpg
public class Test {public
static void Main (string[] args) throws IOException {
Verifycode code = new Verifycode () ;
BufferedImage image = Code.createimage ();
Imageio.write (image, "JPG", New File ("f:/image.jpg"));
}
Of course, it can also be generated in the servlet.
@WebServlet (urlpatterns= "/verifycodeservlet.do") public
class Verifycodeservlet extends HttpServlet {
@ Override
protected Void Service (HttpServletRequest request, httpservletresponse response)
throws Servletexception, IOException {
Verifycode code = new Verifycode ();
BufferedImage image = Code.createimage ();
Imageio.write (image, "JPG", Response.getoutputstream ());
}
And then show it in. html or. jsp