each challenge provider now respects the size parameter when construction image

This commit is contained in:
hrj
2022-03-01 23:03:43 +05:30
parent edd211fb52
commit 4f3bec0bc6
7 changed files with 43 additions and 21 deletions

View File

@@ -45,14 +45,14 @@ class DebugCaptcha extends ChallengeProvider {
matches
}
private def simpleText(text: String): Array[Byte] = {
val img = new BufferedImage(350, 100, BufferedImage.TYPE_INT_RGB)
private def simpleText(width: Int, height: Int, text: String): Array[Byte] = {
val img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB)
val font = new Font("Arial", Font.ROMAN_BASELINE, 56)
val graphics2D = img.createGraphics()
val textLayout = new TextLayout(text, font, graphics2D.getFontRenderContext())
HelperFunctions.setRenderingHints(graphics2D)
graphics2D.setPaint(Color.WHITE)
graphics2D.fillRect(0, 0, 350, 100)
graphics2D.fillRect(0, 0, width, height)
graphics2D.setPaint(Color.BLACK)
textLayout.draw(graphics2D, 15, 50)
graphics2D.dispose()
@@ -68,6 +68,9 @@ class DebugCaptcha extends ChallengeProvider {
def returnChallenge(level: String, size: String): Challenge = {
val secret = HelperFunctions.randomString(6, HelperFunctions.safeAlphabets)
new Challenge(simpleText(secret), "image/png", secret.toLowerCase())
val size2D = HelperFunctions.parseSize2D(size)
val width = size2D(0)
val height = size2D(1)
new Challenge(simpleText(width, height, secret), "image/png", secret.toLowerCase())
}
}

View File

@@ -10,6 +10,7 @@ import lc.captchas.interfaces.Challenge
import java.util.{List => JavaList, Map => JavaMap}
import java.io.ByteArrayOutputStream
import lc.misc.PngImageWriter
import lc.misc.HelperFunctions
class FilterChallenge extends ChallengeProvider {
def getId = "FilterChallenge"
@@ -35,7 +36,10 @@ class FilterChallenge extends ChallengeProvider {
val alphabet = "abcdefghijklmnopqrstuvwxyz"
val n = 8
val secret = LazyList.continually(r.nextInt(alphabet.size)).map(alphabet).take(n).mkString
val canvas = new BufferedImage(225, 50, BufferedImage.TYPE_INT_RGB)
val size2D = HelperFunctions.parseSize2D(size)
val width = size2D(0)
val height = size2D(1)
val canvas = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB)
val g = canvas.createGraphics()
g.setColor(Color.WHITE)
g.fillRect(0, 0, canvas.getWidth, canvas.getHeight)

View File

@@ -11,6 +11,7 @@ import lc.captchas.interfaces.ChallengeProvider
import lc.captchas.interfaces.Challenge
import lc.misc.GifSequenceWriter
import java.util.{List => JavaList, Map => JavaMap}
import lc.misc.HelperFunctions
class Drop {
var x = 0
@@ -59,8 +60,9 @@ class RainDropsCP extends ChallengeProvider {
def returnChallenge(level: String, size: String): Challenge = {
val r = new scala.util.Random
val secret = LazyList.continually(r.nextInt(alphabet.size)).map(alphabet).take(n).mkString
val width = 450
val height = 100
val size2D = HelperFunctions.parseSize2D(size)
val width = size2D(0)
val height = size2D(1)
val imgType = BufferedImage.TYPE_INT_RGB
val xOffset = 2 + r.nextInt(3)
val xBias = (height / 10) - 2