Gif Captcha: Simplify code

Signed-off-by: hrj <harshad.rj@gmail.com>
This commit is contained in:
hrj 2021-04-19 15:26:03 +05:30
parent d2ef8c5259
commit d32d836475
1 changed files with 12 additions and 15 deletions

View File

@ -1,7 +1,6 @@
package lc.captchas;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Color;
import java.awt.image.BufferedImage;
@ -9,7 +8,6 @@ import java.io.IOException;
import java.util.Map;
import java.util.List;
import javax.imageio.stream.ImageOutputStream;
import javax.imageio.stream.MemoryCacheImageOutputStream;
import java.io.ByteArrayOutputStream;
import lc.captchas.interfaces.Challenge;
@ -19,13 +17,12 @@ import lc.misc.GifSequenceWriter;
public class GifCaptcha implements ChallengeProvider {
private BufferedImage charToImg(String text) {
BufferedImage img = new BufferedImage(250, 100, BufferedImage.TYPE_INT_RGB);
Font font = new Font("Bradley Hand", Font.ROMAN_BASELINE, 48);
Graphics2D graphics2D = img.createGraphics();
private BufferedImage charToImg(final String text) {
final var img = new BufferedImage(250, 100, BufferedImage.TYPE_INT_RGB);
final var font = new Font("Bradley Hand", Font.ROMAN_BASELINE, 48);
final var graphics2D = img.createGraphics();
graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
graphics2D.setRenderingHint(
RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
graphics2D.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
graphics2D.setFont(font);
graphics2D.setColor(new Color((int) (Math.random() * 0x1000000)));
graphics2D.drawString(text, 45, 45);
@ -33,13 +30,13 @@ public class GifCaptcha implements ChallengeProvider {
return img;
}
private byte[] gifCaptcha(String text) {
private byte[] gifCaptcha(final String text) {
try {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
ImageOutputStream output = new MemoryCacheImageOutputStream(byteArrayOutputStream);
GifSequenceWriter writer = new GifSequenceWriter(output, 1, 1000, true);
final var byteArrayOutputStream = new ByteArrayOutputStream();
final var output = new MemoryCacheImageOutputStream(byteArrayOutputStream);
final var writer = new GifSequenceWriter(output, 1, 1000, true);
for (int i = 0; i < text.length(); i++) {
BufferedImage nextImage = charToImg(String.valueOf(text.charAt(i)));
final var nextImage = charToImg(String.valueOf(text.charAt(i)));
writer.writeToSequence(nextImage);
}
writer.close();
@ -51,7 +48,7 @@ public class GifCaptcha implements ChallengeProvider {
return null;
}
public void configure(String config) {
public void configure(final String config) {
// TODO: Add custom config
}
@ -63,7 +60,7 @@ public class GifCaptcha implements ChallengeProvider {
}
public Challenge returnChallenge() {
String secret = HelperFunctions.randomString(6);
final var secret = HelperFunctions.randomString(6);
return new Challenge(gifCaptcha(secret), "image/gif", secret.toLowerCase());
}