您的位置:首页 > 编程语言 > Java开发

java 验证码之 patchca

2015-08-01 13:28 701 查看
patchca

jar下载地址:http://code.google.com/p/patchca/downloads/list

源码下载地址:https://github.com/pusuo/patchca

测试结果:较理想

一、测试

测试后查看项目的根目录

public static void main(String[] args) throws IOException {

ConfigurableCaptchaService cs = new ConfigurableCaptchaService();
cs.setColorFactory(new SingleColorFactory(new Color(25, 60, 170)));
cs.setFilterFactory(new CurvesRippleFilterFactory(cs.getColorFactory()));

FileOutputStream fos = new FileOutputStream("patcha_demo.png");
String validate_code = EncoderHelper.getChallangeAndWriteImage(cs, "png", fos);
System.out.println("*****************"+validate_code+"*****************");
fos.close();
}


二、springMVC 的使用

/**
* 获取验证码
*
* @param request
* @param response
* @throws IOException
*/
@RequestMapping(value = "/init/InitRegValCode", produces = "text/plain;charset=UTF-8")
@ResponseBody
public void InitRegValCode(HttpServletRequest request,
HttpServletResponse response) throws IOException {
ConfigurableCaptchaService cs = new ConfigurableCaptchaService();
cs.setColorFactory(new SingleColorFactory(new Color(25, 60, 170)));
cs.setFilterFactory(new CurvesRippleFilterFactory(cs.getColorFactory()));
RandomFontFactory ff = new RandomFontFactory();
ff.setMinSize(30);
ff.setMaxSize(30);
RandomWordFactory rwf = new RandomWordFactory();
rwf.setMinLength(4);
rwf.setMaxLength(4);
cs.setWordFactory(rwf);
cs.setFontFactory(ff);
cs.setHeight(30);
cs.setWidth(140);

try {

ServletOutputStream stream = response.getOutputStream();
String validate_code = EncoderHelper.getChallangeAndWriteImage(cs,
"png", stream);

request.getSession().setAttribute("REG_VAL_CODE", validate_code);

stream.flush();
stream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: