package com.letousky.lottery.util;

import java.io.*;
import java.util.*;
import javax.imageio.ImageIO;
import javax.servlet.*;
import javax.servlet.http.*;
import java.awt.*;
import java.awt.image.*;

//import org.apache.log4j.Logger;

//import com.mlot.aplc.util.Symbols;

public class ValidateCode extends HttpServlet {

	// private final static Logger __logger =
	// Logger.getLogger(ValidateCode.class);
	private final static String FONT = "����";
	private final static String REQUEST_TYPE = "image/gif";

	private Font font = new Font(FONT, Font.PLAIN, 12);// ��������

	public void init() throws ServletException {
	}

	private static final String CONTENT_TYPE = "image/jpeg; charset=UTF-8";

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

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType(CONTENT_TYPE);
		// ����ҳ�治����

		HttpSession session = request.getSession();

		response.setHeader("Pragma", "No-cache");
		response.setHeader("Cache-Control", "no-cache");
		response.setDateHeader("Expires", 0);
		// ���ڴ��д���ͼ��
		int width = 60, height = 20;
		BufferedImage image = new BufferedImage(width, height,
				BufferedImage.TYPE_INT_RGB);
		// ��ȡͼ��������
		Graphics g = image.getGraphics();
		// �趨����ɫ
		g.setColor(getRandColor(155, 254));
		// g.setColor(new Color(255,255,255));
		g.fillRect(0, 0, width, height);
		// �趨����
		g.setFont(new Font("Times New Roman", Font.PLAIN, 18));
		// ���߿�
		// g.setColor(new Color());
		// g.drawRect(0,0,width-1,height-1);
		// ������155������ߣ�ʹͼ���е���֤�벻�ױ��������̽�⵽
		g.setColor(getRandColor(160, 220));

		// ��������
		Random random = new Random();

		for (int i = 0; i < 155; i++) {
			int x = random.nextInt(width);
			int y = random.nextInt(height);
			int xl = random.nextInt(12);
			int yl = random.nextInt(12);
			g.drawLine(x, y, x + xl, y + yl);
		}
		// ȡ���������֤��(4λ����)
		String sRand = "";
		for (int i = 0; i < 4; i++) {
			String rand = String.valueOf(random.nextInt(10));
			sRand += rand;
			// ����֤����ʾ��ͼ����
			g.setColor(new Color(20 + random.nextInt(110), 20 + random
					.nextInt(110), 20 + random.nextInt(110)));// ���ú����4����ɫ��ͬ����������Ϊ����̫�ӽ�����ֻ��ֱ�����
			g.drawString(rand, 13 * i + 6, 16);
		}
		// ����֤�����SESSION
		session.setAttribute("validateCode", sRand);
		// ͼ����Ч
		g.dispose();
		// ���ͼ��ҳ��
		ImageIO.write(image, "JPEG", response.getOutputStream());

		/*
		 * ע��ImageIO.write(image, "JPEG", response.getOutputStream());
		 * ���ʾ���ǰ�servlet��������4��һ�������
		 */
	}

	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);
	}

	// Clean up resources
	public void destroy() {
		this.font = null;
	}
}
