A minimalistic CAPTCHA generator and validator, with customizable rendering options ready for both web and desktop applications. The image manipulation is done through https://github.com/ajmas/JH-Labs-Java-Image-Filters.
AbstractCaptchaRenderer renderer=CaptchaRenderer.getDefaultCaptchaRenderer();
AbstractCaptchaCipher captchaCipher = CaptchaCipher.builder()
.expirationTimeSettings(
new ExpirationTimeSettings( 560L, ()->System.currentTimeMillis()))
.build();
AbstractCaptchaGenerator generator = new CaptchaGenerator(captchaCipher,renderer);
String solution="abc123";
String salt="someSerializable";
Captcha captcha=generator.generate(solution,salt);
A Captcha generated this way is very basic and not obscured:
With the predefined builders, captcha image generation can be further modified to obscure the image, set colors, font, image file format, background and captcha character colors:
AbstractCaptchaRenderer renderer = CaptchaRenderer.builder()
.availableTextColors(List.of(Color.BLUE, Color.ORANGE, Color.BLACK))
.noiseSettings(new NoiseSettings(1, Color.GRAY))
.build();
Outputs:
Alternatively, an arbitrary chain of effects can be used to further manipulate the rendered image:
TwirlFilter filter = new TwirlFilter();
float twirlStrength = -0.6f;
filter.setAngle(twirlStrength);
AbstractCaptchaRenderer renderer = CaptchaRenderer.builder()
.noiseSettings(new NoiseSettings(8, Color.BLACK))
.availableTextColors(List.of(Color.BLUE, Color.RED, Color.ORANGE, Color.MAGENTA, Color.CYAN))
.maximumLetterRotationAngle(0.45d)
.imageBackground(new FlatColorBackground(Color.DARK_GRAY))
.imageOperationsList(Collections.singletonList(filter))
.build();
AbstractCaptchaCipher captchaCipher = CaptchaCipher.builder()
.expirationTimeSettings(new ExpirationTimeSettings(560L, System::currentTimeMillis))
.build();
AbstractCaptchaGenerator generator = new CaptchaGenerator(captchaCipher, renderer);
String solution = "ABCD1234";
String salt = "someSerializable";
Captcha captcha = generator.generate(solution, salt);
Resulting in:
<dependency>
<groupId>io.github.yaforster</groupId>
<artifactId>flexcaptcha</artifactId>
<version>2.0.0</version>
</dependency>