patchca验证码的使用

/**
* 生成验证码
*/
private static RandomFontFactory ff = null;
// 自定义验证码图片背景
private static MyCustomBackgroundFactory backgroundFactory = new MyCustomBackgroundFactory();
private static ConfigurableCaptchaService cs = new ConfigurableCaptchaService();
private static Random random = new Random();
static {
cs.setColorFactory(new ColorFactory() {
public Color getColor(int x) {
int[] c = new int[3];
int i = random.nextInt(c.length);
for (int fi = 0; fi < c.length; fi++) {
if (fi == i) {
c[fi] = random.nextInt(71);// 71
} else {
c[fi] = random.nextInt(256);// 256
}
}
return new Color(c[0], c[1], c[2]);
}
});
ff = new RandomFontFactory();
RandomWordFactory wf = new RandomWordFactory();
wf.setCharacters("abcdefghigkmnpqrstuvwxyzABCDEFGHIGKLMNPQRSTUVWXYZ1234567890");
wf.setMaxLength(5);
wf.setMinLength(4);
// 该代码经过测试 在后台修改字体大小不能自适应,只能在前台页面修改验证码高度和宽度
//设置验证码的背景颜色
cs.setBackgroundFactory(backgroundFactory);
cs.setWordFactory(wf);
cs.setFontFactory(ff);
}

protected void setResponseHeaders(HttpServletResponse response) {
response.setContentType("image/png");
response.setHeader("Cache-Control", "no-cache, no-store");
response.setHeader("Pragma", "no-cache");
long time = System.currentTimeMillis();
response.setDateHeader("Last-Modified", time);
response.setDateHeader("Date", time);
response.setDateHeader("Expires", time);
}

/**
*
* 自定义验证码图片背景,主要画一些噪点和干扰线
*/
private static class MyCustomBackgroundFactory implements BackgroundFactory {
private Random random = new Random();

public void fillBackground(BufferedImage image) {
Graphics graphics = image.getGraphics();
// 验证码图片的宽高
int imgWidth = image.getWidth();
int imgHeight = image.getHeight();
// 填充为白色背景
graphics.setColor(Color.lightGray);
graphics.fillRect(0, 0, imgWidth, imgHeight);
// 画100个噪点(颜色及位置随机)
for (int i = 0; i < 100; i++) {
// 随机颜色
int rInt = random.nextInt(255);
int gInt = random.nextInt(255);
int bInt = random.nextInt(255);
graphics.setColor(new Color(rInt, gInt, bInt));
// 随机位置
int xInt = random.nextInt(imgWidth - 3);

int yInt = random.nextInt(imgHeight - 2);
// 随机旋转角度
int sAngleInt = random.nextInt(360);
int eAngleInt = random.nextInt(360);
// 随机大小
int wInt = random.nextInt(6);
int hInt = random.nextInt(6);
graphics.fillArc(xInt, yInt, wInt, hInt, sAngleInt, eAngleInt);
// 画5条干扰线
if (i % 20 == 0) {
int xInt2 = random.nextInt(imgWidth);
int yInt2 = random.nextInt(imgHeight);
graphics.drawLine(xInt, yInt, xInt2, yInt2);
}
}
}
}

@RequestMapping("/safecode.do")
public void safecode(HttpServletRequest request,
HttpServletResponse response) throws IOException {
switch (random.nextInt(5)) {
case 0:
cs.setFilterFactory(new CurvesRippleFilterFactory(cs
.getColorFactory()));
break;
case 1:
cs.setFilterFactory(new MarbleRippleFilterFactory());
break;
case 2:
cs.setFilterFactory(new DoubleRippleFilterFactory());
break;
case 3:
cs.setFilterFactory(new WobbleRippleFilterFactory());
break;
case 4:
cs.setFilterFactory(new DiffuseRippleFilterFactory());
break;
}
HttpSession session = request.getSession(false);
if (session == null) {
session = request.getSession();
}
setResponseHeaders(response);
String randomCode = EncoderHelper.getChallangeAndWriteImage(cs, "png",
response.getOutputStream());
session.setAttribute("randomCode", randomCode);
}

上一篇:UVALive 7139 Rotation(矩阵前缀和)(2014 Asia Shanghai Regional Contest)


下一篇:深入解析js异步编程利器Generator