Quality Image Resizing with JAI

ThumbMaster Reference: resizeImageToHeight

Resizes an image so that the result has the specified height. The width will be variable, and depends on the aspect ratio of the initial image.

Declaration

static RenderedImage resizeImageToHeight(source, height)

Parameters

source
A RenderedImage object containing the source image that is to be resized.
height
The height to which the image should be resized. The result image will have a height that is exactly equal to this value.

Details

The new image’s height is guaranteed to be equal to the requested value. The width will be scaled by the same amount that the height was, and will vary depending on the source image’s aspect ratio. The following table contains example image dimensions, and what size image will be created by a call to this method.

Original Size Requested Width New Size
800×800 200 200×200
800×400 200 400×200
400×800 200 100×200

Example Usage

The following example shows how to load an image file, resize it to fit inside a 150 pixel tall row, and then save it as a new image:

import net.devella.thumbs.ThumbMaster;

...
// read source image from a file
RenderedImage source =
    ImageIO.read(new File("/tmp/source.png"));

// resize the image
RenderedImage result =
    ThumbMaster.resizeImageToHeight(source, 150);

// write the image to a new file
ImageIO.write(result, "png", new File("/tmp/result.png"));
...
Example 1: Resizing images using resizeImageToHeight

© 2008 Sean Kleinjung. All Rights Reserved.