package com.saicent.servlet;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.servlet.*;
import javax.servlet.annotation.*;
import javax.servlet.http.*;
@SuppressWarnings("deprecation")
@WebServlet(
name = "MyServlet",
urlPatterns = {"/saicent"},
loadOnStartup = 1,
initParams = {
@WebInitParam(name="width", value="120"),
@WebInitParam(name="height", value="40"),
@WebInitParam(name="number_word", value="4"),
@WebInitParam(name="max_angle_of_character", value="45")
}
)
public class Saicent extends HttpServlet implements SingleThreadModel{
private static final long serialVersionUID = 1L;
//设定验证码图片的尺寸大小
private final int WIDTH = 120;
private final int HEIGHT = 40;
private final int NUMBER_WORD = 4;
private final int MAX_ANGLE_OF_CHARACTER = 45;
private final String BUFFER_STRING = "实打实的考虑房价来看我服你忘记烦恼我付款了无法看到苏发热分别就看你空间的考虑将覅来得及个人敏感快乐毛利率的私家车老师的那个人公开课";
private int yCoordinateOfWorld;
private int wordSpace;
private int sizeOfWord;
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//计算验证码图片的各种参数
initParamOfVerificationCode(HEIGHT, WIDTH, NUMBER_WORD);
System.out.println("yCoordinateOfWorld " + yCoordinateOfWorld);
System.out.println("wordSpace " + wordSpace);
System.out.println("sizeOfWord " + sizeOfWord);
//创建一个 BufferImage 的对象,他的父类是 Image.
BufferedImage bufferImage = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
//获得 BufferImage 的图形,以操作并绘制这张图片.
Graphics graphic = bufferImage.getGraphics();
//注意:绘制图形,每次描绘图形前,都要设置画笔的颜色
//设置背景颜色
graphic.setColor(Color.WHITE);
graphic.fillRect(0, 0, WIDTH, HEIGHT);
//设置边框的颜色
graphic.setColor(Color.GRAY);
graphic.drawRect(1, 1, WIDTH - 2, HEIGHT - 2);
//设置文字字体以及文字的样式
graphic.setColor(Color.GRAY);
graphic.setFont(new Font("宋体", Font.BOLD | Font.ITALIC, (int)sizeOfWord));
//保存发送给客户端的实际字符串
String keyString = new String();
for(int i = 0; i < NUMBER_WORD; i++) {
//随机获得一个字符
char character = BUFFER_STRING.charAt( new Random().nextInt(BUFFER_STRING.length()) );
//随机获得文字旋转的角度
double characterAngle = (new Random().nextInt() % MAX_ANGLE_OF_CHARACTER) * Math.PI / 180;
((Graphics2D)graphic).rotate(characterAngle, (wordSpace * (1 + i) + i * sizeOfWord + sizeOfWord / 2), yCoordinateOfWorld - sizeOfWord / 2);
graphic.drawString(character + "", (wordSpace * (1 + i) + i * sizeOfWord), yCoordinateOfWorld);
((Graphics2D)graphic).rotate(-characterAngle, (wordSpace * (1 + i) + i * sizeOfWord + sizeOfWord / 2), yCoordinateOfWorld - sizeOfWord / 2);
keyString = keyString + character;
}
//设置干扰线的样式
graphic.setColor(Color.GRAY);
for(int i = 0; i < 7; i ++) {
int x1 = new Random().nextInt(WIDTH);
int x2 = new Random().nextInt(WIDTH);
int y1 = new Random().nextInt(HEIGHT);
int y2 = new Random().nextInt(HEIGHT);
graphic.drawLine(x1, y1, x2, y2);
}
//输出图片
HttpSession session = request.getSession();
session.setAttribute("key", keyString);
ImageIO.write(bufferImage, "jpg", response.getOutputStream());
response.setContentType("image/jpeg");
System.out.println(keyString);
System.out.println(session.getAttribute("key"));
}
protected void initParamOfVerificationCode(int height, int width, int numberOfWord) {
/*
* 方法摘要
* 需要计算的数据:
* 1. 文字的 y 坐标
* 2. 文字的间距
* 3. 文字的尺寸
*
* 设计方案:
* 1. 文字的高度要占整张图片的上下边距占文字的 1/3
* 2. 文字的间距要是文字的 1/3
*
* 需要注意的细节:
* 1. 坐标系 x, y 的零点在左上角。向下 y 增大; 向右 x 增大。
* 2. 文字的重心在左下角。
*
*/
sizeOfWord = (int)Math.rint(3* width / (4 * numberOfWord + 1));
wordSpace = (int)Math.rint(width / (4 * numberOfWord + 1));
yCoordinateOfWorld = (int)Math.rint((HEIGHT - sizeOfWord) / 2 + sizeOfWord);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
HttpSession session = request.getSession();
response.setContentType("text/html; utf-8");
response.setCharacterEncoding("utf-8");
request.setCharacterEncoding("utf-8");
if(session.getAttribute("key") != null && session.getAttribute("key").equals(request.getParameter("key")))
{
response.getWriter().println( "WELCOME!" );
}
else
{
response.getWriter().println( "验证码出错!" );
}
}
@Override
protected void service(HttpServletRequest arg0, HttpServletResponse arg1)
throws ServletException, IOException {
super.service(arg0, arg1);
System.out.println("Method of service is called!");
}
}