support for loading fonts dynamically with an option to choose level("easy","medium","hard")
This commit is contained in:
parent
0fe1dd1ae4
commit
e467972902
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
|
@ -4,7 +4,11 @@ import javax.imageio.ImageIO;
|
|||
import java.awt.*;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.util.Random;
|
||||
import java.io.File;
|
||||
import java.nio.file.Paths;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Files;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public class FontFunCaptcha implements ChallengeProvider{
|
||||
|
||||
|
|
@ -12,19 +16,46 @@ public class FontFunCaptcha implements ChallengeProvider{
|
|||
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"};
|
||||
private int noOfFiles(String path,String level){
|
||||
try(Stream<Path> files = Files.list(Paths.get(path+level))){
|
||||
return (short)files.count()-1;
|
||||
} catch (Exception e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
private Font loadCustomFont(String level,String path){
|
||||
String fontName = path+level+"/font"+HelperFunctions.randomNumber(1,noOfFiles(path,level))+".ttf";
|
||||
try{
|
||||
Font font = Font.createFont(Font.TRUETYPE_FONT, new File(fontName));
|
||||
font = font.deriveFont(Font.PLAIN, 48f);
|
||||
return font;
|
||||
} catch (Exception e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private Font getFont(String level, String path){
|
||||
switch (level.toLowerCase()){
|
||||
case "easy": return loadCustomFont(level, path);
|
||||
case "medium": return loadCustomFont(level, path);
|
||||
case "hard": return loadCustomFont(level, path);
|
||||
default: return null;
|
||||
}
|
||||
}
|
||||
|
||||
private byte[] fontFun(String captchaText, String level, String path){
|
||||
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);
|
||||
Font font = getFont(level,path);
|
||||
graphics2D.setFont(font);
|
||||
FontMetrics fontMetrics = graphics2D.getFontMetrics();
|
||||
HelperFunctions.setRenderingHints(graphics2D);
|
||||
graphics2D.setColor(Color.decode(colors[rand.nextInt(4)]));
|
||||
graphics2D.setColor(Color.decode(colors[HelperFunctions.randomNumber(0,3)]));
|
||||
graphics2D.drawString(String.valueOf(captchaText.charAt(i)), (i * 48), fontMetrics.getAscent());
|
||||
}
|
||||
graphics2D.dispose();
|
||||
|
|
@ -39,7 +70,8 @@ public class FontFunCaptcha implements ChallengeProvider{
|
|||
|
||||
public Challenge returnChallenge() {
|
||||
String secret = HelperFunctions.randomString(7);
|
||||
return new Challenge(fontFun(secret),"png",secret.toLowerCase());
|
||||
String path = "./lib/fonts/";
|
||||
return new Challenge(fontFun(secret,"medium",path),"png",secret.toLowerCase());
|
||||
}
|
||||
|
||||
public boolean checkAnswer(String secret, String answer){
|
||||
|
|
|
|||
|
|
@ -20,4 +20,8 @@ public class HelperFunctions {
|
|||
}
|
||||
return stringBuilder.toString();
|
||||
}
|
||||
|
||||
public static int randomNumber(int min,int max){
|
||||
return (int)(Math.random() * ((max - min) +1)) + min;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -45,7 +45,7 @@ class Captcha {
|
|||
|
||||
def getChallenge(param: Parameters): Id = {
|
||||
//TODO: eval params to choose a provider
|
||||
val providerMap = "GifCaptcha"
|
||||
val providerMap = "FontFunCaptcha"
|
||||
val provider = filters(providerMap)
|
||||
val challenge = provider.returnChallenge()
|
||||
val blob = new ByteArrayInputStream(challenge.content)
|
||||
|
|
|
|||
Loading…
Reference in New Issue