Skip to main content

Find out DPI of an image

2 replies [Last post]
spgmx
Offline
Joined: 2009-05-10

Hi,

I read images with ImageIO.read(...).
How can I find out the DPI of the image?

Thank you

P.S. The image is in 90 % of all cases a Tiff with Jpeg compression (old and new).

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
marcojacob
Offline
Joined: 2006-07-10

You should go a different way to read the metadata.
Get your ImageReader instance. Then use the first ImageReader,
set the Input and read IIOImage or only getImageMetadata(pageIndex).
You get the image format neutral metadata xml and parse it for the
desired data.

ImageInputStream iis = ImageIO.createImageInputStream(in);
Iterator it = ImageIO.getImageReaders(iis);
if (!it.hasNext()) {
System.outprintln("No reader for this format");
}
ImageReader reader = (ImageReader) it.next();
reader.setInput(iis);
IIOMetadata meta = reader.getImageMetadata(0);
IIOMetadataNode dimNode = meta.getStandardDimensionNode();
NodeList nodes = dimNode.getElementsByTagName("HorizontalPixelSize");
IIOMetadataNode dpcWidth = (IIOMetadataNode)nodes.nextElement();
nodes = dimNode.getElementsByTagName("VerticalPixelSize");
IIOMetadataNode dpcHeight = (IIOMetadataNode)nodes.nextElement();
// ... calc dot per centimeter to dpi : dpi = dpc / 2.54

The whole image neutral metadata format at

http://java.sun.com/j2se/1.4.2/docs/api/javax/imageio/metadata/doc-files...

spgmx
Offline
Joined: 2009-05-10

Thank you!