package net.coobird.thumbnailator.builders; import java.awt.Dimension; import java.awt.image.BufferedImage; import java.util.Collections; import java.util.List; import net.coobird.thumbnailator.ThumbnailParameter; import net.coobird.thumbnailator.filters.ImageFilter; import net.coobird.thumbnailator.resizers.DefaultResizerFactory; import net.coobird.thumbnailator.resizers.FixedResizerFactory; import net.coobird.thumbnailator.geometry.Region; import net.coobird.thumbnailator.resizers.Resizer; import net.coobird.thumbnailator.resizers.ResizerFactory; /** * A builder for generating {@link ThumbnailParameter}. *
* The default values assigned to the {@link ThumbnailParameter} created by * the {@link ThumbnailParameterBuilder} are as follows: *
*
* An acceptable value is in the range of {@code 0.0f} to {@code 1.0f}, * where {@code 0.0f} is for the lowest quality setting and {@code 1.0f} for * the highest quality setting. *
* If the default compression quality is to be used, then the value * {@link ThumbnailParameter#DEFAULT_QUALITY} should be used. * * @param quality The compression quality setting of the thumbnail. * @return A reference to this object. */ public ThumbnailParameterBuilder quality(float quality) { this.thumbnailQuality = quality; return this; } /** * Sets the output format of the thumbnail. * * @param format The output format of the thumbnail. * @return A reference to this object. */ public ThumbnailParameterBuilder format(String format) { this.thumbnailFormat = format; return this; } /** * Sets the output format type of the thumbnail. * * @param formatType The output format type of the thumbnail. * @return A reference to this object. */ public ThumbnailParameterBuilder formatType(String formatType) { this.thumbnailFormatType = formatType; return this; } /** * Sets the {@link ImageFilter}s to apply to the thumbnail. *
* These filters will be applied after the original image is resized.
*
* @param filters The output format type of the thumbnail.
* @return A reference to this object.
*/
public ThumbnailParameterBuilder filters(List
* Calling this method after {@link #resizerFactory(ResizerFactory)} will
* cause the {@link ResizerFactory} used by the resulting
* {@link ThumbnailParameter} to only return the specified {@link Resizer}.
*
* @param resizer The {@link Resizer} to use when creating the
* thumbnail.
* @return A reference to this object.
*/
public ThumbnailParameterBuilder resizer(Resizer resizer)
{
if (resizer == null)
{
throw new NullPointerException("Resizer is null.");
}
this.resizerFactory = new FixedResizerFactory(resizer);
return this;
}
/**
* Sets the {@link ResizerFactory} to use to obtain a {@link Resizer} when
* performing the resizing operation to create the thumbnail.
*
* Calling this method after {@link #resizer(Resizer)} could result in
* {@link Resizer}s not specified in the {@code resizer} method to be used
* when creating thumbnails.
*
*
* @param resizerFactory The {@link ResizerFactory} to use when obtaining
* a {@link Resizer} to create the thumbnail.
* @return A reference to this object.
* @since 0.4.0
*/
public ThumbnailParameterBuilder resizerFactory(ResizerFactory resizerFactory)
{
if (resizerFactory == null)
{
throw new NullPointerException("Resizer is null.");
}
this.resizerFactory = resizerFactory;
return this;
}
/**
* Sets whether or not the thumbnail should fit within the specified
* dimensions.
*
* @param fit {@code true} if the thumbnail should be sized to fit
* within the specified dimensions, if the thumbnail
* is going to exceed those dimensions.
* @return A reference to this object.
* @since 0.4.0
*/
public ThumbnailParameterBuilder fitWithinDimensions(boolean fit)
{
this.fitWithinDimensions = fit;
return this;
}
/**
* Sets whether or not the Exif metadata should be used to determine the
* orientation of the thumbnail.
*
* @param use {@code true} if the Exif metadata should be used
* to determine the orientation of the thumbnail,
* {@code false} otherwise.
* @return A reference to this object.
* @since 0.4.3
*/
public ThumbnailParameterBuilder useExifOrientation(boolean use)
{
this.useExifOrientation = use;
return this;
}
/**
* Returns a {@link ThumbnailParameter} from the parameters which are
* currently set.
*
* This method will throw a {@link IllegalArgumentException} required
* parameters for the {@link ThumbnailParameter} have not been set.
*
* @return A {@link ThumbnailParameter} with parameters set through
* the use of this builder.
* @throws IllegalStateException If neither the size nor the scaling
* factor has been set.
*/
public ThumbnailParameter build()
{
if (!Double.isNaN(widthScalingFactor))
{
// If scaling factor has been set.
return new ThumbnailParameter(
widthScalingFactor,
heightScalingFactor,
sourceRegion,
keepAspectRatio,
thumbnailFormat,
thumbnailFormatType,
thumbnailQuality,
imageType,
filters,
resizerFactory,
fitWithinDimensions,
useExifOrientation
);
}
else if (width != UNINITIALIZED && height != UNINITIALIZED)
{
return new ThumbnailParameter(
new Dimension(width, height),
sourceRegion,
keepAspectRatio,
thumbnailFormat,
thumbnailFormatType,
thumbnailQuality,
imageType,
filters,
resizerFactory,
fitWithinDimensions,
useExifOrientation
);
}
else
{
throw new IllegalStateException(
"The size nor the scaling factor has been set."
);
}
}
}