JCaptchaitjob - 千亿集团

JCaptchaitjob

2019-02-09 08:05:50 | 作者: 飞荷 | 标签: 验证码,图片,款式 | 浏览: 8240

import com.octo.captcha.component.image.backgroundgenerator.BackgroundGenerator; import com.octo.captcha.component.image.backgroundgenerator.UniColorBackgroundGenerator; import com.octo.captcha.component.image.color.RandomListColorGenerator; import com.octo.captcha.component.image.deformation.ImageDeformation; import com.octo.captcha.component.image.deformation.ImageDeformationByFilters; import com.octo.captcha.component.image.fontgenerator.FontGenerator; import com.octo.captcha.component.image.fontgenerator.RandomFontGenerator; import com.octo.captcha.component.image.textpaster.DecoratedRandomTextPaster; import com.octo.captcha.component.image.textpaster.TextPaster; import com.octo.captcha.component.image.textpaster.textdecorator.TextDecorator; import com.octo.captcha.component.image.wordtoimage.DeformedComposedWordToImage; 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; * 模仿JCaptcha2.0编写GMail验证码款式的图片引擎. * @author calvin public class GMailEngine extends ListImageCaptchaEngine { @Override protected void buildInitialFactories() { int minWordLength = 4; int maxWordLength = 5; int fontSize = 50; int imageWidth = 250; int imageHeight = 100; //word generator WordGenerator dictionnaryWords = new ComposeDictionaryWordGenerator(new FileDictionary("toddlist")); //word2image components TextPaster randomPaster = new DecoratedRandomTextPaster(minWordLength, maxWordLength, new RandomListColorGenerator(new Color[] { new Color(23, 170, 27), new Color(220, 34, 11), new Color(23, 67, 172) }), new TextDecorator[] {}); BackgroundGenerator background = new UniColorBackgroundGenerator(imageWidth, imageHeight, Color.white); FontGenerator font = new RandomFontGenerator(fontSize, fontSize, new Font[] { new Font("nyala", Font.BOLD, fontSize), new Font("Bell MT", Font.PLAIN, fontSize), new Font("Credit valley", Font.BOLD, fontSize) }); ImageDeformation postDef = new ImageDeformationByFilters(new ImageFilter[] {}); ImageDeformation backDef = new ImageDeformationByFilters(new ImageFilter[] {}); ImageDeformation textDef = new ImageDeformationByFilters(new ImageFilter[] {}); WordToImage word2image = new DeformedComposedWordToImage(font, background, randomPaster, backDef, textDef, postDef); addFactory(new GimpyFactory(dictionnaryWords, word2image)); }

 发生图片单例:

 

 

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 GMailEngine(), 180, 100000 , 75000);
// private static ImageCaptchaService instance=new DefaultManageableImageCaptchaService();
 public static ImageCaptchaService getInstance(){
 return instance;
}

 SpringMVC:Controller:

 

 

@RequestMapping(value="getCaptcha",headers = "Accept=image/jpeg, image/jpg, image/png, image/gif") //,produces = "image/jpg"
 public @ResponseBody byte[] getCaptcha(HttpServletRequest request,HttpServletResponse response) throws Exception {
 String captchaId = request.getSession(true).getId();
 BufferedImage challenge = (BufferedImage)CaptchaServiceSingleton.getInstance().getChallengeForID(captchaId, request.getLocale());
 ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
 ImageIO.write(challenge, "jpg", byteArrayOutputStream);
 return byteArrayOutputStream.toByteArray();
 }

 页面:

 

 label 验证码: /label 
 input name="jcaptcha_code" type="text" size="6" / 
 span img id="img_jcaptcha" src="${ctx}/login/getCaptcha.htm" width="75" height="24" / /span 
 script type="text/javascript" 
function changeJcaptchaSrc(){
 document.getElementById("img_jcaptcha").src=${ctx}/login/getCaptcha.htm?_=+(new Date()).getTime();
 /script 

 

版权声明
本文来源于网络,版权归原作者所有,其内容与观点不代表千亿集团立场。转载文章仅为传播更有价值的信息,如采编人员采编有误或者版权原因,请与我们联系,我们核实后立即修改或删除。

猜您喜欢的文章