Example usage for javax.imageio ImageReadParam setSourceRenderSize

List of usage examples for javax.imageio ImageReadParam setSourceRenderSize

Introduction

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

Prototype

public void setSourceRenderSize(Dimension size) throws UnsupportedOperationException 

Source Link

Document

If the image is able to be rendered at an arbitrary size, sets the source width and height to the supplied values.

Usage

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

/**
 * use this methode to load an image from a given byte array
 * //from  ww  w . j a v a2s.c om
 * @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;
}