Servlet验证码

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.geom.Line2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.sun.image.codec.jpeg.ImageFormatException;

public class ValidateImageServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	private Font mFont = new Font("Consolas", Font.PLAIN, 30);

	public char[] c = { 'A', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L',
			'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
			'a', 'c', 'd', 'e', 'f', 'g', 'h', 'j', 'k', 'm', 'n', 'p',
			'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', };

//	public String[] rBase = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
//			"a", "b", "c", "d", "e", "f" };

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException {
		response.setHeader("Pragma", "No-cache");
		response.setHeader("Cache-Control", "No-cache");

		response.setContentType("image/jpg");
		int width = 120;
		int height = 30;
		BufferedImage image = new BufferedImage(width, height,
				BufferedImage.TYPE_INT_RGB);
		Graphics g = image.getGraphics();
		Graphics2D g2d = (Graphics2D) g;
		Random random = new Random();
		g.setColor(getRandColor(200, 250));
		g.fillRect(0, 0, width, height);
		
		g.setColor(getRandColor(200, 250));
		for (int i = 0; i < 10; i++) {
			int x = random.nextInt(width - 1);
			int y = random.nextInt(height - 1);
			int x1 = random.nextInt(6) + 1;
			int y1 = random.nextInt(12) + 1;
			Line2D line = new Line2D.Double(x, y, x + x1, y + y1);
			g2d.draw(line);
		}

		g.setColor(Color.DARK_GRAY);
		int[] xPoints = new int[3];
		int[] yPoints = new int[3];
		for (int j = 0; j < 3; j++) {
			xPoints[j] = random.nextInt(width - 1);
			yPoints[j] = random.nextInt(height - 1);
		}
		g.drawPolyline(xPoints, yPoints, 3);

		g.setFont(mFont);
		String sRand = "";
		String rand = "";
		int itmp;
		for (int i = 0; i < 4; i++) {
			switch (random.nextInt(4)) {
			case 1: {
				rand = String.valueOf(c[random.nextInt(c.length)]);
				break;
			}
			default: {
				itmp = random.nextInt(9) + 49;
				rand = String.valueOf((char) itmp);
				break;
			}
			}
			sRand += rand;
			Color color = new Color(20 + random.nextInt(110), 20 + random.nextInt(110), 20 + random.nextInt(110));
			g.setColor(color);
//			AffineTransform trans = new AffineTransform();
//			trans.rotate(random.nextInt(18) * 3.14 / 180, 10 * i + 10, 7);
//			float scaleSize = random.nextFloat() + 0.8f;
//			if (scaleSize > 0.9f)
//				scaleSize = 0.9f;
//			trans.scale(scaleSize, scaleSize);
//			g2d.setTransform(trans);
			g.drawString(rand, 20 * i + 20, 19);
		}
		HttpSession session = request.getSession(true);
		session.setAttribute("validateImageCode", sRand.toLowerCase());
		g.dispose();
		try {
			ImageIO.write(image, "JPEG", response.getOutputStream());
		} catch (ImageFormatException ex1) {
		} catch (IOException ex1) {
			ex1.printStackTrace();
		}
	}

	private Color getRandColor(int fc, int bc) {
		Random random = new Random();
		if (fc > 255) {
			fc = 255;
		}
		if (bc > 255) {
			bc = 255;
		}
		int r = fc + random.nextInt(bc - fc);
		int g = fc + random.nextInt(bc - fc);
		int b = fc + random.nextInt(bc - fc);
		return new Color(r, g, b);
	}

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值