Customizing JCAPTCHA, opensource Java api for CAPTCHA
It took me some time to customize the engine which generate words for CAPTCHA validation. The api has no easy way to change the behaviour of the word generation. After all, I find a way to do it. I extends the class SimpleListImageCaptchaEngine and overwrite buildInitialFactories method to get desired behaviour. Basically what I want is to use UPPERCASE letters and numbers only which are also safe from confusion(for example 0,O,L,I).Here's the code:
import java.awt.Color;
import com.octo.captcha.component.image.backgroundgenerator.BackgroundGenerator;
import com.octo.captcha.component.image.backgroundgenerator.FunkyBackgroundGenerator;
import com.octo.captcha.component.image.fontgenerator.FontGenerator;
import com.octo.captcha.component.image.fontgenerator.TwistedAndShearedRandomFontGenerator;
import com.octo.captcha.component.image.textpaster.RandomTextPaster;
import com.octo.captcha.component.image.textpaster.TextPaster;
import com.octo.captcha.engine.image.gimpy.SimpleListImageCaptchaEngine;
public class MyImageCaptchaEngine extends SimpleListImageCaptchaEngine {
public MyImageCaptchaEngine() {
super();
}
protected void buildInitialFactories() {
com.octo.captcha.component.word.wordgenerator.WordGenerator wordGenerator = new com.octo.captcha.component.word.wordgenerator.RandomWordGenerator(
"123456789ABCDEFGHJKMNPQRSTUVWXYZ"); //Use only uppercase letters and safe characters (i.e. omits chars like O,L,I)
TextPaster textPaster = new RandomTextPaster(new Integer(5),
new Integer(8), Color.white);
BackgroundGenerator backgroundGenerator = new FunkyBackgroundGenerator(
new Integer(200), new Integer(100));
FontGenerator fontGenerator = new TwistedAndShearedRandomFontGenerator(
new Integer(25), new Integer(30));
com.octo.captcha.component.image.wordtoimage.WordToImage wordToImage = new com.octo.captcha.component.image.wordtoimage.ComposedWordToImage(
fontGenerator, backgroundGenerator, textPaster);
this.addFactory(
new com.octo.captcha.image.gimpy.GimpyFactory(wordGenerator,
wordToImage));
}
}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment