Example usage for javax.imageio ImageReadParam canSetSourceRenderSize

List of usage examples for javax.imageio ImageReadParam canSetSourceRenderSize

Introduction

In this page you can find the example usage for javax.imageio ImageReadParam canSetSourceRenderSize.

Prototype

boolean canSetSourceRenderSize

To view the source code for javax.imageio ImageReadParam canSetSourceRenderSize.

Click Source Link

Document

true if this ImageReadParam allows the source rendering dimensions to be set.

Usage

From source file:org.jimcat.services.imagemanager.ImageUtil.java

/**
 * use this methode to load an image from a given byte array
 * //  w  ww .  jav  a2  s  .c o  m
 * @param data -
 *            the byte array containing an encoded image
 * @param quality
 *            the rendering quality
 * @return - a Buffered Image containing image. Its size is limited by
 *         SOURCE_BOUNDING_BOX constant
 * @throws IOException -
 *             if something goes wrong
 */
public static BufferedImage loadImage(byte[] data, ImageQuality quality) throws IOException {

    // read first element
    ImageReader reader = getReaderForImage(data);

    // get image dimension and calculate resulting image size
    int width = reader.getWidth(0);
    int height = reader.getHeight(0);
    Dimension size = getScaledDimension(width, height, SOURCE_BOUNDING_BOX, false);

    // performe read
    BufferedImage bi = null;
    try {
        if (USE_TILES) {
            // if image is smaller than a tile
            if (width <= IMAGE_TILE_SIZE.width && height <= IMAGE_TILE_SIZE.height) {
                bi = reader.read(0);
            } else {
                // prepaire reader
                ImageReadParam param = reader.getDefaultReadParam();

                // so image is bigger than a tile
                // a) check if reader supports source scaling
                if (param.canSetSourceRenderSize()) {
                    // fine => do it so
                    param.setSourceRenderSize(size);
                    bi = reader.read(0, param);
                } else {
                    // so, scaling has to be done by hand
                    bi = loadImageWithTiles(reader, size, quality);
                }
            }
        } else {
            bi = loadImageWithSubSampling(reader, size, quality);
        }
    } finally {
        reader.dispose();
    }
    return bi;
}