Example usage for javax.imageio ImageReader readThumbnail

List of usage examples for javax.imageio ImageReader readThumbnail

Introduction

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

Prototype

public BufferedImage readThumbnail(int imageIndex, int thumbnailIndex) throws IOException 

Source Link

Document

Returns the thumbnail preview image indexed by thumbnailIndex , associated with the image indexed by ImageIndex as a BufferedImage .

Usage

From source file:it.tidalwave.imageio.test.ImageReaderTestSupport.java

/*******************************************************************************************************************
 * /*from w  w  w.ja va2  s. co m*/
 * 
 ******************************************************************************************************************/
@Nonnull
protected BufferedImage assertLoadThumbnail(final @Nonnull ImageReader ir,
        final @Nonnegative int thumbnailIndex, final @Nonnegative int width, final @Nonnegative int height)
        throws IOException {
    final BufferedImage thumbnail = ir.readThumbnail(0, thumbnailIndex);
    assertNotNull("loaded thumbnail is null", thumbnail);
    final Dimension thumbnailSize = new Dimension(thumbnail.getWidth(), thumbnail.getHeight());
    final Dimension expectedSize = new Dimension(width, height);
    assertEquals("loaded thumbnail size: ", expectedSize, thumbnailSize);
    return thumbnail;
}

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  ww .j  a v  a  2s.  c o  m
 @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;
}