From 40d0fbd0f78d7480d5441312daed10fd359052e1 Mon Sep 17 00:00:00 2001 From: sanjana Date: Sat, 6 Jan 2018 10:10:43 +0530 Subject: [PATCH] Add FilterCaptcha FilterCaptcha generates a random string, creates an image of the string, and applies a series of filters to make the string hard to read. --- src/main/scala/BlurCaptcha.scala | 2 +- src/main/scala/FilterCaptcha.scala | 68 ++++++++++++++++++++++++++++++ src/main/scala/Main.scala | 6 +-- 3 files changed, 70 insertions(+), 6 deletions(-) create mode 100644 src/main/scala/FilterCaptcha.scala diff --git a/src/main/scala/BlurCaptcha.scala b/src/main/scala/BlurCaptcha.scala index aabd9af..fa4a784 100644 --- a/src/main/scala/BlurCaptcha.scala +++ b/src/main/scala/BlurCaptcha.scala @@ -12,7 +12,7 @@ class BlurCaptcha extends CaptchaProvider { val blur = new GaussianBlurFilter(5) blur.apply(image) val s = scala.util.Random - val token = s.nextInt(1000).toString + val token = s.nextInt(10000).toString val challenge = new Challenge(token, image) val answer = "about" tokenAnswer += token -> answer diff --git a/src/main/scala/FilterCaptcha.scala b/src/main/scala/FilterCaptcha.scala new file mode 100644 index 0000000..e3edf34 --- /dev/null +++ b/src/main/scala/FilterCaptcha.scala @@ -0,0 +1,68 @@ +import com.sksamuel.scrimage._ +import com.sksamuel.scrimage.filter._ +import java.awt.image.BufferedImage +import java.awt.{Graphics2D,Color,Font} + +class FilterCaptcha extends CaptchaProvider { + val tokenAnswer = scala.collection.mutable.Map[String, String]() + def getChallenge(): Challenge = { + val filterTypes = List(new FilterType1, new FilterType2) + val r = new scala.util.Random + val alphabet = "abcdefghijklmnopqrstuvwxyz" + val n = 8 + val answer = Stream.continually(r.nextInt(alphabet.size)).map(alphabet).take(n).mkString + val token = scala.util.Random.nextInt(10000).toString + tokenAnswer += token -> answer + val canvas = new BufferedImage(225, 50, BufferedImage.TYPE_INT_RGB) + val g = canvas.createGraphics() + g.setColor(Color.WHITE) + g.fillRect(0, 0, canvas.getWidth, canvas.getHeight) + g.setColor(Color.BLACK) + g.setFont(new Font("Serif", Font.PLAIN, 30)) + g.drawString(answer, 5, 30) + g.dispose() + var image = new Image(canvas, ImageMetadata.empty) + val s = scala.util.Random.nextInt(2) + image = filterTypes(s).applyFilter(image) + val challenge = new Challenge(token, image) + challenge + } + def checkAnswer(token: String, input: String): Boolean = { + if(tokenAnswer(token)==input) + { + true + } + else + { + false + } + } +} + +trait FilterType { + def applyFilter(image: Image): Image +} + +class FilterType1 extends FilterType { + override def applyFilter(image: Image): Image = { + val blur = new GaussianBlurFilter(2) + val smear = new SmearFilter(com.sksamuel.scrimage.filter.SmearType.Circles, 10, 10, 10, 0, 1) + val diffuse = new DiffuseFilter(2) + blur.apply(image) + diffuse.apply(image) + smear.apply(image) + image + } +} + +class FilterType2 extends FilterType { + override def applyFilter(image: Image): Image = { + val smear = new SmearFilter(com.sksamuel.scrimage.filter.SmearType.Circles, 10, 10, 10, 0, 1) + val diffuse = new DiffuseFilter(1) + val ripple = new RippleFilter(com.sksamuel.scrimage.filter.RippleType.Noise, 1, 1, 0.005.toFloat, 0.005.toFloat) + diffuse.apply(image) + ripple.apply(image) + smear.apply(image) + image + } +} diff --git a/src/main/scala/Main.scala b/src/main/scala/Main.scala index cd99b56..a684bd6 100644 --- a/src/main/scala/Main.scala +++ b/src/main/scala/Main.scala @@ -2,7 +2,7 @@ import com.sksamuel.scrimage._ import java.io._ class CaptchaLibrary { - val captchas = List(new BlurCaptcha, new LabelCaptcha) + val captchas = List(new BlurCaptcha, new LabelCaptcha, new FilterCaptcha) var tokenCaptcha = scala.collection.mutable.Map[String, CaptchaProvider]() def init = {} def shutdown = {} @@ -32,9 +32,6 @@ class Answer(val token: String, val input: String) object LibreCaptcha { def main(args: Array[String]) { val captcha = new CaptchaLibrary - var a = 0 - for (a <- 1 to 3) - { val challenge = captcha.getChallenge() println(s"Token: ${challenge.token}") challenge.image.output(new File("Captcha.png")) @@ -42,6 +39,5 @@ object LibreCaptcha { val input = scala.io.StdIn.readLine() val result = captcha.checkAnswer(challenge.token, input) println(s"Result: $result") - } } }