David Thielen

For search engines (ignore): java report .net report java reporting .net reporting .net reports java reports


A program architected by David Thielen
Home On Programming Articles, Etc. Blogs Resume

Querying the bitmap resolution

Bitmap independent method of determining the bitmap pixel size

SUMMARY

Most advanced bitmap formats include a setting for the number of dots per inch (dpi). This "dimension" determines how large the bitmap should be when displayed. This is one of those tasks that is easy (sort-of) once you figure it out. However, SUN has the necessary information pretty much hidden making it difficult to figure it out.

Most graphics formats (including jpg and png - but not gif) have a Setting for the image dimension in the x and y direction. This is how Many pixels should be drawn for each inch (dpi). It is critical to use this, otherwise a 300x300 pixel image will be 1"x1" on a 300 dpi printer And 1/4" x 1/4" on a 1200 dpi printer.

Please note, this code works only on java 1.4.

The program is written to provide both a callable static method and to Run from the command line. The callable method will accept File and InputStream objects. (And alsmost certainly some other objects – the Docs are not clear on this.)

import javax.imageio.ImageIO;

import javax.imageio.ImageReader;

import javax.imageio.metadata.IIOMetadata;

import javax.imageio.stream.ImageInputStream;

import java.awt.*;

import java.util.Iterator;

import java.io.File;

/**

 * Most graphics formats (including jpg and png - but not gif) have a

 * Setting for the image dimension in the x and y direction. This is how

 * Many pixels should be drawn for each inch (dpi). It is critical to use

 * This, otherwise a 300x300 pixel image will be 1"x1" on a 300 dpi printer

 * And 1/4" x 1/4" on a 1200 dpi printer.

 *

 * Please note, this code works only on java 1.4.

 * User: dave

 * Date: Mar 15, 2003

 * Time: 9:21:57 PM

 */

public class BitmapResolution {

    /**

     * Return the dpi of this image. If it cannot be determined, returns 72x72.

     *

     * @param fil The bitmap object to return the dpi of. Can be a File or InputStream.

     *

     * @return The dimension of the bitmap.

     */

    public static Point getDpi(Object fil) {

 

        int xDPI = 72;

        int yDPI = 72;

        try {

            // get a reader that can read this bitmap type

            ImageInputStream imageInput = ImageIO.createImageInputStream(fil);

            Iterator it = (Iterator) ImageIO.getImageReaders(imageInput);

            if (!it.hasNext())

                return new Point(72, 72);

            ImageReader reader = (ImageReader) it.next();

 

            reader.setInput(imageInput);

            IIOMetadata meta = reader.getImageMetadata(0);

            org.w3c.dom.Node n = meta.getAsTree("javax_imageio_1.0");

 

            n = n.getFirstChild();

            while (n != null) {

                if (n.getNodeName().equals("Dimension")) {

                    org.w3c.dom.Node n2 = n.getFirstChild();

                    while (n2 != null) {

                        if (n2.getNodeName().equals("HorizontalPixelSize")) {

                            org.w3c.dom.NamedNodeMap nnm = n2.getAttributes();

                            org.w3c.dom.Node n3 = nnm.item(0);

                            float hps = Float.parseFloat(n3.getNodeValue());

                            xDPI = Math.round(25.4f / hps);

                        }

                        if (n2.getNodeName().equals("VerticalPixelSize")) {

                            org.w3c.dom.NamedNodeMap nnm = n2.getAttributes();

                            org.w3c.dom.Node n3 = nnm.item(0);

                            float vps = Float.parseFloat(n3.getNodeValue());

                            yDPI = Math.round(25.4f / vps);

                        }

                        n2 = n2.getNextSibling();

                    }

                }

                n = n.getNextSibling();

            }

 

        } catch (Exception e) {

            // just use 72, 72

            e.printStackTrace();

        }

 

    return new Point(xDPI, yDPI);

    }

 

    /**

     * A commandline utility to get the dimensions of a bitmap file. The usage is

     * "java BitmapResolution filename".

     *

     * @param args The filename to get the resolution of.

     */

    public static void main(String[] args) {

 

        if (args.length != 1) {

            System.out.println("usage: BitmapResolution filename");

            System.exit(1);

        }

        

        Point pt = getDpi(new File(args[0]));

 

        System.out.println("Resolution of " + args[0] + " is " + pt.x + "x" + pt.y);

    }

}

RESOURCES

Source code: BitmapResolution.java

Article: Aging bugs and setting dpi with java image io

Standard (Plug-in Neutral) Metadata Format Specification: http://java.sun.com/j2se/1.4/docs/api/javax/imageio/metadata/doc-files/standard_metadata.html