Rename DPISetter to PngImageWritter

Make fn static

Signed-off-by: Rahul Rudragoudar <rr83019@gmail.com>
This commit is contained in:
Rahul Rudragoudar 2021-09-01 22:57:26 +05:30
parent 26d86bca4c
commit f83289514b
No known key found for this signature in database
GPG Key ID: 0D2CE231A7287EBC
5 changed files with 13 additions and 17 deletions

View File

@ -9,7 +9,7 @@ import java.util.Map;
import java.util.List; import java.util.List;
import lc.captchas.interfaces.Challenge; import lc.captchas.interfaces.Challenge;
import lc.captchas.interfaces.ChallengeProvider; import lc.captchas.interfaces.ChallengeProvider;
import lc.misc.DPISetter; import lc.misc.PngImageWriter;
import lc.misc.HelperFunctions; import lc.misc.HelperFunctions;
public class FontFunCaptcha implements ChallengeProvider { public class FontFunCaptcha implements ChallengeProvider {
@ -74,8 +74,7 @@ public class FontFunCaptcha implements ChallengeProvider {
graphics2D.dispose(); graphics2D.dispose();
ByteArrayOutputStream baos = new ByteArrayOutputStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream();
try { try {
DPISetter dpi = new DPISetter(); PngImageWriter.write(baos, img);
dpi.setDPI(baos, img);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }

View File

@ -13,7 +13,7 @@ import java.util.Map;
import java.util.List; import java.util.List;
import lc.misc.HelperFunctions; import lc.misc.HelperFunctions;
import lc.misc.DPISetter; import lc.misc.PngImageWriter;
import lc.captchas.interfaces.Challenge; import lc.captchas.interfaces.Challenge;
import lc.captchas.interfaces.ChallengeProvider; import lc.captchas.interfaces.ChallengeProvider;
@ -67,8 +67,7 @@ public class ShadowTextCaptcha implements ChallengeProvider {
g2d.dispose(); g2d.dispose();
ByteArrayOutputStream baos = new ByteArrayOutputStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream();
try { try {
DPISetter dpi = new DPISetter(); PngImageWriter.write(baos, img2);
dpi.setDPI(baos, img2);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }

View File

@ -15,12 +15,12 @@ import javax.imageio.metadata.IIOMetadataNode;
import javax.imageio.stream.ImageOutputStream; import javax.imageio.stream.ImageOutputStream;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
public class DPISetter { public class PngImageWriter {
final int DPI = 245; static final int DPI = 245;
final double INCH_2_CM = 2.54; 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"; final String formatName = "png";
for (Iterator<ImageWriter> iw = ImageIO.getImageWritersByFormatName(formatName); for (Iterator<ImageWriter> iw = ImageIO.getImageWritersByFormatName(formatName);
iw.hasNext(); ) { 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 // for PNG, it's dots per millimeter
double dotsPerMilli = 1.0 * DPI / 10 / INCH_2_CM; double dotsPerMilli = 1.0 * DPI / 10 / INCH_2_CM;

View File

@ -11,7 +11,7 @@ import java.util.List
import lc.misc.HelperFunctions import lc.misc.HelperFunctions
import lc.captchas.interfaces.Challenge import lc.captchas.interfaces.Challenge
import lc.captchas.interfaces.ChallengeProvider 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 */ /** 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 { class DebugCaptcha extends ChallengeProvider {
@ -53,8 +53,7 @@ class DebugCaptcha extends ChallengeProvider {
graphics2D.dispose() graphics2D.dispose()
val baos = new ByteArrayOutputStream() val baos = new ByteArrayOutputStream()
try { try {
val dpi = new DPISetter(); PngImageWriter.write(baos, img);
dpi.setDPI(baos, img);
} catch { } catch {
case e: Exception => case e: Exception =>
e.printStackTrace() e.printStackTrace()

View File

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