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.
This commit is contained in:
sanjana 2018-01-06 10:10:43 +05:30
parent dfa146e139
commit 40d0fbd0f7
3 changed files with 70 additions and 6 deletions

View File

@ -12,7 +12,7 @@ class BlurCaptcha extends CaptchaProvider {
val blur = new GaussianBlurFilter(5) val blur = new GaussianBlurFilter(5)
blur.apply(image) blur.apply(image)
val s = scala.util.Random val s = scala.util.Random
val token = s.nextInt(1000).toString val token = s.nextInt(10000).toString
val challenge = new Challenge(token, image) val challenge = new Challenge(token, image)
val answer = "about" val answer = "about"
tokenAnswer += token -> answer tokenAnswer += token -> answer

View File

@ -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
}
}

View File

@ -2,7 +2,7 @@ import com.sksamuel.scrimage._
import java.io._ import java.io._
class CaptchaLibrary { 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]() var tokenCaptcha = scala.collection.mutable.Map[String, CaptchaProvider]()
def init = {} def init = {}
def shutdown = {} def shutdown = {}
@ -32,9 +32,6 @@ class Answer(val token: String, val input: String)
object LibreCaptcha { object LibreCaptcha {
def main(args: Array[String]) { def main(args: Array[String]) {
val captcha = new CaptchaLibrary val captcha = new CaptchaLibrary
var a = 0
for (a <- 1 to 3)
{
val challenge = captcha.getChallenge() val challenge = captcha.getChallenge()
println(s"Token: ${challenge.token}") println(s"Token: ${challenge.token}")
challenge.image.output(new File("Captcha.png")) challenge.image.output(new File("Captcha.png"))
@ -42,6 +39,5 @@ object LibreCaptcha {
val input = scala.io.StdIn.readLine() val input = scala.io.StdIn.readLine()
val result = captcha.checkAnswer(challenge.token, input) val result = captcha.checkAnswer(challenge.token, input)
println(s"Result: $result") println(s"Result: $result")
}
} }
} }