From d30249a89fef4c0430859e78374f53809aec7f1b Mon Sep 17 00:00:00 2001 From: Rahul Rudragoudar Date: Tue, 31 Aug 2021 15:42:57 +0530 Subject: [PATCH] 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); } - }