如何将现有的程序改成算数验证码?

发布于 2021-12-05 10:55:36 字数 4986 浏览 675 评论 3

我这个属于二次开发,这段代码暂时还不懂,现需要将现有代码改成算式验证码,该如何更改?  之前用的验证码都是用graphics这个画的,但是这个需要存放session,在我们系统,不知道怎么回事,session会销毁,所以就在原有代码上修改了,下面这段代码该怎样改成算式验证码???10以内加减那种

package com.auchan.captcha.util;


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 captchaService1 = new DefaultManageableImageCaptchaService(
	          new FastHashMapCaptchaStore(),
	    		  new MyImageCaptchaEngine(),
	    		  180,
	    		  100000,
	    		  75000
	      );
 
    public static ImageCaptchaService getInstance(){
    	 if (captchaService1==null){
    	      captchaService1=new DefaultManageableImageCaptchaService(
    	          new FastHashMapCaptchaStore(),
    	    		  new MyImageCaptchaEngine(),
    	    		  180,
    	    		  100000,
    	    		  75000
    	      );
    	    }
    	    return captchaService1;
    }
}

package com.auchan.captcha.util;

import java.awt.Color;
import java.awt.Font;
import java.awt.image.ImageFilter;
import java.util.Random;

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.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.BaffleTextDecorator;
import com.octo.captcha.component.image.textpaster.textdecorator.LineTextDecorator;
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.wordgenerator.RandomWordGenerator;
import com.octo.captcha.component.word.wordgenerator.WordGenerator;
import com.octo.captcha.engine.image.ListImageCaptchaEngine;

public class MyImageCaptchaEngine extends ListImageCaptchaEngine {
	//验证码的Key
    private static Random random = new Random();  
  @Override
  protected void buildInitialFactories() {
    WordGenerator wgen = new RandomWordGenerator("abcdefghijklmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ123456789");
    RandomRangeColorGenerator cgen = new RandomRangeColorGenerator(  
    		 new int[] { 0, 150 }, new int[] { 0, 150 },  
           new int[] { 0, 150 });  
   RandomRangeColorGenerator lineColors = new RandomRangeColorGenerator(  
          new int[] { 150, 255 }, new int[] { 150, 255 }, new int[] {  
                  150, 255 });  
   BaffleTextDecorator baffleTextDecorator = new BaffleTextDecorator(2,cgen);//气泡干扰  
    TextPaster textPaster = new DecoratedRandomTextPaster(new Integer(4),  
              new Integer(4), cgen, true,  
              new TextDecorator[] {  new BaffleTextDecorator(new Integer(0), Color.black) }); 
    LineTextDecorator lineTextDecorator = new LineTextDecorator(1,cgen);//曲线干扰  
    TextDecorator[] textDecorators = new TextDecorator[1];  
    Color c = getRandColor(200, 250);  
    Color c1 = getRandColor(160, 200); 
  //过滤器  
   BackgroundGenerator backgroundGenerator = new UniColorBackgroundGenerator(new Integer(140), new Integer(40),new Color(238,238,238));
   ImageDeformation postDef = new ImageDeformationByFilters(new ImageFilter[] {});  
   ImageDeformation backDef = new ImageDeformationByFilters(new ImageFilter[] {});  
   ImageDeformation textDef = new ImageDeformationByFilters(new ImageFilter[] {});  
    Font[] fontsList = new Font[] { new Font("Arial", 0, 17),  
            new Font("Tahoma", 0, 17), new Font("Verdana", 0, 17),  
            new Font("Helvetica", 0, 17), new Font("宋体", 0, 17),  
            new Font("黑体", 0, 17), new Font("幼圆", 0, 17) }; 
    FontGenerator fontGenerator = new RandomFontGenerator(new Integer(15), new Integer(25), fontsList);

    WordToImage wordToImage = new DeformedComposedWordToImage(fontGenerator, backgroundGenerator, textPaster,backDef, textDef, postDef);
    this.addFactory(new MyGimpyFactory(wgen, wordToImage));
  }
  private static Color getRandColor(int fc, int bc) {  
      if (fc > 255)  
          fc = 255;  
      if (bc > 255)  
          bc = 255;  
      int r = fc + random.nextInt(bc - fc);  
      int g = fc + random.nextInt(bc - fc);  
      int b = fc + random.nextInt(bc - fc);  
      return new Color(r, g, b);  
  }  
}


 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

南汐寒笙箫 2021-12-05 11:53:43

找了,就是没找到,因为这个属于二次开发,很多东西都不知道在哪,与其花时间去找代码,不如重新写一个,只是这个WordToImage没用过, 不知道怎么去写算数验证码

醉酒的小男人 2021-12-05 11:52:31

算数验证码也是需要session支持的,前台计算结果要和后台存储的结果进行对比才能完成验证啊。

带上头具痛哭 2021-12-05 11:38:54

我觉得还是先查查session为什么会销毁的好,如果不用session,可以直接用前台验证。

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文