added captcha types as required

This commit is contained in:
Prajwal Goudar 2019-03-09 16:27:36 +05:30
parent bb9fa10d72
commit a4a3a63f3f
5 changed files with 3289 additions and 0 deletions

View File

@ -0,0 +1,48 @@
package lc;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.util.Random;
public class FontFunCaptcha implements ChallengeProvider{
public String getId() {
return "FontFunCaptcha";
}
private byte[] fontFun(String captchaText){
String[] fonts = {"Captcha Code","Mom'sTypewriter","Annifont","SF Intoxicated Blues",
"BeachType","Batmos","Barbecue","Bad Seed","Aswell","Alien Marksman"};
String[] colors = {"#f68787","#f8a978","#f1eb9a","#a4f6a5"};
BufferedImage img = new BufferedImage(350, 100, BufferedImage.TYPE_INT_RGB);
Graphics2D graphics2D = img.createGraphics();
Random rand = new Random();
for(int i=0; i< captchaText.length(); i++) {
Font font = new Font(fonts[rand.nextInt(10)], Font.ROMAN_BASELINE, 48);
graphics2D.setFont(font);
FontMetrics fontMetrics = graphics2D.getFontMetrics();
HelperFunctions.setRenderingHints(graphics2D);
graphics2D.setColor(Color.decode(colors[rand.nextInt(4)]));
graphics2D.drawString(String.valueOf(captchaText.toLowerCase().charAt(i)), (i * 48), fontMetrics.getAscent());
}
graphics2D.dispose();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
ImageIO.write(img,"png",baos);
}catch (Exception e){
e.printStackTrace();
}
return baos.toByteArray();
}
public Challenge returnChallenge() {
String secret = HelperFunctions.randomString(7);
return new Challenge(fontFun(secret),"png",secret);
}
public boolean checkAnswer(String secret, String answer){
return secret.equals(answer);
}
}

View File

@ -0,0 +1,55 @@
package lc;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.stream.ImageOutputStream;
import javax.imageio.stream.MemoryCacheImageOutputStream;
import java.io.ByteArrayOutputStream;
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();
graphics2D.setFont(font);
graphics2D.setColor(new Color((int)(Math.random() * 0x1000000)));
graphics2D.drawString( text , 45, 45);
graphics2D.dispose();
return img;
}
public byte[] gifCaptcha(String text){
try {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
ImageOutputStream output = new MemoryCacheImageOutputStream(byteArrayOutputStream);
GifSequenceWriter writer = new GifSequenceWriter( output, 1,1000, true );
for(int i=0; i< text.length(); i++){
BufferedImage nextImage = charToImg(String.valueOf(text.charAt(i)));
writer.writeToSequence(nextImage);
}
writer.close();
output.close();
return byteArrayOutputStream.toByteArray();
} catch (IOException e){
e.printStackTrace();
}
return null;
}
public Challenge returnChallenge() {
String secret = HelperFunctions.randomString(6);
return new Challenge(gifCaptcha(secret),"gif",secret);
}
public boolean checkAnswer(String secret, String answer) {
return secret.equals(answer);
}
public String getId() {
return "GifCaptcha";
}
}

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,23 @@
package lc;
import java.awt.*;
public class HelperFunctions {
public static void setRenderingHints(Graphics2D g2d){
g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS,
RenderingHints.VALUE_FRACTIONALMETRICS_ON);
}
public static String randomString(int n){
String characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890";
StringBuilder stringBuilder = new StringBuilder();
for(int i=0; i<n; i++){
int index = (int)(characters.length() * Math.random());
stringBuilder.append(characters.charAt(index));
}
return stringBuilder.toString();
}
}

View File

@ -0,0 +1,60 @@
package lc;
import javax.imageio.ImageIO;
import java.awt.Graphics2D;
import java.awt.Color;
import java.awt.Font;
import java.awt.font.TextLayout;
import java.awt.image.BufferedImage;
import java.awt.image.ConvolveOp;
import java.awt.image.Kernel;
import java.io.ByteArrayOutputStream;
public class ShadowTextCaptcha implements ChallengeProvider{
public String getId() {
return "ShadowText";
}
public boolean checkAnswer(String secret, String answer) {
return secret.equals(answer);
}
public byte[] shadowText(String text){
BufferedImage img = new BufferedImage(350, 100, BufferedImage.TYPE_INT_RGB);
Font font = new Font("Arial",Font.ROMAN_BASELINE ,48);
Graphics2D graphics2D = img.createGraphics();
TextLayout textLayout = new TextLayout(text, font, graphics2D.getFontRenderContext());
HelperFunctions.setRenderingHints(graphics2D);
graphics2D.setPaint(Color.WHITE);
graphics2D.fillRect(0, 0, 350, 100);
graphics2D.setPaint(Color.BLACK);
textLayout.draw(graphics2D, 15, 50);
graphics2D.dispose();
float[] kernel = {
1f / 9f, 1f / 9f, 1f / 9f,
1f / 9f, 1f / 9f, 1f / 9f,
1f / 9f, 1f / 9f, 1f / 9f
};
ConvolveOp op = new ConvolveOp(new Kernel(3, 3, kernel),
ConvolveOp.EDGE_NO_OP, null);
BufferedImage img2 = op.filter(img, null);
Graphics2D g2d = img2.createGraphics();
HelperFunctions.setRenderingHints(g2d);
g2d.setPaint(Color.WHITE);
textLayout.draw(g2d, 13, 50);
g2d.dispose();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
ImageIO.write(img2,"png",baos);
}catch(Exception e){
e.printStackTrace();
}
return baos.toByteArray();
}
public Challenge returnChallenge() {
String secret = HelperFunctions.randomString(6);
return new Challenge(shadowText(secret),"png",secret);
}
}