Question

I am reading in and processing TIF images using ImageIO and JAI. The results are all working perfectly, except that a number of the TIF images do not have square pixels. The aspect ratio of the pixels is being lost during the processing so the resulting image looks stretched.

I found this question which reads out the resolution in C#: Change tiff pixel aspect ratio to square but I cannot find any equivalent in java.

Does anyone know how either to read the horizontal and vertical resolution (not size) of a BufferedImage and/or TIF Image in Java or cause JAI to scale the image as it loads it so that the resulting pixels are square?

Was it helpful?

Solution

After an hour of Googling and trying things I think I have found a solution.

    IIOMetadata iiom = ir.getImageMetadata(i);
    TIFFDirectory dir = TIFFDirectory.createFromMetadata(iiom);

    TIFFField fieldXRes = dir.getTIFFField(BaselineTIFFTagSet.TAG_X_RESOLUTION);
    TIFFField fieldYRes = dir.getTIFFField(BaselineTIFFTagSet.TAG_Y_RESOLUTION);

    int xRes = fieldXRes.getAsInt(0);
    int yRes = fieldYRes.getAsInt(0);

OTHER TIPS

As an alternative, you can also get the same values from the Standard Metadata, if you don't want to rely on the JAI API (or TIFF format specifics at all).

The Dimension element has the child elements HorizontalPixelSize and VerticalPixelSize which should be equivalent to the values you got from the TIFF tags above, as well as a PixelAspectRatio you could use directly.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top