java中文验证码

JSP中文验证码页面,java验证码,中文验证码



<%@ page pageEncoding="gb2312" contentType="image/jpeg"
	import="javax.imageio.*,java.util.*,java.awt.image.*,java.awt.*"%>
<%!
	Color getRandColor(Random random, int ff, int cc) {
		if (ff > 255)
			ff = 255;
		if (cc > 255)
			cc = 255;
		int r = ff + random.nextInt(cc - ff);
		int g = ff + random.nextInt(cc - ff);
		int b = ff + random.nextInt(cc - ff);
		return new Color(r, g, b);
	}%>
<%
	
	response.setHeader("Pragma", "No-cache");
	response.setHeader("Cache-Control", "no-cache");
	response.setDateHeader("Expires", 0);
	int width = 130;
	int height = 30;
	
	String base = "\u51ac\u5929\u7684\u592a\u9633\u6652\u5728\u8eab\u4e0a\u8ba9\u4eba\u72af\u56f0\u772f\u7740\u773c\u775b\u8111\u5b50\u6ca1\u6709\u95f2\u7740\u770b\u4f3c\u7761\u7740\u4e86\u5176\u5b9e\u8111\u6d77\u91cc\u9762\u50cf\u653e\u7535\u5f71\u4e00\u6837\u5728\u56de\u653e\u7740\u4ee5\u524d\u751f\u6d3b\u91cc\u9762\u7684\u70b9\u70b9\u6ef4\u6ef4\u4e0d\u77e5\u9053\u73b0\u5728\u56de\u653e\u5230\u54ea\u4e86\u7a81\u7136\u4e4b\u95f4\u60f3\u8d77\u4e86\u4ee5\u524d\u5728\u6211\u6700\u56f0\u96be\u7684\u65f6\u5019\u5411\u6211\u4f38\u8fc7\u624b\u81c2\u7684\u670b\u53cb\u60f3\u8d77\u4e86\u7ed9\u6211\u7a9d\u5934\u5403\u7684\u90a3\u4e9b\u670b\u53cb\u597d\u4e45\u6ca1\u6709\u8054\u7cfb\u5230\u4e86\u4e0d\u77e5\u9053\u5728\u8fd9\u4e2a\u591a\u53d8\u7684\u51ac\u5929\u4f60\u4eec\u597d\u5417\u4eba\u597d\u50cf\u5c31\u662f\u8fd9\u6837\u5bf9\u5e2e\u8fc7\u81ea\u5df1\u7684\u4eba\u603b\u662f\u8fd9\u6837\u96be\u5fd8\u5bf9\u548c\u81ea\u5df1\u4e00\u8d77\u8d70\u8fc7\u56f0\u96be";
	
	int length = base.length();
	BufferedImage image = new BufferedImage(width, height,
			BufferedImage.TYPE_INT_RGB);
	Graphics g = image.getGraphics();
	Random random = new Random();
	g.setColor(getRandColor(random, 188, 235));
	g.fillRect(0, 0, width, height);
	String[] fontTypes = { "\u5b8b\u4f53", "\u65b0\u5b8b\u4f53","\u9ed1\u4f53", "\u6977\u4f53", "\u96b6\u4e66" };
	int fontTypesLength = fontTypes.length;
	
	g.setColor(getRandColor(random, 180, 199));
	g.setFont(new Font("Times New Roman", Font.PLAIN, 14));
	for (int i = 0; i < 4; i++) {
		g.drawString("@*@*@*@*@*@*@*", 0, 5 * (i + 2));
	}
	String sRand = "";
	for (int i = 0; i < 4; i++) {
		int start = random.nextInt(length);
		String rand = base.substring(start, start + 1);
		sRand += rand;
		g.setColor(getRandColor(random, 10, 150));
		g.setFont(new Font(fontTypes[random.nextInt(fontTypesLength)],	Font.BOLD, 18 + random.nextInt(6)));
		
		g.drawString(rand, 24 * i + 10 + random.nextInt(8), 24);
	}
	session.setAttribute("rand", sRand);
	g.dispose();
	
	ImageIO.write(image, "JPEG", response.getOutputStream());

	out.clear();
	out = pageContext.pushBody();
%>


你可能感兴趣的:(Java)