mirror of
https://github.com/librecaptcha/lc-core.git
synced 2025-01-27 04:53:13 -05:00
filter challenge: scale font height based on image size
This commit is contained in:
parent
d7c1f9a4cc
commit
cc8addb0c7
@ -41,23 +41,21 @@ class FilterChallenge extends ChallengeProvider {
|
|||||||
val height = size2D(1)
|
val height = size2D(1)
|
||||||
val canvas = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB)
|
val canvas = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB)
|
||||||
val g = canvas.createGraphics()
|
val g = canvas.createGraphics()
|
||||||
|
val fontHeight = (height*0.6).toInt
|
||||||
g.setColor(Color.WHITE)
|
g.setColor(Color.WHITE)
|
||||||
g.fillRect(0, 0, canvas.getWidth, canvas.getHeight)
|
g.fillRect(0, 0, canvas.getWidth, canvas.getHeight)
|
||||||
g.setColor(Color.BLACK)
|
g.setColor(Color.BLACK)
|
||||||
g.setFont(new Font("Serif", Font.PLAIN, 30))
|
g.setFont(new Font("Serif", Font.PLAIN, fontHeight))
|
||||||
g.drawString(secret, 5, 30)
|
val stringWidth = g.getFontMetrics().stringWidth(secret)
|
||||||
|
val xOffset = ((width - stringWidth)*r.nextDouble).toInt
|
||||||
|
g.drawString(secret, xOffset, fontHeight)
|
||||||
g.dispose()
|
g.dispose()
|
||||||
var image = ImmutableImage.fromAwt(canvas)
|
var image = ImmutableImage.fromAwt(canvas)
|
||||||
val s = scala.util.Random.nextInt(2)
|
val s = r.nextInt(2)
|
||||||
image = filterTypes(s).applyFilter(image)
|
image = filterTypes(s).applyFilter(image)
|
||||||
val img = image.awt()
|
val img = image.awt()
|
||||||
val baos = new ByteArrayOutputStream()
|
val baos = new ByteArrayOutputStream()
|
||||||
try {
|
PngImageWriter.write(baos, img);
|
||||||
PngImageWriter.write(baos, img);
|
|
||||||
} catch {
|
|
||||||
case e: Exception =>
|
|
||||||
e.printStackTrace()
|
|
||||||
}
|
|
||||||
new Challenge(baos.toByteArray, "image/png", secret)
|
new Challenge(baos.toByteArray, "image/png", secret)
|
||||||
}
|
}
|
||||||
def checkAnswer(secret: String, answer: String): Boolean = {
|
def checkAnswer(secret: String, answer: String): Boolean = {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user