packagecom.ninemax.cul.servlet;
importjava.awt.Color;
importjava.awt.Graphics;
importjava.awt.image.BufferedImage;
importjava.awt.image.BufferedImageOp;
importjava.io.IOException;
importjava.io.OutputStream;
importjava.util.ArrayList;
importjava.util.List;
importjava.util.Random;
importjavax.imageio.ImageIO;
importjavax.servlet.ServletException;
importjavax.servlet.http.HttpServlet;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
importjavax.servlet.http.HttpSession;
importorg.patchca.background.BackgroundFactory;
importorg.patchca.color.ColorFactory;
importorg.patchca.color.RandomColorFactory;
importorg.patchca.filter.ConfigurableFilterFactory;
importorg.patchca.filter.library.AbstractImageOp;
importorg.patchca.filter.library.WobbleImageOp;
importorg.patchca.font.RandomFontFactory;
importorg.patchca.service.Captcha;
importorg.patchca.service.ConfigurableCaptchaService;
importorg.patchca.text.renderer.BestFitTextRenderer;
importorg.patchca.text.renderer.TextRenderer;
importorg.patchca.word.RandomWordFactory;
/**
* 验证码生成类
*
* 使用开源验证码项目patchca生成
* 依赖jar包:patchca-0.5.0.jar
* 项目网址:https://code.google.com/p/patchca/
*
* @author zyh
* @version 1.00 2012-7-12 New
*/
publicclassValidationCodeServletextendsHttpServlet {
privatestaticfinallongserialVersionUID = 5126616339795936447L;
privateConfigurableCaptchaService configurableCaptchaService =null;
privateColorFactory colorFactory =null;
privateRandomFontFactory fontFactory =null;
privateRandomWordFactory wordFactory =null;
privateTextRenderer textRenderer =null;
publicValidationCodeServlet() {
super();
}
/**
* Servlet销毁方法,负责销毁所使用资源.
*/
publicvoiddestroy() {
wordFactory = null;
colorFactory = null;
fontFactory = null;
textRenderer = null;
configurableCaptchaService = null;
super.destroy();// Just puts "destroy" string in log
}
publicvoiddoGet(HttpServletRequest request, HttpServletResponse response)
throwsServletException, IOException {
doPost(request, response);
}
publicvoiddoPost(HttpServletRequest request, HttpServletResponse response)
throwsServletException, IOException {
response.setContentType("image/png");
response.setHeader("cache","no-cache");
HttpSession session = request.getSession(true);
OutputStream outputStream = response.getOutputStream();
// 得到验证码对象,有验证码图片和验证码字符串
Captcha captcha = configurableCaptchaService.getCaptcha();
// 取得验证码字符串放入Session
String validationCode = captcha.getChallenge();
session.setAttribute("validationCode", validationCode);
// 取得验证码图片并输出
BufferedImage bufferedImage = captcha.getImage();
ImageIO.write(bufferedImage, "png", outputStream);
outputStream.flush();
outputStream.close();
}
/**
* Servlet初始化方法
*/
publicvoidinit()throwsServletException {
configurableCaptchaService = newConfigurableCaptchaService();
// 颜色创建工厂,使用一定范围内的随机色
colorFactory = newRandomColorFactory();
configurableCaptchaService.setColorFactory(colorFactory);
// 随机字体生成器
fontFactory = newRandomFontFactory();
fontFactory.setMaxSize(32);
fontFactory.setMinSize(28);
configurableCaptchaService.setFontFactory(fontFactory);
// 随机字符生成器,去除掉容易混淆的字母和数字,如o和0等
wordFactory = newRandomWordFactory();
wordFactory.setCharacters("abcdefghkmnpqstwxyz23456789");
wordFactory.setMaxLength(5);
wordFactory.setMinLength(4);
configurableCaptchaService.setWordFactory(wordFactory);
// 自定义验证码图片背景
MyCustomBackgroundFactory backgroundFactory = newMyCustomBackgroundFactory();
configurableCaptchaService.setBackgroundFactory(backgroundFactory);
// 图片滤镜设置
ConfigurableFilterFactory filterFactory = newConfigurableFilterFactory();
List filters = newArrayList();
WobbleImageOp wobbleImageOp = newWobbleImageOp();
wobbleImageOp.setEdgeMode(AbstractImageOp.EDGE_MIRROR);
wobbleImageOp.setxAmplitude(2.0);
wobbleImageOp.setyAmplitude(1.0);
filters.add(wobbleImageOp);
filterFactory.setFilters(filters);
configurableCaptchaService.setFilterFactory(filterFactory);
// 文字渲染器设置
textRenderer = newBestFitTextRenderer();
textRenderer.setBottomMargin(3);
textRenderer.setTopMargin(3);
configurableCaptchaService.setTextRenderer(textRenderer);
// 验证码图片的大小
configurableCaptchaService.setWidth(82);
configurableCaptchaService.setHeight(32);
}
/**
* 自定义验证码图片背景,主要画一些噪点和干扰线
*/
privateclassMyCustomBackgroundFactoryimplementsBackgroundFactory {
privateRandom random =newRandom();
publicvoidfillBackground(BufferedImage image) {
Graphics graphics = image.getGraphics();
// 验证码图片的宽高
intimgWidth = image.getWidth();
intimgHeight = image.getHeight();
// 填充为白色背景
graphics.setColor(Color.WHITE);
graphics.fillRect(0,0, imgWidth, imgHeight);
// 画100个噪点(颜色及位置随机)
for(inti =0; i <100; i++) {
// 随机颜色
intrInt = random.nextInt(255);
intgInt = random.nextInt(255);
intbInt = random.nextInt(255);
graphics.setColor(newColor(rInt, gInt, bInt));
// 随机位置
intxInt = random.nextInt(imgWidth -3);
intyInt = random.nextInt(imgHeight -2);
// 随机旋转角度
intsAngleInt = random.nextInt(360);
inteAngleInt = random.nextInt(360);
// 随机大小
intwInt = random.nextInt(6);
inthInt = random.nextInt(6);
graphics.fillArc(xInt, yInt, wInt, hInt, sAngleInt, eAngleInt);
// 画5条干扰线
if(i %20==0) {
intxInt2 = random.nextInt(imgWidth);
intyInt2 = random.nextInt(imgHeight);
graphics.drawLine(xInt, yInt, xInt2, yInt2);
}
}
}
}
}