mirror of
https://github.com/librecaptcha/lc-core.git
synced 2025-01-13 06:53:19 -05:00
support for loading fonts dynamically with an option to choose level (#26)
* support for loading fonts dynamically with an option to choose level("easy","medium","hard") * removed unwanted method * changes made as required
This commit is contained in:
parent
2d13c10812
commit
f2c588ef23
@ -4,7 +4,8 @@ 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.io.FilenameFilter;
|
||||
|
||||
public class FontFunCaptcha implements ChallengeProvider{
|
||||
|
||||
@ -12,19 +13,44 @@ 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 String getFontName(String path, String level){
|
||||
File file = new File(path+level+"/");
|
||||
FilenameFilter txtFileFilter = new FilenameFilter() {
|
||||
@Override
|
||||
public boolean accept(File dir, String name)
|
||||
{
|
||||
if(name.endsWith(".ttf"))
|
||||
return true;
|
||||
else
|
||||
return false;
|
||||
}
|
||||
};
|
||||
File[] files = file.listFiles(txtFileFilter);
|
||||
return path+level.toLowerCase()+"/"+files[HelperFunctions.randomNumber(0,files.length-1)].getName();
|
||||
}
|
||||
|
||||
private Font loadCustomFont(String level, String path) {
|
||||
String fontName = getFontName(path,level);
|
||||
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 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 = loadCustomFont(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 +65,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
Block a user