Merge pull request #96 from rr83019/image-dpi

Set Image DPI for captcha providers
This commit is contained in:
hrj
2021-09-02 16:37:00 +05:30
committed by GitHub
7 changed files with 92 additions and 14 deletions

View File

@@ -1,6 +1,5 @@
package lc.captchas
import javax.imageio.ImageIO
import java.awt.Color
import java.awt.Font
import java.awt.font.TextLayout
@@ -12,6 +11,7 @@ import java.util.List
import lc.misc.HelperFunctions
import lc.captchas.interfaces.Challenge
import lc.captchas.interfaces.ChallengeProvider
import lc.misc.PngImageWriter
/** This captcha is only for debugging purposes. It creates very simple captchas that are deliberately easy to solve with OCR engines */
class DebugCaptcha extends ChallengeProvider {
@@ -53,7 +53,7 @@ class DebugCaptcha extends ChallengeProvider {
graphics2D.dispose()
val baos = new ByteArrayOutputStream()
try {
ImageIO.write(img, "png", baos)
PngImageWriter.write(baos, img);
} catch {
case e: Exception =>
e.printStackTrace()

View File

@@ -8,6 +8,8 @@ import java.awt.Color
import lc.captchas.interfaces.ChallengeProvider
import lc.captchas.interfaces.Challenge
import java.util.{List => JavaList, Map => JavaMap}
import java.io.ByteArrayOutputStream
import lc.misc.PngImageWriter
class FilterChallenge extends ChallengeProvider {
def getId = "FilterChallenge"
@@ -41,7 +43,15 @@ class FilterChallenge extends ChallengeProvider {
var image = ImmutableImage.fromAwt(canvas)
val s = scala.util.Random.nextInt(2)
image = filterTypes(s).applyFilter(image)
new Challenge(image.bytes(new nio.PngWriter()), "image/png", secret)
val img = image.awt()
val baos = new ByteArrayOutputStream()
try {
PngImageWriter.write(baos, img);
} catch {
case e: Exception =>
e.printStackTrace()
}
new Challenge(baos.toByteArray, "image/png", secret)
}
def checkAnswer(secret: String, answer: String): Boolean = {
secret == answer

View File

@@ -10,6 +10,7 @@ import java.awt.Color
import lc.captchas.interfaces.ChallengeProvider
import lc.captchas.interfaces.Challenge
import java.util.{List => JavaList, Map => JavaMap}
import lc.misc.PngImageWriter
class LabelCaptcha extends ChallengeProvider {
private var knownFiles = new File("known").list.toList
@@ -49,7 +50,7 @@ class LabelCaptcha extends ChallengeProvider {
val token = encrypt(knownImageFile + "," + unknownImageFile)
val baos = new ByteArrayOutputStream()
ImageIO.write(mergedImage, "png", baos)
PngImageWriter.write(baos, mergedImage);
new Challenge(baos.toByteArray(), "image/png", token)
}