package net.coobird.thumbnailator; import java.awt.Dimension; import java.awt.Rectangle; import java.awt.RenderingHints; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.URL; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import javax.imageio.ImageIO; import net.coobird.thumbnailator.filters.Canvas; import net.coobird.thumbnailator.filters.ImageFilter; import net.coobird.thumbnailator.filters.Pipeline; import net.coobird.thumbnailator.filters.Rotation; import net.coobird.thumbnailator.filters.Watermark; import net.coobird.thumbnailator.geometry.AbsoluteSize; import net.coobird.thumbnailator.geometry.Coordinate; import net.coobird.thumbnailator.geometry.Position; import net.coobird.thumbnailator.geometry.Positions; import net.coobird.thumbnailator.geometry.Region; import net.coobird.thumbnailator.geometry.Size; import net.coobird.thumbnailator.name.Rename; import net.coobird.thumbnailator.resizers.BicubicResizer; import net.coobird.thumbnailator.resizers.BilinearResizer; import net.coobird.thumbnailator.resizers.DefaultResizerFactory; import net.coobird.thumbnailator.resizers.FixedResizerFactory; import net.coobird.thumbnailator.resizers.ProgressiveBilinearResizer; import net.coobird.thumbnailator.resizers.Resizer; import net.coobird.thumbnailator.resizers.ResizerFactory; import net.coobird.thumbnailator.resizers.configurations.AlphaInterpolation; import net.coobird.thumbnailator.resizers.configurations.Antialiasing; import net.coobird.thumbnailator.resizers.configurations.Dithering; import net.coobird.thumbnailator.resizers.configurations.Rendering; import net.coobird.thumbnailator.resizers.configurations.ScalingMode; import net.coobird.thumbnailator.tasks.SourceSinkThumbnailTask; import net.coobird.thumbnailator.tasks.io.BufferedImageSink; import net.coobird.thumbnailator.tasks.io.BufferedImageSource; import net.coobird.thumbnailator.tasks.io.FileImageSink; import net.coobird.thumbnailator.tasks.io.FileImageSource; import net.coobird.thumbnailator.tasks.io.ImageSource; import net.coobird.thumbnailator.tasks.io.InputStreamImageSource; import net.coobird.thumbnailator.tasks.io.OutputStreamImageSink; import net.coobird.thumbnailator.tasks.io.URLImageSource; import net.coobird.thumbnailator.util.ThumbnailatorUtils; /** * Provides a fluent interface to create thumbnails. *
* This is the main entry point for creating thumbnails with Thumbnailator. *
* By using the Thumbnailator's fluent interface, it is possible to write * thumbnail generation code which resembles written English. *
*
Thumbnails.of(directory.listFiles()) .size(200, 200) .outputFormat("jpeg") .asFiles(Rename.PREFIX_DOT_THUMBNAIL); // English: "Make thumbnails of files in the directory, with a size of 200x200, with output format of JPEG, and save them as files while renaming the files to be prefixed with a 'thumbnail.'." **
*
* As a rule of thumb, always method chain from the {@code Thumbnails.of} * all the way until the output method (e.g. {@code toFile}, {@code asBufferedImage}, * etc.) is called without breaking them down into single statements. * See the "Usage" section above for the intended use of the Thumbnailator's * fluent interface. *
// Unintended use - not recommended! Builder<File> instance = Thumbnails.of("path/to/image"); instance.size(200, 200); instance.asFiles("path/to/thumbnail"); **
* If any of the dimensions are less than or equal to 0, an * {@code IllegalArgumentException} is thrown with an message specifying the * reason for the exception. *
* This method is used to perform a check on the output dimensions of a
* thumbnail for the {@link Thumbnails#createThumbnail} methods.
*
* @param width The width to validate.
* @param height The height to validate.
*/
private static void validateDimensions(int width, int height)
{
if (width <= 0 && height <= 0)
{
throw new IllegalArgumentException(
"Destination image dimensions must not be less than " +
"0 pixels."
);
}
else if (width <= 0 || height <= 0)
{
String dimension = width == 0 ? "width" : "height";
throw new IllegalArgumentException(
"Destination image " + dimension + " must not be " +
"less than or equal to 0 pixels."
);
}
}
private static void checkForNull(Object o, String message)
{
if (o == null)
{
throw new NullPointerException(message);
}
}
private static void checkForEmpty(Object[] o, String message)
{
if (o.length == 0)
{
throw new IllegalArgumentException(message);
}
}
private static void checkForEmpty(Iterable> o, String message)
{
if (!o.iterator().hasNext())
{
throw new IllegalArgumentException(message);
}
}
/**
* Indicate to make thumbnails for images with the specified filenames.
*
* @param files File names of image files for which thumbnails
* are to be produced for.
* @return Reference to a builder object which is used to
* specify the parameters for creating the thumbnail.
* @throws NullPointerException If the argument is {@code null}.
* @throws IllegalArgumentException If the argument is an empty array.
*/
public static Builder
* Thumbnailator is intended to be used by calling one of the
* {@code Thumbnails.of(...)} methods, then chaining methods such as
* {@link #size(int, int)} and {@link #outputQuality(double)} to set up
* the thumbnail generation parameters. (See "Intended Use" below.)
* The end result should be code that resembles English.
*
* In most cases, holding an instance of this class in a local variable,
* such as seen in the "Unintended Use" example below, is more verbose
* and less future-proof, as changes to this class (which is just an
* inner class of the {@link Thumbnails} class) can lead to broken code
* when attempting to use future releases of Thumbnailator.
*
*
* An instance of this class provides the fluent interface in the form of
* method chaining. Through the fluent interface, the parameters used for
* the thumbnail creation, such as {@link #size(int, int)} and
* {@link #outputQuality(double)} can be set up. Finally, to execute the
* thumbnail creation, one of the output methods whose names start with
* {@code to} (e.g. {@link #toFiles(Rename)}) or {@code as}
* (e.g. {@link #asBufferedImages()}) is called.
*
* An instance of this class is obtained by calling one of:
*
* For example, to create thumbnails which should fit within a
* bounding rectangle of 640 x 480, the following code can be used:
*
* In the above code, the thumbnail will preserve the aspect ratio
* of the original image. If the thumbnail should be forced to the
* specified size, the {@link #forceSize(int, int)} method can
* be used instead of this method.
*
* Once this method is called, calling the {@link #scale(double)} method
* will result in an {@link IllegalStateException}.
*
* Calling this method multiple times will result in an
* {@link IllegalStateException} to be thrown.
*
* @param width The width of the thumbnail.
* @param height The height of the thumbnail.
* @return Reference to this object.
*/
public Builder
* The thumbnail will have the dimensions constrained by the specified
* width, and the aspect ratio of the original image will be preserved
* by the thumbnail.
*
* Once this method is called, calling the {@link #size(int, int)} or
* the {@link #scale(double)} method will result in an
* {@link IllegalStateException}.
*
* Calling this method multiple times will result in an
* {@link IllegalStateException} to be thrown.
*
* @param width The width of the thumbnail.
* @return Reference to this object.
* @since 0.3.5
*/
public Builder
* The thumbnail will have the dimensions constrained by the specified
* height, and the aspect ratio of the original image will be preserved
* by the thumbnail.
*
* Once this method is called, calling the {@link #size(int, int)} or
* the {@link #scale(double)} method will result in an
* {@link IllegalStateException}.
*
* Calling this method multiple times will result in an
* {@link IllegalStateException} to be thrown.
*
* @param height The height of the thumbnail.
* @return Reference to this object.
* @since 0.3.5
*/
public Builder
* The thumbnails will be forced to the specified size, therefore,
* the aspect ratio of the original image will not be preserved in
* the thumbnails. Calling this method will be equivalent to calling
* the {@link #size(int, int)} method in conjunction with the
* {@link #keepAspectRatio(boolean)} method with the value {@code false}.
*
* Once this method is called, calling the {@link #scale(double)} method
* will result in an {@link IllegalStateException}.
*
* Calling this method multiple times will result in an
* {@link IllegalStateException} to be thrown.
*
* @param width The width of the thumbnail.
* @param height The height of the thumbnail.
* @return Reference to this object.
* @since 0.3.2
*/
public Builder
* For example, to create thumbnails which are 50% the size of the
* original, the following code can be used:
*
* Once this method is called, calling the {@link #size(int, int)}
* method, or the {@link #scale(double, double)} method, or the
* {@link #keepAspectRatio(boolean)} method will result in an
* {@link IllegalStateException}.
*
* Calling this method multiple times will result in an
* {@link IllegalStateException} to be thrown.
*
* @param scale The scaling factor to use when creating a
* thumbnail.
*
* The value must be a {@code double} which is
* greater than {@code 0.0}, and not
* {@link Double#POSITIVE_INFINITY}.
* @return Reference to this object.
*/
public Builder
* If the scaling factor for the width and height are not equal, then
* the thumbnail will not preserve the aspect ratio of the original
* image.
*
* For example, to create thumbnails which are 50% the width of the
* original, while 75% the height of the original, the following code
* can be used:
*
* Once this method is called, calling the {@link #size(int, int)}
* method, or the {@link #scale(double)} method, or the
* {@link #keepAspectRatio(boolean)} method will result in an
* {@link IllegalStateException}.
*
* Calling this method multiple times will result in an
* {@link IllegalStateException} to be thrown.
*
* @param scaleWidth The scaling factor to use for the width when
* creating a thumbnail.
*
* The value must be a {@code double} which is
* greater than {@code 0.0}, and not
* {@link Double#POSITIVE_INFINITY}.
* @param scaleHeight The scaling factor to use for the height when
* creating a thumbnail.
*
* The value must be a {@code double} which is
* greater than {@code 0.0}, and not
* {@link Double#POSITIVE_INFINITY}.
* @return Reference to this object.
* @since 0.3.10
*/
public Builder
* Calling this method multiple times will result in an
* {@link IllegalStateException} to be thrown.
*
* @param sourceRegion Source region to use when creating a thumbnail.
*
* @return Reference to this object.
* @throws NullPointerException If the source region object is
* {@code null}.
* @since 0.3.4
*/
public Builder
* Calling this method multiple times will result in an
* {@link IllegalStateException} to be thrown.
*
* @param position Position of the source region.
* @param size Size of the source region.
* @return Reference to this object.
* @throws NullPointerException If the position and/or size is
* {@code null}.
* @since 0.3.4
*/
public Builder
* Calling this method multiple times will result in an
* {@link IllegalStateException} to be thrown.
*
* @param x The horizontal-compoennt of the top left-hand
* corner of the source region.
* @param y The vertical-compoennt of the top left-hand
* corner of the source region.
* @param width Width of the source region.
* @param height Height of the source region.
* @return Reference to this object.
* @throws IllegalArgumentException If the width and/or height is
* less than or equal to {@code 0}.
* @since 0.3.4
*/
public Builder
* Calling this method multiple times will result in an
* {@link IllegalStateException} to be thrown.
*
* @param position Position of the source region.
* @param width Width of the source region.
* @param height Height of the source region.
* @return Reference to this object.
* @throws NullPointerException If the position and/or size is
* {@code null}.
* @throws IllegalArgumentException If the width and/or height is
* less than or equal to {@code 0}.
* @since 0.3.4
*/
public Builder
* Calling this method multiple times will result in an
* {@link IllegalStateException} to be thrown.
*
* @param region A rectangular region which specifies the source
* region to use when creating the thumbnail.
* @throws NullPointerException If the region is {@code null}.
* @since 0.3.4
*/
public Builder
* Calling this method will guarantee that the size of the thumbnail
* will be exactly the dimensions specified in the
* {@link #size(int, int)} method.
*
* Internally, the resizing is performed in two steps.
* First, the thumbnail will be sized so that one of the dimensions will
* be sized exactly to the dimension specified in the {@code size}
* method, while allowing the other dimension to overhang the specified
* dimension. Then, the thumbnail will be cropped to the dimensions
* specified in the {@code size} method, positioned using the speficied
* {@link Position} object.
*
* Once this method is called, calling the {@link #scale(double)} method
* will result in an {@link IllegalStateException}.
*
* Calling this method multiple times will result in an
* {@link IllegalStateException} to be thrown.
*
* @param position The position to which the thumbnail should be
* cropped to. For example, if
* {@link Positions#CENTER} is specified, the
* resulting thumbnail will be made by cropping to
* the center of the image.
* @throws NullPointerException If the position is {@code null}.
* @since 0.4.0
*/
public Builder
* This method will change the output behavior of the following methods:
*
* Calling this method multiple times will result in an
* {@link IllegalStateException} to be thrown.
*
* @param allowOverwrite If {@code true} then existing files will be
* overwritten if specified as a destination.
* If {@code false}, then the existing files
* will not be altered. For specific behavior,
* please refer to the specific output methods
* listed above.
*
* @since 0.3.7
*/
public Builder
* Calling this method to set this parameter is optional.
*
* Calling this method multiple times will result in an
* {@link IllegalStateException} to be thrown.
*
* @param type The image type of the thumbnail.
* @return Reference to this object.
*/
public Builder
* Calling this method to set this parameter is optional.
*
* Calling this method multiple times will result in an
* {@link IllegalStateException} to be thrown.
*
* @param config The scaling mode to use.
* @return Reference to this object.
*/
public Builder
* Calling this method to set this parameter is optional.
*
* Calling this method multiple times will result in an
* {@link IllegalStateException} to be thrown.
*
* This method cannot be called in conjunction with the
* {@link #resizerFactory(ResizerFactory)} method.
*
* @param resizer The scaling operation to use.
* @return Reference to this object.
*/
public Builder
* Calling this method to set this parameter is optional.
*
* Calling this method multiple times will result in an
* {@link IllegalStateException} to be thrown.
*
* This method cannot be called in conjunction with the
* {@link #resizer(Resizer)} method.
*
* @param resizerFactory The scaling operation to use.
* @return Reference to this object.
* @since 0.4.0
*/
public Builder
* Calling this method to set this parameter is optional.
*
* Calling this method multiple times will result in an
* {@link IllegalStateException} to be thrown.
*
* This method cannot be called in conjunction with the
* {@link #resizerFactory(ResizerFactory)} method.
*
* @param config The alpha interpolation mode.
* @return Reference to this object.
*/
public Builder
* Calling this method to set this parameter is optional.
*
* Calling this method multiple times will result in an
* {@link IllegalStateException} to be thrown.
*
* This method cannot be called in conjunction with the
* {@link #resizerFactory(ResizerFactory)} method.
*
* @param config The dithering mode.
* @return Reference to this object.
*/
public Builder
* Calling this method to set this parameter is optional.
*
* Calling this method multiple times will result in an
* {@link IllegalStateException}.
*
* This method cannot be called in conjunction with the
* {@link #resizerFactory(ResizerFactory)} method.
*
* @param config The antialiasing mode.
* @return Reference to this object.
*/
public Builder
* Calling this method to set this parameter is optional.
*
* Calling this method multiple times will result in an
* {@link IllegalStateException} to be thrown.
*
* This method cannot be called in conjunction with the
* {@link #resizerFactory(ResizerFactory)} method.
*
* @param config The rendering mode.
* @return Reference to this object.
*/
public Builder
* Calling this method without first calling the {@link #size(int, int)}
* method will result in an {@link IllegalStateException} to be thrown.
*
* If this method is not called when, by default the aspect ratio of
* the original image is preserved for the thumbnail.
*
* Calling this method after calling the {@link #scale(double)} method
* or the {@link #scale(double, double)} method will result in a
* {@link IllegalStateException}.
*
* @param keep {@code true} if the thumbnail is to maintain
* the aspect ratio of the original image,
* {@code false} otherwise.
* @return Reference to this object.
*
* @throws IllegalStateException If
*
* The value is a {@code float} between {@code 0.0f} and {@code 1.0f}
* where {@code 0.0f} indicates the minimum quality and {@code 1.0f}
* indicates the maximum quality settings should be used for by the
* compression codec.
*
* Calling this method to set this parameter is optional.
*
* Calling this method in conjunction with {@link #asBufferedImage()}
* or {@link #asBufferedImages()} will not result in any changes to the
* final result.
*
* Calling this method multiple times, or the
* {@link #outputQuality(double)} in conjunction with this method will
* result in an {@link IllegalStateException} to be thrown.
*
* @param quality The compression quality to use when writing
* the thumbnail.
* @return Reference to this object.
* @throws IllegalArgumentException If the argument is less than
* {@code 0.0f} or is greater than
* {@code 1.0f}.
*/
public Builder
* The value is a {@code double} between {@code 0.0d} and {@code 1.0d}
* where {@code 0.0d} indicates the minimum quality and {@code 1.0d}
* indicates the maximum quality settings should be used for by the
* compression codec.
*
* This method is a convenience method for {@link #outputQuality(float)}
* where the {@code double} argument type is accepted instead of a
* {@code float}.
*
* Calling this method to set this parameter is optional.
*
* Calling this method in conjunction with {@link #asBufferedImage()}
* or {@link #asBufferedImages()} will not result in any changes to the
* final result.
*
* Calling this method multiple times, or the
* {@link #outputQuality(float)} in conjunction with this method will
* result in an {@link IllegalStateException} to be thrown.
*
* @param quality The compression quality to use when writing
* the thumbnail.
* @return Reference to this object.
* @throws IllegalArgumentException If the argument is less than
* {@code 0.0d} or is greater than
* {@code 1.0d}.
*/
public Builder
* For example, to set the output format to JPEG, the following code
* can be used:
*
* Currently, whether or not the compression format string is valid
* dependents on whether the Java Image I/O API recognizes the string
* as a format that it supports for output. (Valid format names can
* be obtained by calling the {@link ImageIO#getWriterFormatNames()}
* method.)
*
* Calling this method to set this parameter is optional.
*
* Calling this method in conjunction with {@link #asBufferedImage()}
* or {@link #asBufferedImages()} will not result in any changes to the
* final result.
*
* Calling this method multiple times will result in an
* {@link IllegalStateException} to be thrown.
*
* @param format The compression format to use when writing
* the thumbnail.
* @return Reference to this object.
* @throws IllegalArgumentException If an unsupported format is
* specified.
*/
public Builder
* Calling this method multiple times will result in an
* {@link IllegalStateException} to be thrown.
*
* @return Reference to this object.
* @since 0.4.0
*/
public Builder
* Calling this method multiple times will result in an
* {@link IllegalStateException} to be thrown.
*
* @param useExifOrientation {@code true} if the Exif metadata
* should be used to determine the
* orientation of the thumbnail,
* {@code false} otherwise.
* @return Reference to this object.
* @since 0.4.3
*/
public Builder
* For example, calling this method will cause the output format to be
* determined from the file extension if thumbnails are written to
* files.
*
* Calling this method multiple times will result in an
* {@link IllegalStateException} to be thrown.
*
* @return Reference to this object.
* @since 0.4.0
*/
public Builder
* If the default type for the compression codec should be used, a
* value of {@link ThumbnailParameter#DEFAULT_FORMAT_TYPE} should be
* used.
*
* Calling this method to set this parameter is optional.
*
* Calling this method multiple times will result in an
* {@link IllegalStateException} to be thrown.
*
* Furthermore, if this method is called, then calling the
* {@link #outputFormat} method is disabled, in order to prevent
* cases where the output format type does not exist in the format
* specified for the {@code outputFormat} method.
*
* @param formatType The compression format type
* @return Reference to this object.
* @throws IllegalArgumentException If an unsupported format type is
* specified for the current output
* format type. Or, if the output
* format has not been specified before
* this method was called.
*/
public Builder
* This method can be called multiple times to apply multiple
* watermarks.
*
* If multiple watermarks are to be applied, the watermarks will be
* applied in the order that this method is called.
*
* Calling this method to set this parameter is optional.
*
* @param w The watermark to apply to the thumbnail.
* @return Reference to this object.
*/
public Builder
* This method is a convenience method for the
* {@link #watermark(Position, BufferedImage, float)} method, where
* the opacity is 50%, and the position is set to center of the
* thumbnail:
*
*
* If multiple watermarks are to be applied, the watermarks will be
* applied in the order that this method is called.
*
* Calling this method to set this parameter is optional.
*
* @param image The image of the watermark.
* @return Reference to this object.
*/
public Builder
* This method is a convenience method for the
* {@link #watermark(Position, BufferedImage, float)} method, where
* the opacity is 50%:
*
*
* If multiple watermarks are to be applied, the watermarks will be
* applied in the order that this method is called.
*
* Calling this method to set this parameter is optional.
*
* @param image The image of the watermark.
* @param opacity The opacity of the watermark.
*
* The value should be between {@code 0.0f} and
* {@code 1.0f}, where {@code 0.0f} is completely
* transparent, and {@code 1.0f} is completely
* opaque.
* @return Reference to this object.
*/
public Builder
* This method can be called multiple times to apply multiple
* watermarks.
*
* If multiple watermarks are to be applied, the watermarks will be
* applied in the order that this method is called.
*
* Calling this method to set this parameter is optional.
*
* @param position The position of the watermark.
* @param image The image of the watermark.
* @param opacity The opacity of the watermark.
*
* The value should be between {@code 0.0f} and
* {@code 1.0f}, where {@code 0.0f} is completely
* transparent, and {@code 1.0f} is completely
* opaque.
* @return Reference to this object.
*/
public Builder
* The thumbnail will be rotated clockwise by the angle specified.
*
* This method can be called multiple times to apply multiple
* rotations.
*
* If multiple rotations are to be applied, the rotations will be
* applied in the order that this method is called.
*
* Calling this method to set this parameter is optional.
*
* @param angle Angle in degrees.
* @return Reference to this object.
*/
public Builder
* This method can be called multiple times to apply multiple
* filters.
*
* If multiple filters are to be applied, the filters will be
* applied in the order that this method is called.
*
* Calling this method to set this parameter is optional.
*
* @param filter An image filter to apply to the thumbnail.
* @return Reference to this object.
*/
public Builder
* This method can be called multiple times to apply multiple
* filters.
*
* If multiple filters are to be applied, the filters will be
* applied in the order that this method is called.
*
* Calling this method to set this parameter is optional.
*
* @param filters A list of filters to apply to the thumbnail.
* @return Reference to this object.
*/
public Builder
* For situations where multiple thumbnails are being generated, this
* method is preferred over the {@link #asBufferedImages()} method,
* as (1) the processing does not have to complete before the method
* returns and (2) the thumbnails can be retrieved one at a time,
* potentially reducing the number of thumbnails which need to be
* retained in the heap memory, potentially reducing the chance of
* {@link OutOfMemoryError}s from occurring.
*
* If an {@link IOException} occurs during the processing of the
* thumbnail, the {@link Iterable} will return a {@code null} for that
* element.
*
* @return An {@link Iterable} which will provide an
* {@link Iterator} which returns thumbnails as
* {@link BufferedImage}s.
*/
public Iterable
*
* If many thumbnails are being processed at once, then using the
* {@link #iterableBufferedImages()} method would be preferable.
*
* @return A list of thumbnails.
* @throws IOException If an problem occurred during
* the reading of the original
* images.
*/
public List
* To call this method, the thumbnail must have been created from a
* single source.
*
* @return A thumbnail as a {@link BufferedImage}.
* @throws IOException If an problem occurred during
* the reading of the original
* image.
* @throws IllegalArgumentException If multiple original images are
* specified.
*/
public BufferedImage asBufferedImage() throws IOException
{
checkReadiness();
Iterator
* When the destination file exists, and overwriting files has been
* disabled by calling the {@link #allowOverwrite(boolean)} method
* with {@code false}, then the thumbnail with the destination file
* already existing will not be written and the corresponding
* {@code File} object will not be included in the {@code List} returned
* by this method.
*
* The file names for the thumbnails are obtained from the given
* {@link Iterable}.
*
* @param iterable An {@link Iterable} which returns an
* {@link Iterator} which returns file names
* which should be assigned to each thumbnail.
* @return A list of {@link File}s of the thumbnails
* which were created.
* @throws IOException If a problem occurs while reading the
* original images or writing the thumbnails
* to files.
* @since 0.3.7
*/
public List
* When the destination file exists, and overwriting files has been
* disabled by calling the {@link #allowOverwrite(boolean)} method
* with {@code false}, then the thumbnail with the destination file
* already existing will not be written.
*
* The file names for the thumbnails are obtained from the given
* {@link Iterable}.
*
* @param iterable An {@link Iterable} which returns an
* {@link Iterator} which returns file names
* which should be assigned to each thumbnail.
* @throws IOException If a problem occurs while reading the
* original images or writing the thumbnails
* to files.
* @since 0.3.7
*/
public void toFiles(Iterable
* When the destination file exists, and overwriting files has been
* disabled by calling the {@link #allowOverwrite(boolean)} method
* with {@code false}, then the thumbnail with the destination file
* already existing will not be written and the corresponding
* {@code File} object will not be included in the {@code List} returned
* by this method.
*
* To call this method, the thumbnails must have been creates from
* files by calling the {@link Thumbnails#of(File...)} method.
*
* @param rename The rename function which is used to
* determine the filenames of the thumbnail
* files to write.
* @return A list of {@link File}s of the thumbnails
* which were created.
* @throws IOException If a problem occurs while reading the
* original images or writing the thumbnails
* to files.
* @throws IllegalStateException If the original images are not
* from files.
* @since 0.3.7
*/
public List
* When the destination file exists, and overwriting files has been
* disabled by calling the {@link #allowOverwrite(boolean)} method
* with {@code false}, then the thumbnail with the destination file
* already existing will not be written and the corresponding
* {@code File} object will not be included in the {@code List} returned
* by this method.
*
* Extra caution should be taken when using this method, as there are
* no protections in place to prevent file name collisions resulting
* from creating thumbnails from files in separate directories but
* having the same name. In such a case, the behavior will be depend
* on the behavior of the {@link #allowOverwrite(boolean)} as
* described in the previous paragraph.
*
* To call this method, the thumbnails must have been creates from
* files by calling the {@link Thumbnails#of(File...)} method.
*
* @param destinationDir The destination directory to which the
* thumbnails should be written to.
* @param rename The rename function which is used to
* determine the filenames of the thumbnail
* files to write.
* @return A list of {@link File}s of the thumbnails
* which were created.
* @throws IOException If a problem occurs while reading the
* original images or writing the thumbnails
* to files.
* @throws IllegalStateException If the original images are not
* from files.
* @throws IllegalArgumentException If the destination directory
* is not a directory.
* @since 0.4.7
*/
public List
* When the destination file exists, and overwriting files has been
* disabled by calling the {@link #allowOverwrite(boolean)} method
* with {@code false}, then the thumbnail with the destination file
* already existing will not be written.
*
* To call this method, the thumbnails must have been creates from
* files by calling the {@link Thumbnails#of(File...)} method.
*
* @param rename The rename function which is used to
* determine the filenames of the thumbnail
* files to write.
* @throws IOException If a problem occurs while reading the
* original images or writing the thumbnails
* to files.
* thumbnails to files.
* @throws IllegalStateException If the original images are not
* from files.
* @since 0.3.7
*/
public void toFiles(Rename rename) throws IOException
{
toFiles(null, rename);
}
/**
* Creates thumbnails and stores them to files in the directory
* specified by the given {@link File} object, and using the
* {@link Rename} function to determine the filenames.
*
* When the destination file exists, and overwriting files has been
* disabled by calling the {@link #allowOverwrite(boolean)} method
* with {@code false}, then the thumbnail with the destination file
* already existing will not be written.
*
* Extra caution should be taken when using this method, as there are
* no protections in place to prevent file name collisions resulting
* from creating thumbnails from files in separate directories but
* having the same name. In such a case, the behavior will be depend
* on the behavior of the {@link #allowOverwrite(boolean)} as
* described in the previous paragraph.
*
* To call this method, the thumbnails must have been creates from
* files by calling the {@link Thumbnails#of(File...)} method.
*
* @param destinationDir The destination directory to which the
* thumbnails should be written to.
* @param rename The rename function which is used to
* determine the filenames of the thumbnail
* files to write.
* @throws IOException If a problem occurs while reading the
* original images or writing the thumbnails
* to files.
* thumbnails to files.
* @throws IllegalStateException If the original images are not
* from files.
* @throws IllegalArgumentException If the destination directory
* is not a directory.
* @since 0.4.7
*/
public void toFiles(File destinationDir, Rename rename) throws IOException
{
asFiles(destinationDir, rename);
}
/**
* Create a thumbnail and writes it to a {@link File}.
*
* When the destination file exists, and overwriting files has been
* disabled by calling the {@link #allowOverwrite(boolean)} method
* with {@code false}, then an {@link IllegalArgumentException} will
* be thrown.
*
* To call this method, the thumbnail must have been created from a
* single source.
*
* @param outFile The file to which the thumbnail is to be
* written to.
* @throws IOException If a problem occurs while reading the
* original images or writing the thumbnails
* to files.
* @throws IllegalArgumentException If multiple original image files
* are specified, or if the
* destination file exists, and
* overwriting files is disabled.
*/
public void toFile(File outFile) throws IOException
{
checkReadiness();
Iterator
* When the destination file exists, and overwriting files has been
* disabled by calling the {@link #allowOverwrite(boolean)} method
* with {@code false}, then an {@link IllegalArgumentException} will
* be thrown.
*
* To call this method, the thumbnail must have been created from a
* single source.
*
* @param outFilepath The file to which the thumbnail is to be
* written to.
* @throws IOException If a problem occurs while reading the
* original images or writing the thumbnails
* to files.
* @throws IllegalArgumentException If multiple original image files
* are specified, or if the
* destination file exists, and
* overwriting files is disabled.
*/
public void toFile(String outFilepath) throws IOException
{
checkReadiness();
Iterator
* To call this method, the thumbnail must have been created from a
* single source.
*
* @param os The output stream to which the thumbnail
* is to be written to.
* @throws IOException If a problem occurs while reading the
* original images or writing the thumbnails.
* @throws IllegalArgumentException If multiple original image files
* are specified.
* @throws IllegalStateException If the output format has not
* been specified through the
* {@link #outputFormat(String)}
* method.
*/
public void toOutputStream(OutputStream os) throws IOException
{
checkReadiness();
Iterator
*
*
// Intended use - recommended!
Thumbnails.of("path/to/image")
.size(200, 200)
.asFile("path/to/thumbnail");
// English: "Make a thumbnail of 'path/to/image' with a size of 200x200,
and save it as a file to 'path/to/thumbnail'."
*
*
// Unintended use - not recommended!
Builder<File> instance = Thumbnails.of("path/to/image");
instance.size(200, 200);
instance.asFiles("path/to/thumbnail");
*
*
*
*
* @author coobird
*
*/
public static class Builder
*
Thumbnails.of(image)
.size(640, 480)
.toFile(thumbnail);
*
*
Thumbnails.of(image)
.scale(0.5)
.toFile(thumbnail);
*
*
Thumbnails.of(image)
.scale(0.5, 0.75)
.toFile(thumbnail);
*
*
* The behavior of methods which are not listed above will not be
* affected by calling this method.
*
*
*/
public Builder
* or, alternatively:
*
Thumbnails.of(image)
.size(640, 480)
.outputFormat("JPEG")
.toFile(thumbnail);
*
*
Thumbnails.of(image)
.size(640, 480)
.outputFormat("jpg")
.toFile(thumbnail);
*
watermark(Positions.CENTER, image, 0.5f);
*
* This method can be called multiple times to apply multiple
* watermarks.
*
watermark(Positions.CENTER, image, opacity);
*
* This method can be called multiple times to apply multiple
* watermarks.
* Note about performance
* If there are many thumbnails generated at once, it is possible that
* the Java virtual machine's heap space will run out and an
* {@link OutOfMemoryError} could result.
*