1. 程式人生 > >java工具類——驗證碼(VerifyCode)

java工具類——驗證碼(VerifyCode)

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();
	// 定義有那些字型
	private String[] fontNames = { "宋體", "華文楷體", "黑體", "微軟雅黑", "楷體_GB2312" };
	// 定義有那些驗證碼的隨機字元
	private String codes = "23456789abcdefghjkmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ";
	// 生成背景色
	private Color bgColor = new Color(250, 250, 250);
	// 用於gettext 方法 獲得生成的驗證碼文字
	private String text;

	// 生成隨機顏色
	private Color randomColor() {
		int red = r.nextInt(150);
		int green = r.nextInt(150);
		int blue = r.nextInt(150);
		return new Color(red, green, blue);
	}

	// 生成隨機字型
	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);
	}

	// 畫干擾線
	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));// 不知道
			g2.setColor(Color.blue);
			g2.drawLine(x1, y1, x2, y2);
		}
	}

	// 得到codes的長度內的隨機數 並使用charAt 取得隨機數位置上的codes中的字元
	private char randomChar() {
		int index = r.nextInt(codes.length());
		return codes.charAt(index);
	}

	// 建立一張驗證碼的圖片
	public BufferedImage createImage() {
		BufferedImage image = new BufferedImage(w, h,
				BufferedImage.TYPE_INT_RGB);
		Graphics2D g2 = (Graphics2D) image.getGraphics();
		StringBuilder sb = new StringBuilder();
		// 向圖中畫四個字元
		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);

		// 返回圖片
		return image;

	}

	// 得到驗證碼的文字 後面是用來和使用者輸入的驗證碼 檢測用
	public String getText() {
		return text;
	}

	// 定義輸出的物件和輸出的方向
	public static void output(BufferedImage bi, OutputStream fos)
			throws FileNotFoundException, IOException {
		ImageIO.write(bi, "JPEG", fos);
	}
}

以上,就生成了一個驗證碼。

 寫一個test,生成指定的驗證碼影象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"));
	}
}


當然,也可以在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());
	}

}

然後展示在.html或.jsp裡

<img src="VerifyCodeServlet.do">