How to get the dpi of an image(Java)

后端 未结 7 1884
无人及你
无人及你 2021-01-06 01:51

In c#, we can use Image.HorizontalResolution and Image.VerticalResolution.

But in java, how to get it?

I found ImageInfo.java, but it only supp

相关标签:
7条回答
  • 2021-01-06 01:56

    It is working for me.

     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 Depndency

     <!-- https://mvnrepository.com/artifact/org.apache.sanselan/sanselan -->
        <dependency>
            <groupId>org.apache.sanselan</groupId>
            <artifactId>sanselan</artifactId>
            <version>0.97-incubator</version>
        </dependency>
    
    0 讨论(0)
  • 2021-01-06 02:01

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

    final ImageInfo imageInfo = Sanselan.getImageInfo(file_);
    
    final int physicalWidthDpi = imageInfo.getPhysicalWidthDpi();
    final int physicalHeightDpi = imageInfo.getPhysicalHeightDpi();
    
    0 讨论(0)
  • 2021-01-06 02:02

    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

    0 讨论(0)
  • 2021-01-06 02:02

    I found this example interesting:

    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;
    */
    
    0 讨论(0)
  • 2021-01-06 02:03

    Find dpi of .bmp image use :

    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 Dependency :

            <!-- https://mvnrepository.com/artifact/com.lowagie/itext -->
            <dependency>
                <groupId>com.lowagie</groupId>
                <artifactId>itext</artifactId>
                <version>2.1.7</version>
            </dependency>     
    
    0 讨论(0)
  • 2021-01-06 02:13

    With the help of an ImageReader instance, you can get the image meta data in a neutral format, and then parse it for what you need. A DTD is 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: -");
    

    (Source/Inspiration: David Thielen)

    Note that you will get a dpi only if it is there.

    If you wonder what's in the Metadata XML, use this code:

        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);
    
    0 讨论(0)
提交回复
热议问题