Quality Image Resizing with JAI

ThumbMaster Reference: resizeImageToWidth

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

Declaration

static RenderedImage resizeImageToWidth(source, width)

Parameters

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

Details

The new image’s width is guaranteed to be equal to the requested value. The height will be scaled by the same amount that the width 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 200×100
400×800 200 200×400

Example Usage

The following example shows how to load an image file, resize it to fit inside a 150 pixel wide column, 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.resizeImageToWidth(source, 150);

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

© 2008 Sean Kleinjung. All Rights Reserved.