Add DPI setter
Signed-off-by: Rahul Rudragoudar <rr83019@gmail.com>
This commit is contained in:
parent
1a41724fdc
commit
d30249a89f
|
@ -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<ImageWriter> 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);
|
||||
}
|
||||
}
|
|
@ -7,7 +7,7 @@ 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);
|
||||
}
|
||||
|
||||
|
@ -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) {
|
||||
public static synchronized int randomNumber(int bound) {
|
||||
return random.nextInt(bound);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue