在 Java 中确定图像的 DPI

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20096810/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 23:07:46  来源:igfitidea点击:

Determine DPI of Image in Java

javajai

提问by Michael

I have a TIFF image that has been read in to my application and is stored in a BufferedImageobject. How can I determine the horizontal and vertical DPI of the image using the Java Advanced Imaging (JAI) APIs? I have been looking around and not been able to find a straight forward way to accomplish this.

我有一个 TIFF 图像已被读入我的应用程序并存储在一个BufferedImage对象中。如何使用 Java Advanced Imaging (JAI) API 确定图像的水平和垂直 DPI?我一直在环顾四周,但无法找到一种直接的方法来实现这一目标。

采纳答案by haraldK

Here's a full example extracting DPI (well, pixels per mm, really) using the standard ImageIO API and the standard metadata format. Complexity, here we come... :-P

这是使用标准 ImageIO API 和标准元数据格式提取 DPI(嗯,实际上是每毫米像素)的完整示例。复杂性,我们来了... :-P

public class DPITest {
    public static void main(String[] args) throws IOException {
        File input = new File(args[0]);

        ImageInputStream stream = ImageIO.createImageInputStream(input);
        Iterator<ImageReader> readers = ImageIO.getImageReaders(stream);

        if (readers.hasNext()) {
            ImageReader reader = readers.next();
            reader.setInput(stream);

            IIOMetadata metadata = reader.getImageMetadata(0);
            IIOMetadataNode standardTree = (IIOMetadataNode) metadata.getAsTree(IIOMetadataFormatImpl.standardMetadataFormatName);
            IIOMetadataNode dimension = (IIOMetadataNode) standardTree.getElementsByTagName("Dimension").item(0);
            float horizontalPixelSizeMM = getPixelSizeMM(dimension, "HorizontalPixelSize");
            float verticalPixelSizeMM = getPixelSizeMM(dimension, "VerticalPixelSize");

            // TODO: Convert pixelsPerMM to DPI left as an exercise to the reader.. ;-)  

            System.err.println("horizontalPixelSizeMM: " + horizontalPixelSizeMM);
            System.err.println("verticalPixelSizeMM: " + verticalPixelSizeMM);
        }
        else {
            System.err.printf("Could not read %s\n", input);
        }
    }

    private static float getPixelSizeMM(final IIOMetadataNode dimension, final String elementName) {
        // NOTE: The standard metadata format has defined dimension to pixels per millimeters, not DPI...
        NodeList pixelSizes = dimension.getElementsByTagName(elementName);
        IIOMetadataNode pixelSize = pixelSizes.getLength() > 0 ? (IIOMetadataNode) pixelSizes.item(0) : null;
        return pixelSize != null ? Float.parseFloat(pixelSize.getAttribute("value")) : -1;
    }
}

Some sources to read:

一些阅读来源:

回答by constantlearner

Apache Commons Sanselan library to get image info: http://commons.apache.org/imaging/index.html.

用于获取图像信息的 Apache Commons Sanselan 库:http://commons.apache.org/imaging/index.html 。

final ImageInfo imageInfo = Sanselan.getImageInfo(file);

final int physicalWidthDpi = imageInfo.getPhysicalWidthDpi();
final int physicalHeightDpi = imageInfo.getPhysicalHeightDpi();