如何获取图像的dpi(Java)

时间:2011-08-22 02:18:32

标签: java image dpi

在c#中,我们可以使用Image.Horizo​​ntalResolution和Image.VerticalResolution。

但在java中,如何获得它?

我找到ImageInfo.java,但它只支持一些图片类型。

http://kickjava.com/src/imageinfo/ImageInfo.java.htm

7 个答案:

答案 0 :(得分:6)

您可以使用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();

答案 1 :(得分:4)

在ImageReader实例的帮助下,您可以以中性格式获取图像元数据,然后根据需要对其进行解析。 DTD是here

    ImageInputStream iis = ImageIO.createImageInputStream(new File(path));
    Iterator it = ImageIO.getImageReaders(iis);
    if (!it.hasNext())
    {
        System.err.println("No reader for this format");
        return;
    }
    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: -");

(来源/灵感:David Thielen

请注意,只有在那里才能获得dpi。

如果您想知道元数据XML中的内容,请使用以下代码:

    StringWriter xmlStringWriter = new StringWriter();
    StreamResult streamResult = new StreamResult(xmlStringWriter);
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes"); // http://stackoverflow.com/a/1264872/535646
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
    DOMSource domSource = new DOMSource(root);
    transformer.transform(domSource, streamResult);
    System.out.println (xmlStringWriter);

答案 2 :(得分:0)

获取ImageReader实例。然后使用第一个ImageReader, 设置输入和读取IIOImage或仅设置getImageMetadata(pageIndex)。 你得到图像格式中性元数据xml并解析它 想要的数据。

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();

// ...每厘米计算点数到dpi:dpi = dpc / 2.54

的整个图像中性元数据格式

答案 3 :(得分:0)

它正在为我工​​作。

 try {

        final ImageInfo imageInfo = Sanselan.getImageInfo(new File("C:/Users/AngryMan/Desktop/abc.png"));
        final int physicalWidthDpi = imageInfo.getPhysicalWidthDpi();
        final int physicalHeightDpi = imageInfo.getPhysicalHeightDpi();
        System.out.println("physicalWidthDpi :"+physicalWidthDpi );
        System.out.println("physicalHeightDpi : "+physicalHeightDpi);

    } catch (Exception e) {
        e.printStackTrace();
    }

Maven依赖

 <!-- https://mvnrepository.com/artifact/org.apache.sanselan/sanselan -->
    <dependency>
        <groupId>org.apache.sanselan</groupId>
        <artifactId>sanselan</artifactId>
        <version>0.97-incubator</version>
    </dependency>

答案 4 :(得分:0)

查找.bmp图像使用的dpi:

import com.lowagie.text.Image.

    public class BitmapResolution {
        public static void main(String args[]) {
            try {
                Image img = Image.getInstance("C:/Users/AngryMan/Desktop/img003.bmp");
                System.out.println(img.getDpiX());
                System.out.println(img.getDpiY());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

Maven依赖关系:

        <!-- https://mvnrepository.com/artifact/com.lowagie/itext -->
        <dependency>
            <groupId>com.lowagie</groupId>
            <artifactId>itext</artifactId>
            <version>2.1.7</version>
        </dependency>     

答案 5 :(得分:-1)

ImageMagick是一个功能强大的工具,适用于所有与图像相关的工作。 IM需要安装并需要对环境进行一些配置,但这是值得的。

http://www.imagemagick.org

我建议您使用JMagick wit IM:

http://www.jmagick.org

我不会解释如何的详细信息,因为它是在给定的网址中记录的。

答案 6 :(得分:-2)

我发现这个例子很有趣:

ByteArrayInputStream bis = new 
   ByteArrayInputStream(uploadedFile.getContents());
Iterator<?> readers = ImageIO.getImageReadersByFormatName("jpg");
ImageReader reader = (ImageReader) readers.next();
IIOMetadata meta = reader.getImageMetadata(0);
Element tree = (Element) meta.getAsTree("javax_imageio_jpeg_image_1.0");
Element jfif = (Element)tree.getElementsByTagName("app0JFIF").item(0);
int dpiH = Integer.parseInt( jfif.getAttribute("Xdensity") );
int dpiV = Integer.parseInt( jfif.getAttribute("Ydensity") );

/* now test that (dpiH == dpiV) */
/* imports are:
import javax.imageio.ImageIO;
import javax.imageio.ImageReadParam;
import javax.imageio.ImageReader;
import javax.imageio.metadata.IIOMetadata;
import javax.imageio.stream.ImageInputStream;
import org.primefaces.model.UploadedFile;
import org.w3c.dom.Element;
*/