码迷,mamicode.com
首页 > 其他好文 > 详细

kaptcha验证码

时间:2017-10-12 17:04:25      阅读:179      评论:0      收藏:0      [点我收藏+]

标签:nat   map   []   ase   ring   stat   cache   initial   stream   

 

@Action("/validimg")
public String validimg() throws Exception {
        genernateCaptchaImage();
        return null;
}

 

 

protected void genernateCaptchaImage() throws IOException {
        response.setHeader("Cache-Control", "no-store");
        response.setHeader("Pragma", "no-cache");
        response.setDateHeader("Expires", 0);
        response.setContentType("image/jpeg");
        ServletOutputStream out = response.getOutputStream();
        try {
            String captchaId = request.getSession(true).getId();
            LOGGER.info("当前生产验证码的sessionId为: "+captchaId);
            BufferedImage challenge = (BufferedImage) CaptchaServiceSingleton.getInstance().getChallengeForID(
                    captchaId, request.getLocale());
            ImageIO.write(challenge, "jpg", out);
            out.flush();
        } catch (CaptchaServiceException e) {
        } finally {
            out.close();
        }
    }

 

 

 

/**
 * JCaptcha的单例
 * 
 * @author fuxingxign
 */
public class CaptchaServiceSingleton {
    private static ImageCaptchaService instance = new DefaultManageableImageCaptchaService(
            new FastHashMapCaptchaStore(), new RdImageEngine(), 180, 100000, 75000);

    /*
     * private static ImageCaptchaService instance = new DefaultManageableImageCaptchaService( new
     * FastHashMapCaptchaStore(), new GMailEngine(), 180, 100000 , 75000);
     */
    public static ImageCaptchaService getInstance() {
        return instance;
    }
}

 

 

 

 

import java.awt.Color;
import java.awt.Font;

import com.octo.captcha.component.image.backgroundgenerator.BackgroundGenerator;
import com.octo.captcha.component.image.backgroundgenerator.UniColorBackgroundGenerator;
import com.octo.captcha.component.image.color.RandomRangeColorGenerator;
import com.octo.captcha.component.image.fontgenerator.FontGenerator;
import com.octo.captcha.component.image.fontgenerator.RandomFontGenerator;
import com.octo.captcha.component.image.textpaster.RandomTextPaster;
import com.octo.captcha.component.image.textpaster.TextPaster;
import com.octo.captcha.component.image.wordtoimage.ComposedWordToImage;
import com.octo.captcha.component.image.wordtoimage.WordToImage;
import com.octo.captcha.component.word.wordgenerator.RandomWordGenerator;
import com.octo.captcha.component.word.wordgenerator.WordGenerator;
import com.octo.captcha.engine.image.ListImageCaptchaEngine;
import com.octo.captcha.image.gimpy.GimpyFactory;

public class RdImageEngine extends ListImageCaptchaEngine {
    @Override
    protected void buildInitialFactories() {
        WordGenerator wgen = new RandomWordGenerator("abcdefghijklmnopqrstuvwxyz");
        RandomRangeColorGenerator cgen = new RandomRangeColorGenerator(new int[] { 0, 100 }, new int[] { 0, 100 },
                new int[] { 0, 100 });
        TextPaster textPaster = new RandomTextPaster(Integer.valueOf(4),Integer.valueOf(4), cgen, true);
        BackgroundGenerator backgroundGenerator = new UniColorBackgroundGenerator(Integer.valueOf(80), Integer.valueOf(37),
                Color.WHITE);
        Font[] fontsList = new Font[] { new Font("Arial", 0, 12), new Font("Tahoma", 0, 12),
                new Font("Verdana", 0, 12), };
        FontGenerator fontGenerator = new RandomFontGenerator(Integer.valueOf(26),Integer.valueOf(26), fontsList);

        WordToImage wordToImage = new ComposedWordToImage(fontGenerator, backgroundGenerator, textPaster);
        this.addFactory(new GimpyFactory(wgen, wordToImage));
    }

}

 

kaptcha验证码

标签:nat   map   []   ase   ring   stat   cache   initial   stream   

原文地址:http://www.cnblogs.com/shihaiming/p/7656685.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!