Java CAPTCHA Code:
Package cn.itcast.image;
Import Java.awt.BasicStroke;
Import Java.awt.Color;
Import Java.awt.Font;
Import Java.awt.Graphics2D;
Import Java.awt.image.BufferedImage;
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 ();
{"Arial", "Chinese Italic", "Black Body", "Chinese New Wei", "Chinese Script", "Microsoft Ya Hei", "Italics _gb2312"}
Private string[] FontNames = {"Arial", "Chinese Italic", "Black Body", "Microsoft Jas", "Italics _gb2312"};
Optional characters
Private String codes = "23456789abcdefghjkmnopqrstuvwxyzABCDEFGHJKMNPQRSTUVWXYZ";
Background color
Private color BgColor = new color (255, 255, 255);
Text on the Verification code
private String text;
Generate a 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 a random font
Private Font Randomfont () {
int index = R.nextint (fontnames.length);
String FontName = fontnames[index];//generates a random font name
int style = R.nextint (4);//generate random style, 0 (no style), 1 (bold), 2 (Italic), 3 (bold + italic)
int size = R.nextint (5) + 24; Generate random font size, 24 ~ 28
return new Font (FontName, style, size);
}
Draw interference Lines
private void DrawLine (BufferedImage image) {
int num = 3;//altogether draws 3 strips
graphics2d g2 = (graphics2d) image.getgraphics ();
for (int i = 0; i < num; i++) {//Generate two point coordinates, i.e. 4 values
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));
G2.setcolor (Color.Blue); The interference line is blue
G2.drawline (x1, y1, x2, y2);//Draw Line
}
}
Randomly generate one character
Private char Randomchar () {
int index = R.nextint (Codes.length ());
Return Codes.charat (index);
}
Create BufferedImage
Private BufferedImage createimage () {
BufferedImage image = New BufferedImage (W, H, Bufferedimage.type_int_rgb);
graphics2d g2 = (graphics2d) image.getgraphics ();
G2.setcolor (This.bgcolor);
G2.fillrect (0, 0, W, h);
return image;
}
Call this method to get the verification code
Public BufferedImage GetImage () {
BufferedImage image = CreateImage ();//Create Picture buffer
graphics2d g2 = (graphics2d) image.getgraphics ();//Get Drawing environment
StringBuilder sb = new StringBuilder ();//used to load generated captcha text
Draw 4 characters into a picture
for (int i = 0; i < 4; i++) {//Cycles four times, one character at a time
String s = Randomchar () + "";//random generation of one letter
Sb.append (s); Add the letters to SB
float x = i * 1.0F * W/4; Sets the x-axis coordinates of the current character
G2.setfont (Randomfont ()); Set Random Font
G2.setcolor (Randomcolor ()); Set Random Colors
G2.drawstring (S, x, h-5); Drawing
}
This.text = Sb.tostring (); The generated string is assigned to the This.text
DrawLine (image); Adding interference lines
return image;
}
Returns the text on the captcha picture
Public String GetText () {
return text;
}
Save the picture to the specified output stream
public static void output (BufferedImage image, outputstream out)
Throws IOException {
Imageio.write (Image, "JPEG", out);
}
}
Jar Making of Java Verification code