package net.coobird.thumbnailator.makers; import java.awt.image.BufferedImage; /** * A {@link ThumbnailMaker} which scales an image by a specified scaling factor * when producing a thumbnail. *
* Upon calculating the size of the thumbnail, if any of the dimensions are * {@code 0}, then that dimension will be promoted to {@code 1}. This will * cause some resizing operations to not preserve the aspect ratio of the * original image. *
*
BufferedImage img = ImageIO.read(new File("sourceImage.jpg")); BufferedImage thumbnail = new ScaledThumbnailMaker() .scale(0.25) .make(img); **
*
BufferedImage img = ImageIO.read(new File("sourceImage.jpg")); BufferedImage thumbnail = new ScaledThumbnailMaker() .scale(0.50, 0.75) .make(img); **
* Creates an instance of {@code ScaledThumbnailMaker} without the * scaling factor specified. *
** To use this {@code ScaledThumbnailMaker}, one must specify the * scaling factor to use by calling the {@link #scale(double)} method * before generating a thumbnail. *
*/ public ScaledThumbnailMaker() { super(); ready.unset(PARAM_SCALE); } /** * Creates an instance of {@code ScaledThumbnailMaker} with the specified * scaling factor. * * @param factor The scaling factor to apply when resizing an * image to create a thumbnail. */ public ScaledThumbnailMaker(double factor) { this(); scale(factor); } /** * Creates an instance of {@code ScaledThumbnailMaker} with the specified * scaling factors for the width and height. * * @param widthFactor The scaling factor to apply to the width when * resizing an image to create a thumbnail. * @param heightFactor The scaling factor to apply to the height when * resizing an image to create a thumbnail. * @since 0.3.10 */ public ScaledThumbnailMaker(double widthFactor, double heightFactor) { this(); scale(widthFactor, heightFactor); } /** ** Sets the scaling factor for the thumbnail. *
** The aspect ratio of the resulting image is unaltered from the original. *
* * @param factor The scaling factor to apply when resizing an * image to create a thumbnail. * @return A reference to this object. * @throws IllegalStateException If the scaling factor has already * been previously set. */ public ScaledThumbnailMaker scale(double factor) { return scale(factor, factor); } /** ** Sets the scaling factors for the thumbnail. *
* * @param widthFactor The scaling factor to apply to the width when * resizing an image to create a thumbnail. * @param heightFactor The scaling factor to apply to the height when * resizing an image to create a thumbnail. * @return A reference to this object. * @throws IllegalStateException If the scaling factor has already * been previously set. * @since 0.3.10 */ public ScaledThumbnailMaker scale(double widthFactor, double heightFactor) { if (ready.isSet(PARAM_SCALE)) { throw new IllegalStateException( "The scaling factor has already been set." ); } if (widthFactor <= 0 || heightFactor <= 0) { throw new IllegalArgumentException( "The scaling factor must be greater than zero." ); } this.widthFactor = widthFactor; this.heightFactor = heightFactor; ready.set(PARAM_SCALE); return this; } @Override public BufferedImage make(BufferedImage img) { int width = (int)Math.round(img.getWidth() * widthFactor); int height = (int)Math.round(img.getHeight() * heightFactor); width = (width == 0) ? 1 : width; height = (height == 0) ? 1 : height; return super.makeThumbnail(img, width, height); } }