Class FixedSizeThumbnailMaker


  • public final class FixedSizeThumbnailMaker
    extends ThumbnailMaker

    A ThumbnailMaker which resizes an image to a specified dimension when producing a thumbnail.

    Optionally, if the aspect ratio of the thumbnail is to be maintained the same as the original image (by calling the keepAspectRatio(boolean) method with the value true), then the dimensions specified by the size(int, int) method, FixedSizeThumbnailMaker(int, int) or FixedSizeThumbnailMaker(int, int, boolean) constructor will be used as the maximum constraint of dimensions of the thumbnail.

    In other words, when the aspect ratio is to be kept constant, then thumbnails which are created will be sized to fit inside the dimensions specified by the size parameter.

    Upon calculating the size of the thumbnail, if any of the dimensions are 0, then that dimension will be promoted to 1, regardless of whether the aspect ratio of the original image is to be maintained. This will lead to some thumbnails not preserving the aspect ratio of the original image, even if keepAspectRatio(boolean) has been true.

    Usage:
    The following example demonstrates how to create a thumbnail which fits within 200 pixels by 200 pixels, while preserving the aspect ratio of the source image:
    BufferedImage img = ImageIO.read(new File("sourceImage.jpg"));
    
    BufferedImage thumbnail = new FixedSizeThumbnailMaker()
            .size(200, 200)
            .keepAspectRatio(true)
            .make(img);
     
    • Field Detail

      • PARAM_KEEP_RATIO

        private static final java.lang.String PARAM_KEEP_RATIO
        See Also:
        Constant Field Values
      • PARAM_FIT_WITHIN

        private static final java.lang.String PARAM_FIT_WITHIN
        See Also:
        Constant Field Values
      • width

        private int width
      • height

        private int height
      • keepRatio

        private boolean keepRatio
      • fitWithinDimensions

        private boolean fitWithinDimensions
    • Constructor Detail

      • FixedSizeThumbnailMaker

        public FixedSizeThumbnailMaker()
        Creates a FixedSizeThumbnailMaker.

        The size of the resulting thumbnail, and whether or not the aspect ratio of the original image should be maintained in the thumbnail must be set before this instance is able to produce thumbnails.

      • FixedSizeThumbnailMaker

        public FixedSizeThumbnailMaker​(int width,
                                       int height)
        Creates a FixedSizeThumbnailMaker which creates thumbnails with the specified size.

        Before this instance is able to produce thumbnails, whether or not the aspect ratio of the original image should be maintained in the thumbnail must be specified by calling the keepAspectRatio(boolean) method.

        Parameters:
        width - The width of the thumbnail to produce.
        height - The height of the thumbnails to produce.
      • FixedSizeThumbnailMaker

        public FixedSizeThumbnailMaker​(int width,
                                       int height,
                                       boolean aspectRatio)
        Creates a FixedSizeThumbnailMaker which creates thumbnails with the specified size. Whether or not the aspect ratio of the original image should be preserved by the thumbnail is also specified at instantiation.
        Parameters:
        width - The width of the thumbnail to produce.
        height - The height of the thumbnails to produce.
        aspectRatio - Whether or not to maintain the aspect ratio in the thumbnail the same as the original image.

        If true is specified, then the thumbnail image will have the same aspect ratio as the original image.

      • FixedSizeThumbnailMaker

        public FixedSizeThumbnailMaker​(int width,
                                       int height,
                                       boolean aspectRatio,
                                       boolean fit)
        Creates a FixedSizeThumbnailMaker which creates thumbnails with the specified size. Whether or not the aspect ratio of the original image should be preserved by the thumbnail, and whether to fit the thumbnail within the given dimensions is also specified at instantiation.
        Parameters:
        width - The width of the thumbnail to produce.
        height - The height of the thumbnails to produce.
        aspectRatio - Whether or not to maintain the aspect ratio in the thumbnail the same as the original image.

        If true is specified, then the thumbnail image will have the same aspect ratio as the original image.

        fit - Whether or not to fit the thumbnail within the specified dimensions.

        If true is specified, then the thumbnail will be sized to fit within the specified width and height.

    • Method Detail

      • size

        public FixedSizeThumbnailMaker size​(int width,
                                            int height)
        Sets the size of the thumbnail to produce.
        Parameters:
        width - The width of the thumbnail to produce.
        height - The height of the thumbnails to produce.
        Returns:
        A reference to this object.
        Throws:
        java.lang.IllegalStateException - If the size has already been previously set, or if the width or height is less than or equal to zero.
      • keepAspectRatio

        public FixedSizeThumbnailMaker keepAspectRatio​(boolean keep)
        Sets whether or not the thumbnail is to maintain the aspect ratio of the original image.
        Parameters:
        keep - Whether or not to maintain the aspect ratio in the thumbnail the same as the original image.

        If true is specified, then the thumbnail image will have the same aspect ratio as the original image.

        Returns:
        A reference to this object.
        Throws:
        java.lang.IllegalStateException - If whether to keep the aspect ratio has already been previously set.
      • fitWithinDimensions

        public FixedSizeThumbnailMaker fitWithinDimensions​(boolean fit)
        Sets whether or not the thumbnail should fit within the specified dimensions.

        When the dimensions of a thumbnail will exceed the specified dimensions, with the aspect ratio of the original being preserved, then if this method was called with false, then the resulting thumbnail will have the larger dimension align with the specified dimension, and the other will exceed the given dimension.

        When keepAspectRatio(boolean) is false, then calling this method with true or false makes no difference, as the thumbnail dimensions will be exactly the given dimensions.

        Parameters:
        fit - Whether or not to maintain the aspect ratio in the thumbnail the same as the original image.

        If true is specified, then the thumbnail image will have the same aspect ratio as the original image.

        Returns:
        A reference to this object.
        Throws:
        java.lang.IllegalStateException - If whether to keep the aspect ratio has already been previously set.
        Since:
        0.4.0
      • make

        public java.awt.image.BufferedImage make​(java.awt.image.BufferedImage img)
        Description copied from class: ThumbnailMaker
        Makes a thumbnail.
        Specified by:
        make in class ThumbnailMaker
        Parameters:
        img - The source image.
        Returns:
        The thumbnail created from the source image, using the parameters set by the ThumbnailMaker.