利用patchca生成验证码

来源:互联网 发布:淘宝大学vip课有效果吗 编辑:程序博客网 时间:2024/06/02 13:19

patchca 是一个简单但功能强大的验证码的Java类库。

在springMVC 中的使用
    /**     * 验证码     *     * @param width  图片宽度     * @param height 图片高度     * @param number 验证码数量     */    @RequestMapping(value = {"/captcha?w={width}&h={height}&n={number}"})    public void captcha(HttpServletRequest request,                        HttpServletResponse response, @PathVariable int width, @PathVariable int height, @PathVariable int number) 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(number);        rwf.setMaxLength(number);        cs.setWordFactory(rwf);        cs.setFontFactory(ff);        cs.setHeight(height);        cs.setWidth(width);        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);                ServletOutputStream stream = response.getOutputStream();        String validate_code = EncoderHelper.getChallangeAndWriteImage(cs,                "png", stream);        request.getSession().setAttribute("REG_VAL_CODE", validate_code);        stream.flush();        stream.close();    }

jar下载地址:http://code.google.com/p/patchca/downloads/list 
源码下载地址:https://github.com/pusuo/patchca 


2 0