现在是这种随机验证码
要怎么改成算数的????
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() {
//验证码key
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);
}
}
package com.auchan.captcha.util;
import java.awt.image.BufferedImage;
import java.security.SecureRandom;
import java.util.Locale;
import java.util.Random;
import com.octo.captcha.CaptchaException;
import com.octo.captcha.CaptchaQuestionHelper;
import com.octo.captcha.component.image.wordtoimage.WordToImage;
import com.octo.captcha.component.word.wordgenerator.WordGenerator;
import com.octo.captcha.image.ImageCaptcha;
import com.octo.captcha.image.ImageCaptchaFactory;
import com.octo.captcha.image.gimpy.Gimpy;
public class MyGimpyFactory extends ImageCaptchaFactory {
private Random myRandom = new SecureRandom();
private WordToImage wordToImage;
private WordGenerator wordGenerator;
public static final String BUNDLE_QUESTION_KEY = Gimpy.class.getName();
public MyGimpyFactory(WordGenerator generator, WordToImage word2image) {
if (word2image == null) {
throw new CaptchaException("Invalid configuration"
+ " for a GimpyFactory : WordToImage can't be null");
}
if (generator == null) {
throw new CaptchaException("Invalid configuration"
+ " for a GimpyFactory : WordGenerator can't be null");
}
wordToImage = word2image;
wordGenerator = generator;
}
public ImageCaptcha getImageCaptcha() {
return getImageCaptcha(Locale.getDefault());
}
public WordToImage getWordToImage() {
return wordToImage;
}
public WordGenerator getWordGenerator() {
return wordGenerator;
}
@Override
public ImageCaptcha getImageCaptcha(Locale locale) {
Integer wordLength = getRandomLength();
String word = getWordGenerator().getWord(wordLength, locale);
BufferedImage image = null;
try {
image = getWordToImage().getImage(word);
} catch (Throwable e) {
throw new CaptchaException(e);
}
// 这里用我们自己写的MyGimpy
ImageCaptcha captcha = new MyGimpy(CaptchaQuestionHelper.getQuestion(
locale, BUNDLE_QUESTION_KEY), image, word);
return captcha;
}
protected Integer getRandomLength() {
Integer wordLength;
int range = getWordToImage().getMaxAcceptedWordLength()
- getWordToImage().getMinAcceptedWordLength();
int randomRange = range != 0 ? myRandom.nextInt(range + 1) : 0;
wordLength = new Integer(randomRange
+ getWordToImage().getMinAcceptedWordLength());
return wordLength;
}
}
package com.auchan.captcha.util;
import java.awt.image.BufferedImage;
import java.io.Serializable;
import com.octo.captcha.image.ImageCaptcha;
public class MyGimpy extends ImageCaptcha implements Serializable {
private String response;
MyGimpy(String question, BufferedImage challenge, String response) {
super(question, challenge);
this.response = response;
}
public final Boolean validateResponse(final Object response) {
return (null != response && response instanceof String)
? validateResponse((String) response) : Boolean.FALSE;
}
private final Boolean validateResponse(final String response) {
// 主要改的这里
return new Boolean(response.toLowerCase().equals(this.response.toLowerCase()));
}
}