首页 > 代码库 > jcaptcha进阶
jcaptcha进阶
1.改动CaptchaServiceSingleton类。使用带參构造方法来创建DefaultManageableImageCaptchaService对象。
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.效果图
jcaptcha进阶