How to increase Image DPI?
HarshaSKNov 27 2012 — edited Nov 28 2012Hi All,
I increased the DPI of a image to 299.99 DPI from 96 DPI. It displays 299.99 DPI in the image property but it doesnt have any effect on the image clarity. The image is still blur on zoom. How to increase the DPI so that the image clarity increases?
I have used the below logic -
String dotsPerMeter = String.valueOf((int) (300 / 0.0254));//300 is the dpi required
Iterator imageWriters = ImageIO.getImageWritersByFormatName("png");
while (imageWriters.hasNext()) {
ImageWriter iw = (ImageWriter)imageWriters.next();
ImageWriteParam iwp = iw.getDefaultWriteParam();
IIOMetadata metadata = iw.getDefaultImageMetadata(new ImageTypeSpecifier(image), iwp);
String pngFormatName = metadata.getNativeMetadataFormatName();
IIOMetadataNode pngNode =
(IIOMetadataNode) metadata.getAsTree(pngFormatName);
IIOMetadataNode physNode = null;
NodeList childNodes = pngNode.getElementsByTagName("pHYs");
if (childNodes.getLength() == 0) {
physNode = new IIOMetadataNode("pHYs");
pngNode.appendChild(physNode);
} else if (childNodes.getLength() == 1) {
physNode = (IIOMetadataNode) childNodes.item(0);
} else {
throw new IllegalStateException("Don't know what to do with multiple pHYs nodes");
}
physNode.setAttribute("pixelsPerUnitXAxis", dotsPerMeter);
physNode.setAttribute("pixelsPerUnitYAxis", dotsPerMeter);
physNode.setAttribute("unitSpecifier", "meter");
try {
metadata.setFromTree(pngFormatName, pngNode);
IIOImage iioImage = new IIOImage(image, null, metadata);
File file = new File(KIT_ID + "_" + KIT_VER + "_newDPI.png");
ImageOutputStream ios = ImageIO.createImageOutputStream(file);
iw.setOutput(ios);
iw.write(iioImage);
ios.flush();
ios.close();
} catch (Exception e) {
e.printStackTrace();
continue;
}
break;
}
Please someone, help me solve this.
Thanks a ton.