From d30249a89fef4c0430859e78374f53809aec7f1b Mon Sep 17 00:00:00 2001 From: Rahul Rudragoudar Date: Tue, 31 Aug 2021 15:42:57 +0530 Subject: [PATCH 1/5] Add DPI setter Signed-off-by: Rahul Rudragoudar --- src/main/java/lc/misc/DPISetter.java | 69 ++++++++++++++++++++++ src/main/java/lc/misc/HelperFunctions.java | 11 ++-- 2 files changed, 74 insertions(+), 6 deletions(-) create mode 100644 src/main/java/lc/misc/DPISetter.java diff --git a/src/main/java/lc/misc/DPISetter.java b/src/main/java/lc/misc/DPISetter.java new file mode 100644 index 0000000..38b8c07 --- /dev/null +++ b/src/main/java/lc/misc/DPISetter.java @@ -0,0 +1,69 @@ +package lc.misc; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.util.Iterator; + +import javax.imageio.IIOImage; +import javax.imageio.ImageIO; +import javax.imageio.ImageTypeSpecifier; +import javax.imageio.ImageWriteParam; +import javax.imageio.ImageWriter; +import javax.imageio.metadata.IIOInvalidTreeException; +import javax.imageio.metadata.IIOMetadata; +import javax.imageio.metadata.IIOMetadataNode; +import javax.imageio.stream.ImageOutputStream; +import java.awt.image.BufferedImage; + +public class DPISetter { + + final int DPI = 245; + final double INCH_2_CM = 2.54; + + public void setDPI(ByteArrayOutputStream boas, BufferedImage gridImage) throws IOException { + final String formatName = "png"; + for (Iterator iw = ImageIO.getImageWritersByFormatName(formatName); + iw.hasNext(); ) { + ImageWriter writer = iw.next(); + ImageWriteParam writeParam = writer.getDefaultWriteParam(); + ImageTypeSpecifier typeSpecifier = ImageTypeSpecifier.createFromBufferedImageType(BufferedImage.TYPE_INT_RGB); + IIOMetadata metadata = writer.getDefaultImageMetadata(typeSpecifier, writeParam); + if (metadata.isReadOnly() || !metadata.isStandardMetadataFormatSupported()) { + continue; + } + + setDPIMeta(metadata); + + final ImageOutputStream stream = ImageIO.createImageOutputStream(boas); + try { + writer.setOutput(stream); + writer.write(metadata, new IIOImage(gridImage, null, metadata), writeParam); + } finally { + stream.close(); + } + break; + } + } + + public void setDPIMeta(IIOMetadata metadata) throws IIOInvalidTreeException { + + // for PNG, it's dots per millimeter + double dotsPerMilli = 1.0 * DPI / 10 / INCH_2_CM; + System.out.println(dotsPerMilli); + + IIOMetadataNode horiz = new IIOMetadataNode("HorizontalPixelSize"); + horiz.setAttribute("value", Double.toString(dotsPerMilli)); + + IIOMetadataNode vert = new IIOMetadataNode("VerticalPixelSize"); + vert.setAttribute("value", Double.toString(dotsPerMilli)); + + IIOMetadataNode dim = new IIOMetadataNode("Dimension"); + dim.appendChild(horiz); + dim.appendChild(vert); + + IIOMetadataNode root = new IIOMetadataNode("javax_imageio_1.0"); + root.appendChild(dim); + + metadata.mergeTree("javax_imageio_1.0", root); + } +} diff --git a/src/main/java/lc/misc/HelperFunctions.java b/src/main/java/lc/misc/HelperFunctions.java index 4e77821..22b3a8a 100644 --- a/src/main/java/lc/misc/HelperFunctions.java +++ b/src/main/java/lc/misc/HelperFunctions.java @@ -7,9 +7,9 @@ public class HelperFunctions { private static Random random = new Random(); - synchronized public static void setSeed(long seed){ + public static synchronized void setSeed(long seed) { random.setSeed(seed); - } + } public static void setRenderingHints(Graphics2D g2d) { g2d.setRenderingHint( @@ -38,12 +38,11 @@ public class HelperFunctions { return stringBuilder.toString(); } - synchronized public static int randomNumber(int min, int max) { + public static synchronized int randomNumber(int min, int max) { return random.nextInt((max - min) + 1) + min; } - synchronized public static int randomNumber(int bound) { - return random.nextInt(bound); + public static synchronized int randomNumber(int bound) { + return random.nextInt(bound); } - } From 26d86bca4cfdbd92dc50f6b20dbad45361949f9f Mon Sep 17 00:00:00 2001 From: Rahul Rudragoudar Date: Tue, 31 Aug 2021 15:43:58 +0530 Subject: [PATCH 2/5] Set DPI for captcha providers Signed-off-by: Rahul Rudragoudar --- src/main/java/lc/captchas/FontFunCaptcha.java | 5 +++-- src/main/java/lc/captchas/ShadowTextCaptcha.java | 5 +++-- src/main/scala/lc/captchas/DebugCaptcha.scala | 5 +++-- src/main/scala/lc/captchas/LabelCaptcha.scala | 4 +++- 4 files changed, 12 insertions(+), 7 deletions(-) diff --git a/src/main/java/lc/captchas/FontFunCaptcha.java b/src/main/java/lc/captchas/FontFunCaptcha.java index 1007078..be09485 100644 --- a/src/main/java/lc/captchas/FontFunCaptcha.java +++ b/src/main/java/lc/captchas/FontFunCaptcha.java @@ -1,6 +1,5 @@ package lc.captchas; -import javax.imageio.ImageIO; import java.awt.*; import java.awt.image.BufferedImage; import java.io.ByteArrayOutputStream; @@ -10,6 +9,7 @@ import java.util.Map; import java.util.List; import lc.captchas.interfaces.Challenge; import lc.captchas.interfaces.ChallengeProvider; +import lc.misc.DPISetter; import lc.misc.HelperFunctions; public class FontFunCaptcha implements ChallengeProvider { @@ -74,7 +74,8 @@ public class FontFunCaptcha implements ChallengeProvider { graphics2D.dispose(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); try { - ImageIO.write(img, "png", baos); + DPISetter dpi = new DPISetter(); + dpi.setDPI(baos, img); } catch (Exception e) { e.printStackTrace(); } diff --git a/src/main/java/lc/captchas/ShadowTextCaptcha.java b/src/main/java/lc/captchas/ShadowTextCaptcha.java index b510b9a..16f2b54 100644 --- a/src/main/java/lc/captchas/ShadowTextCaptcha.java +++ b/src/main/java/lc/captchas/ShadowTextCaptcha.java @@ -1,6 +1,5 @@ package lc.captchas; -import javax.imageio.ImageIO; import java.awt.Graphics2D; import java.awt.RenderingHints; import java.awt.Color; @@ -14,6 +13,7 @@ import java.util.Map; import java.util.List; import lc.misc.HelperFunctions; +import lc.misc.DPISetter; import lc.captchas.interfaces.Challenge; import lc.captchas.interfaces.ChallengeProvider; @@ -67,7 +67,8 @@ public class ShadowTextCaptcha implements ChallengeProvider { g2d.dispose(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); try { - ImageIO.write(img2, "png", baos); + DPISetter dpi = new DPISetter(); + dpi.setDPI(baos, img2); } catch (Exception e) { e.printStackTrace(); } diff --git a/src/main/scala/lc/captchas/DebugCaptcha.scala b/src/main/scala/lc/captchas/DebugCaptcha.scala index 787b6cf..5a0f0f1 100644 --- a/src/main/scala/lc/captchas/DebugCaptcha.scala +++ b/src/main/scala/lc/captchas/DebugCaptcha.scala @@ -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.DPISetter /** 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,8 @@ class DebugCaptcha extends ChallengeProvider { graphics2D.dispose() val baos = new ByteArrayOutputStream() try { - ImageIO.write(img, "png", baos) + val dpi = new DPISetter(); + dpi.setDPI(baos, img); } catch { case e: Exception => e.printStackTrace() diff --git a/src/main/scala/lc/captchas/LabelCaptcha.scala b/src/main/scala/lc/captchas/LabelCaptcha.scala index cd008e7..7b85e06 100644 --- a/src/main/scala/lc/captchas/LabelCaptcha.scala +++ b/src/main/scala/lc/captchas/LabelCaptcha.scala @@ -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.DPISetter class LabelCaptcha extends ChallengeProvider { private var knownFiles = new File("known").list.toList @@ -49,7 +50,8 @@ class LabelCaptcha extends ChallengeProvider { val token = encrypt(knownImageFile + "," + unknownImageFile) val baos = new ByteArrayOutputStream() - ImageIO.write(mergedImage, "png", baos) + val dpi = new DPISetter(); + dpi.setDPI(baos, mergedImage); new Challenge(baos.toByteArray(), "image/png", token) } From f83289514b44572d7513239a1d81084bb0bd8d6d Mon Sep 17 00:00:00 2001 From: Rahul Rudragoudar Date: Wed, 1 Sep 2021 22:57:26 +0530 Subject: [PATCH 3/5] Rename DPISetter to PngImageWritter Make fn static Signed-off-by: Rahul Rudragoudar --- src/main/java/lc/captchas/FontFunCaptcha.java | 5 ++--- src/main/java/lc/captchas/ShadowTextCaptcha.java | 5 ++--- .../lc/misc/{DPISetter.java => PngImageWriter.java} | 10 +++++----- src/main/scala/lc/captchas/DebugCaptcha.scala | 5 ++--- src/main/scala/lc/captchas/LabelCaptcha.scala | 5 ++--- 5 files changed, 13 insertions(+), 17 deletions(-) rename src/main/java/lc/misc/{DPISetter.java => PngImageWriter.java} (87%) diff --git a/src/main/java/lc/captchas/FontFunCaptcha.java b/src/main/java/lc/captchas/FontFunCaptcha.java index be09485..8dbea52 100644 --- a/src/main/java/lc/captchas/FontFunCaptcha.java +++ b/src/main/java/lc/captchas/FontFunCaptcha.java @@ -9,7 +9,7 @@ import java.util.Map; import java.util.List; import lc.captchas.interfaces.Challenge; import lc.captchas.interfaces.ChallengeProvider; -import lc.misc.DPISetter; +import lc.misc.PngImageWriter; import lc.misc.HelperFunctions; public class FontFunCaptcha implements ChallengeProvider { @@ -74,8 +74,7 @@ public class FontFunCaptcha implements ChallengeProvider { graphics2D.dispose(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); try { - DPISetter dpi = new DPISetter(); - dpi.setDPI(baos, img); + PngImageWriter.write(baos, img); } catch (Exception e) { e.printStackTrace(); } diff --git a/src/main/java/lc/captchas/ShadowTextCaptcha.java b/src/main/java/lc/captchas/ShadowTextCaptcha.java index 16f2b54..a797c50 100644 --- a/src/main/java/lc/captchas/ShadowTextCaptcha.java +++ b/src/main/java/lc/captchas/ShadowTextCaptcha.java @@ -13,7 +13,7 @@ import java.util.Map; import java.util.List; import lc.misc.HelperFunctions; -import lc.misc.DPISetter; +import lc.misc.PngImageWriter; import lc.captchas.interfaces.Challenge; import lc.captchas.interfaces.ChallengeProvider; @@ -67,8 +67,7 @@ public class ShadowTextCaptcha implements ChallengeProvider { g2d.dispose(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); try { - DPISetter dpi = new DPISetter(); - dpi.setDPI(baos, img2); + PngImageWriter.write(baos, img2); } catch (Exception e) { e.printStackTrace(); } diff --git a/src/main/java/lc/misc/DPISetter.java b/src/main/java/lc/misc/PngImageWriter.java similarity index 87% rename from src/main/java/lc/misc/DPISetter.java rename to src/main/java/lc/misc/PngImageWriter.java index 38b8c07..21b5034 100644 --- a/src/main/java/lc/misc/DPISetter.java +++ b/src/main/java/lc/misc/PngImageWriter.java @@ -15,12 +15,12 @@ import javax.imageio.metadata.IIOMetadataNode; import javax.imageio.stream.ImageOutputStream; import java.awt.image.BufferedImage; -public class DPISetter { +public class PngImageWriter { - final int DPI = 245; - final double INCH_2_CM = 2.54; + static final int DPI = 245; + static final double INCH_2_CM = 2.54; - public void setDPI(ByteArrayOutputStream boas, BufferedImage gridImage) throws IOException { + public static void write(ByteArrayOutputStream boas, BufferedImage gridImage) throws IOException { final String formatName = "png"; for (Iterator iw = ImageIO.getImageWritersByFormatName(formatName); iw.hasNext(); ) { @@ -45,7 +45,7 @@ public class DPISetter { } } - public void setDPIMeta(IIOMetadata metadata) throws IIOInvalidTreeException { + private static void setDPIMeta(IIOMetadata metadata) throws IIOInvalidTreeException { // for PNG, it's dots per millimeter double dotsPerMilli = 1.0 * DPI / 10 / INCH_2_CM; diff --git a/src/main/scala/lc/captchas/DebugCaptcha.scala b/src/main/scala/lc/captchas/DebugCaptcha.scala index 5a0f0f1..f0add52 100644 --- a/src/main/scala/lc/captchas/DebugCaptcha.scala +++ b/src/main/scala/lc/captchas/DebugCaptcha.scala @@ -11,7 +11,7 @@ import java.util.List import lc.misc.HelperFunctions import lc.captchas.interfaces.Challenge import lc.captchas.interfaces.ChallengeProvider -import lc.misc.DPISetter +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,8 +53,7 @@ class DebugCaptcha extends ChallengeProvider { graphics2D.dispose() val baos = new ByteArrayOutputStream() try { - val dpi = new DPISetter(); - dpi.setDPI(baos, img); + PngImageWriter.write(baos, img); } catch { case e: Exception => e.printStackTrace() diff --git a/src/main/scala/lc/captchas/LabelCaptcha.scala b/src/main/scala/lc/captchas/LabelCaptcha.scala index 7b85e06..18048f8 100644 --- a/src/main/scala/lc/captchas/LabelCaptcha.scala +++ b/src/main/scala/lc/captchas/LabelCaptcha.scala @@ -10,7 +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.DPISetter +import lc.misc.PngImageWriter class LabelCaptcha extends ChallengeProvider { private var knownFiles = new File("known").list.toList @@ -50,8 +50,7 @@ class LabelCaptcha extends ChallengeProvider { val token = encrypt(knownImageFile + "," + unknownImageFile) val baos = new ByteArrayOutputStream() - val dpi = new DPISetter(); - dpi.setDPI(baos, mergedImage); + PngImageWriter.write(baos, mergedImage); new Challenge(baos.toByteArray(), "image/png", token) } From b46f6795ce1dd91ff3c93e494d77aa2e3746d37c Mon Sep 17 00:00:00 2001 From: Rahul Rudragoudar Date: Thu, 2 Sep 2021 14:53:58 +0530 Subject: [PATCH 4/5] Set DPI for FilterCaptcha provider Signed-off-by: Rahul Rudragoudar --- src/main/scala/lc/captchas/FilterChallenge.scala | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/main/scala/lc/captchas/FilterChallenge.scala b/src/main/scala/lc/captchas/FilterChallenge.scala index 8c21e78..c7f2b70 100644 --- a/src/main/scala/lc/captchas/FilterChallenge.scala +++ b/src/main/scala/lc/captchas/FilterChallenge.scala @@ -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 From a0afba6fa56fa37526e1ab37afaa82cfc3110cb6 Mon Sep 17 00:00:00 2001 From: Rahul Rudragoudar Date: Thu, 2 Sep 2021 14:54:20 +0530 Subject: [PATCH 5/5] Remove print stmts Signed-off-by: Rahul Rudragoudar --- src/main/java/lc/misc/PngImageWriter.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/lc/misc/PngImageWriter.java b/src/main/java/lc/misc/PngImageWriter.java index 21b5034..e13b783 100644 --- a/src/main/java/lc/misc/PngImageWriter.java +++ b/src/main/java/lc/misc/PngImageWriter.java @@ -49,7 +49,6 @@ public class PngImageWriter { // for PNG, it's dots per millimeter double dotsPerMilli = 1.0 * DPI / 10 / INCH_2_CM; - System.out.println(dotsPerMilli); IIOMetadataNode horiz = new IIOMetadataNode("HorizontalPixelSize"); horiz.setAttribute("value", Double.toString(dotsPerMilli));