实际上这是获取图像dpi的代码。它确实适用,但仅适用于较小尺寸的jpeg文件而不适用于bmp文件。我想获取bmp文件dpi。
我也尝试过bufferedimage来读取文件,但是它不起作用。
ImageInputStream iis = ImageIO.createImageInputStream(new File("MAR.BMP"));
Iterator it = ImageIO.getImageReaders(iis);
if (!it.hasNext())
{
System.err.println("No reader for this format");
}
ImageReader reader = (ImageReader) it.next();
reader.setInput(iis);
IIOMetadata meta = reader.getImageMetadata(0);
IIOMetadataNode root = (IIOMetadataNode) meta.getAsTree("javax_imageio_1.0");
NodeList nodes = root.getElementsByTagName("HorizontalPixelSize");
if (nodes.getLength() >0)
{
IIOMetadataNode dpcWidth = (IIOMetadataNode) nodes.item(0);
NamedNodeMap nnm = dpcWidth.getAttributes();
Node item = nnm.item(0);
int xDPI = Math.round(25.4f / Float.parseFloat(item.getNodeValue()));
System.out.println("xDPI: " + xDPI);
}
else
System.out.println("xDPI: -");
if (nodes.getLength() >0)
{
nodes = root.getElementsByTagName("VerticalPixelSize");
IIOMetadataNode dpcHeight = (IIOMetadataNode) nodes.item(0);
NamedNodeMap nnm = dpcHeight.getAttributes();
Node item = nnm.item(0);
int yDPI = Math.round(25.4f / Float.parseFloat(item.getNodeValue()));
System.out.println("yDPI: " + yDPI);
}
else
System.out.println("yDPI: -");
}
}