From f2c588ef239f34fd363db090a3b35feec7cd7132 Mon Sep 17 00:00:00 2001 From: Prajwal Goudar Date: Mon, 25 Mar 2019 19:44:38 +0530 Subject: [PATCH] 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 --- src/main/java/lc/FontFunCaptcha.java | 43 ++++++++++++++++++++++----- src/main/java/lc/HelperFunctions.java | 4 +++ src/main/scala/lc/Main.scala | 2 +- 3 files changed, 40 insertions(+), 9 deletions(-) diff --git a/src/main/java/lc/FontFunCaptcha.java b/src/main/java/lc/FontFunCaptcha.java index 8952613..3603b23 100644 --- a/src/main/java/lc/FontFunCaptcha.java +++ b/src/main/java/lc/FontFunCaptcha.java @@ -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){ diff --git a/src/main/java/lc/HelperFunctions.java b/src/main/java/lc/HelperFunctions.java index c1f0b2a..e3882ca 100644 --- a/src/main/java/lc/HelperFunctions.java +++ b/src/main/java/lc/HelperFunctions.java @@ -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; + } } diff --git a/src/main/scala/lc/Main.scala b/src/main/scala/lc/Main.scala index bae01a4..9b70ad0 100644 --- a/src/main/scala/lc/Main.scala +++ b/src/main/scala/lc/Main.scala @@ -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)