Created
November 7, 2016 11:54
-
-
Save jiacheo/1d580f3143d2e5f0154612f6819f550b to your computer and use it in GitHub Desktop.
customize captcha engine with random funky color background and ignore case sencisive
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.octo.captcha.CaptchaException; | |
import com.octo.captcha.CaptchaQuestionHelper; | |
import com.octo.captcha.component.image.backgroundgenerator.BackgroundGenerator; | |
import com.octo.captcha.component.image.backgroundgenerator.FunkyBackgroundGenerator; | |
import com.octo.captcha.component.image.color.ColorGenerator; | |
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.component.image.wordtoimage.ComposedWordToImage; | |
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; | |
import com.octo.captcha.image.ImageCaptcha; | |
import com.octo.captcha.image.gimpy.GimpyFactory; | |
import java.awt.*; | |
import java.awt.image.BufferedImage; | |
import java.io.Serializable; | |
import java.util.Locale; | |
import java.util.Random; | |
/** | |
* Created by jiacheo on 2016/11/7. | |
*/ | |
public class CustomCaptchaEngine extends ListImageCaptchaEngine { | |
@Override | |
protected void buildInitialFactories() { | |
//去掉 1 i I l , 2 z z, 5 s S, 9 q g, o O 0 等容易混淆(特别在在图片下)的字母,不让用户纠结。 | |
WordGenerator wordGenerator = new RandomWordGenerator("34678ABCDEFHJKMNPQRTUVWXLYabcdefhjkmnpqrtuvwxy"); | |
// nteger minAcceptedWordLength, Integer maxAcceptedWordLength,Color[] | |
// textColors | |
TextPaster textPaster = new RandomTextPaster(4,4, Color.WHITE); | |
// Integer width, Integer height | |
BackgroundGenerator backgroundGenerator = new FunkyBackgroundGenerator(150,42, new RandomColorGennerator(), new RandomColorGennerator(), new RandomColorGennerator(), new RandomColorGennerator(), 0.5f); | |
// Integer minFontSize, Integer maxFontSize | |
FontGenerator fontGenerator = new TwistedAndShearedRandomFontGenerator(20, 22); | |
WordToImage wordToImage = new ComposedWordToImage(fontGenerator, | |
backgroundGenerator, textPaster); | |
addFactory(new GimpyFactory(wordGenerator, wordToImage){ | |
public ImageCaptcha getImageCaptcha(Locale locale) { | |
Integer wordLength = this.getRandomLength(); | |
String word = this.getWordGenerator().getWord(wordLength, locale); | |
BufferedImage image ; | |
try { | |
image = this.getWordToImage().getImage(word); | |
} catch (Throwable e) { | |
throw new CaptchaException(e); | |
} | |
IgnoreCaseGimpy captcha = new IgnoreCaseGimpy(CaptchaQuestionHelper.getQuestion(locale, BUNDLE_QUESTION_KEY), image, word); | |
return captcha; | |
} | |
}); | |
} | |
public static class IgnoreCaseGimpy extends ImageCaptcha implements Serializable { | |
private String response; | |
IgnoreCaseGimpy(String question, BufferedImage challenge, String response) { | |
super(question, challenge); | |
this.response = response; | |
} | |
public final Boolean validateResponse(Object response) { | |
return null != response && response instanceof String?this.validateResponse((String)response):Boolean.FALSE; | |
} | |
private final Boolean validateResponse(String response) { | |
return new Boolean(response.equalsIgnoreCase(this.response)); | |
} | |
} | |
public static class RandomColorGennerator implements ColorGenerator { | |
private Random random = new Random(); | |
private Color[] colors = new Color[] {Color.RED, Color.ORANGE, Color.YELLOW, Color.GREEN, Color.BLUE, Color.CYAN, Color.MAGENTA}; | |
@Override | |
public Color getNextColor() { | |
return colors[random.nextInt(colors.length)]; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment