Example usage for javax.imageio ImageReader getNumThumbnails

List of usage examples for javax.imageio ImageReader getNumThumbnails

Introduction

In this page you can find the example usage for javax.imageio ImageReader getNumThumbnails.

Prototype

public int getNumThumbnails(int imageIndex) throws IOException 

Source Link

Document

Returns the number of thumbnail preview images associated with the given image.

Usage

From source file:org.photovault.image.ImageIOImage.java

/**
 Read the image (either original or proper thumbnail in the same file and subsample 
 it to save memory & time. The image is subsampled so that its reasolution is the
 smallest possible that is bigger than given limits. 
         /*from   w  w  w .j a  v a2s.  c om*/
 @param reader The image reader that is used for reading the image
 @param minWidth Minimum width of the subsampled image
 @param minHeight Minimum height of the subsampled iamge
         
 @return Subsampled image.
 */

private RenderedImage readSubsampled(ImageReader reader, int minWidth, int minHeight) throws IOException {
    /*
     We try to ensure that the thumbnail is actually from the original image
     by comparing aspect ratio of it to original. This is not a perfect check
     but it will usually catch the most typical errors (like having a the original
     rotated by RAW conversion SW but still the original EXIF thumbnail.
     */
    double origAspect = reader.getAspectRatio(0);
    double aspectAccuracy = 0.01;
    int minInstanceSide = Math.max(minWidth, minHeight);

    int numThumbs = 0;
    RenderedImage image = null;
    try {
        int numImages = reader.getNumImages(true);
        if (numImages > 0) {
            numThumbs = reader.getNumThumbnails(0);
        }
    } catch (IOException ex) {
        ex.printStackTrace();
    }
    if (numThumbs > 0 && isOkForThumbCreation(reader.getThumbnailWidth(0, 0), reader.getThumbnailHeight(0, 0),
            minWidth, minHeight, origAspect, aspectAccuracy)) {
        // There is a thumbanil that is big enough - use it

        log.debug("Original has thumbnail, size " + reader.getThumbnailWidth(0, 0) + " x "
                + reader.getThumbnailHeight(0, 0));
        image = reader.readThumbnail(0, 0);
        log.debug("Read thumbnail");
    } else {
        log.debug("No thumbnail in original");
        ImageReadParam param = reader.getDefaultReadParam();

        // Find the maximum subsampling rate we can still use for creating
        // a quality thumbnail. Some image format readers seem to have
        // problems with subsampling values (e.g. PNG sometimes crashed
        // the whole virtual machine, to for now let's do this only
        // with JPG.
        int subsampling = 1;
        if (reader.getFormatName().equals("JPEG")) {
            int minDim = Math.min(reader.getWidth(0), reader.getHeight(0));
            while (2 * minInstanceSide * subsampling < minDim) {
                subsampling *= 2;
            }
        }
        param.setSourceSubsampling(subsampling, subsampling, 0, 0);
        image = reader.read(0, param);
    }
    return image;
}