jcaptcha进阶

1.修改CaptchaServiceSingleton类,使用带参构造方法来创建DefaultManageableImageCaptchaService对象。

pic
这里我自己定制了一个验证码生成器MyImageEngine,代码如下:
package com.xhc.jcaptcha;

import com.octo.captcha.service.captchastore.FastHashMapCaptchaStore;
import com.octo.captcha.service.image.DefaultManageableImageCaptchaService;
import com.octo.captcha.service.image.ImageCaptchaService;

public class CaptchaServiceSingleton {
  private static ImageCaptchaService instance = new DefaultManageableImageCaptchaService(new FastHashMapCaptchaStore(), new MyImageEngine(), 180,
    100000, 75000);
     public static ImageCaptchaService getInstance(){
         return instance;
     }
}



2.MyImageEngine类

package com.xhc.jcaptcha;
import java.awt.Color;
import com.octo.captcha.component.image.backgroundgenerator.BackgroundGenerator;
import com.octo.captcha.component.image.backgroundgenerator.GradientBackgroundGenerator;
import com.octo.captcha.component.image.fontgenerator.FontGenerator;
import com.octo.captcha.component.image.fontgenerator.TwistedRandomFontGenerator;
import com.octo.captcha.component.image.textpaster.LineRandomTextPaster;
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.FileDictionary;
import com.octo.captcha.component.word.wordgenerator.ComposeDictionaryWordGenerator;
import com.octo.captcha.component.word.wordgenerator.WordGenerator;
import com.octo.captcha.engine.image.ListImageCaptchaEngine;
import com.octo.captcha.image.gimpy.GimpyFactory;
public class MyImageEngine extends ListImageCaptchaEngine {
 @Override
 protected void buildInitialFactories() {
  WordGenerator dictionnaryWords = new ComposeDictionaryWordGenerator(
    new FileDictionary("toddlist"));
 /* //BaffleRandomTextPaster类生成的是带干扰点的随机验证码贴纸,这里表示最少4个,最多5个字母,蓝色,1个干扰点,干扰点为黄色 
  TextPaster randomPaster = new BaffleRandomTextPaster(new Integer(4),
    new Integer(5), Color.blue, new Integer(1), Color.yellow);*/
  //LineRandomTextPaster类生成的是带干扰线的随机验证码贴纸。。。
  TextPaster randomPaster = new LineRandomTextPaster(new Integer(4),
    new Integer(5),Color.blue, new Integer(1),Color.pink);
 
 
  /*//背景大小及样式设置,UniColorBackgroundGenerator类生成的是统一背景,这里背景统一是lightGray颜色
  //宽度为180,高度为50。
  BackgroundGenerator back = new UniColorBackgroundGenerator(new Integer(
    180), new Integer(50), Color.lightGray);*/
 /* //FunkyBackgroundGenerator类生成的是炫酷背景
  BackgroundGenerator back = new  FunkyBackgroundGenerator(new Integer(
    180), new Integer(50));*/
 //GradientBackgroundGenerator类生成的是颜色渐变的背景
  BackgroundGenerator back = new  GradientBackgroundGenerator(new Integer(
    180), new Integer(50),Color.gray,Color.yellow);
 
 
 /* //字体设置,RandomFontGenerator类生成的是随机字体,这里字体最小为25px,最大为30px
  FontGenerator shearedFont = new RandomFontGenerator(new Integer(25),
    new Integer(30));*/
  //TwistedRandomFontGenerator类生成扭曲的字体
  FontGenerator shearedFont = new TwistedRandomFontGenerator(new Integer(25),
    new Integer(30));
 
 
  //生成验证码模板图片
  WordToImage word2image = new ComposedWordToImage(shearedFont, back,
    randomPaster);
 
  //把随机生成的验证码和模板放入工厂进行组装生成最终的验证码图片
  addFactory(new GimpyFactory(dictionnaryWords, word2image));
 }
}

3.jsp代码,web.xml配置,以及登陆校验的servlet与前一遍文章使用jcaptcha插件生成验证码代码一样,可以跳转到那里去查看代码。

4.效果图

captchacheck


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值