Class ImageUtils
java.lang.Object
org.hippoecm.frontend.plugins.gallery.imageutil.ImageUtils
Generic image manipulation utility methods.
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic enum
The available strategies for scaling images. -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic InputStream
convertToRGB
(InputStream is, ColorModel colorModel) Converts image raster data to a JPEG with RGB color space.static BufferedImage
cropImage
(BufferedImage image, Rectangle cropArea) Returns a cropped instance of theBufferedImage
provided by theImageReader
.static double
determineResizeRatio
(double sourceWidth, double sourceHeight, int targetWidth, int targetHeight) Determine the ratio by which the source dimensions should be multiplied to fit into the target dimensions.static double
determineScalingFactor
(double width, double height, double maxWidth, double maxHeight) Determine the scaling factor of an image, so that it fits within the max boundaries of the preview container (e.g.static ImageReader
getImageReader
(String aMimeType) Returns an image reader for a MIME type.static ImageWriter
getImageWriter
(String aMimeType) Returns an image writer for a MIME type.static Dimension
normalizeDimension
(Dimension original, Dimension variant) If height or width in the variant dimension is equal to 0 it is a special case.static BufferedImage
scaleImage
(BufferedImage img, int xOffset, int yOffset, int sourceWidth, int sourceHeight, int targetWidth, int targetHeight, Object hint, boolean highQuality) Deprecated.static BufferedImage
scaleImage
(BufferedImage img, int targetWidth, int targetHeight, ImageUtils.ScalingStrategy strategy) Returns a scaled instance of the providedBufferedImage
.static BufferedImage
scaleImage
(BufferedImage original, Rectangle rectangle, Dimension targetDimension, Object hint, boolean highQuality) Deprecated.UsecropImage(BufferedImage, Rectangle)
combined withscaleImage(BufferedImage, int, int, ScalingStrategy)
insteadstatic ByteArrayOutputStream
writeImage
(ImageWriter writer, BufferedImage image) Returns the data of aBufferedImage
as a binary output stream.static ByteArrayOutputStream
writeImage
(ImageWriter writer, BufferedImage image, float compressionQuality) Returns the data of aBufferedImage
as a binary output stream.
-
Constructor Details
-
ImageUtils
protected ImageUtils()Prevent instantiation
-
-
Method Details
-
getImageReader
Returns an image reader for a MIME type.- Parameters:
aMimeType
- MIME type- Returns:
- an image reader for the given MIME type, or
null
if no image reader could be created for the given MIME type.
-
getImageWriter
Returns an image writer for a MIME type.- Parameters:
aMimeType
- MIME type- Returns:
- an image writer for the given MIME type, or
null
if no image writer could be created for the given MIME type.
-
writeImage
public static ByteArrayOutputStream writeImage(ImageWriter writer, BufferedImage image, float compressionQuality) throws IOException Returns the data of aBufferedImage
as a binary output stream. If the image isnull
, a stream of zero bytes is returned.- Parameters:
writer
- the writer to use for writing the image data.image
- the image to write.compressionQuality
- a float between 0 and 1 that indicates the desired compression quality. Values lower than 0 will be interpreted as 0, values higher than 1 will be interpreted as 1.- Returns:
- an output stream with the data of the given image.
- Throws:
IOException
- when creating the binary output stream failed.
-
writeImage
public static ByteArrayOutputStream writeImage(ImageWriter writer, BufferedImage image) throws IOException Returns the data of aBufferedImage
as a binary output stream. If the image isnull
, a stream of zero bytes is returned. The data is written with a compression quality of 1.- Parameters:
writer
- the writer to use for writing the image data.image
- the image to write.- Returns:
- an output stream with the data of the given image.
- Throws:
IOException
- when creating the binary output stream failed.
-
scaleImage
public static BufferedImage scaleImage(BufferedImage img, int targetWidth, int targetHeight, ImageUtils.ScalingStrategy strategy) Returns a scaled instance of the providedBufferedImage
.- Parameters:
img
- the original image to be scaledtargetWidth
- the desired width of the scaled instance, in pixelstargetHeight
- the desired height of the scaled instance, in pixelsstrategy
- the strategy to use for scaling the image- Returns:
- a scaled version of the original
BufferedImage
, ornull
if either the target width or target height is 0 or less.
-
scaleImage
@Deprecated public static BufferedImage scaleImage(BufferedImage original, Rectangle rectangle, Dimension targetDimension, Object hint, boolean highQuality) Deprecated.UsecropImage(BufferedImage, Rectangle)
combined withscaleImage(BufferedImage, int, int, ScalingStrategy)
insteadReturns a scaled instance of the providedBufferedImage
.- Parameters:
original
- the original image to be scaledrectangle
- the rectangle to use for the scaled instancetargetDimension
- the desired dimensions of the scaled instance, in pixelshint
- one of the rendering hints that corresponds toRenderingHints.KEY_INTERPOLATION
(e.g.RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR
,RenderingHints.VALUE_INTERPOLATION_BILINEAR
,RenderingHints.VALUE_INTERPOLATION_BICUBIC
)highQuality
- if true, this method will use a multi-step scaling technique that provides higher quality than the usual one-step technique (only useful in downscaling cases, wheretargetWidth
ortargetHeight
is smaller than the original dimensions, and generally only when theBILINEAR
hint is specified)- Returns:
- a scaled version of the original
BufferedImage
, ornull
if either the target width or target height is 0 or less.
-
determineScalingFactor
public static double determineScalingFactor(double width, double height, double maxWidth, double maxHeight) Determine the scaling factor of an image, so that it fits within the max boundaries of the preview container (e.g.#MAX_PREVIEW_WIDTH
by#MAX_PREVIEW_HEIGHT
).- Parameters:
width
- width of imageheight
- height of imagemaxWidth
- max width of imagemaxHeight
- max height of image- Returns:
- the scaling factor of the image
-
normalizeDimension
If height or width in the variant dimension is equal to 0 it is a special case. The value 0 represents a value that according to the dimension of the original image.With this function a new dimension is created according to the original dimension.
- Parameters:
original
- dimension of the original imagevariant
- dimension of the variant image- Returns:
- scaled dimension based on width or height value
-
determineResizeRatio
public static double determineResizeRatio(double sourceWidth, double sourceHeight, int targetWidth, int targetHeight) Determine the ratio by which the source dimensions should be multiplied to fit into the target dimensions.- Parameters:
sourceWidth
- The width of the source imagesourceHeight
- The height of the source imagetargetWidth
- The width of the target imagetargetHeight
- The height of the target image- Returns:
- The resize ratio
-
cropImage
Returns a cropped instance of theBufferedImage
provided by theImageReader
.- Parameters:
image
- The original imagecropArea
- The rectangle used to crop the image- Returns:
- a cropped version of the original image.
-
scaleImage
@Deprecated public static BufferedImage scaleImage(BufferedImage img, int xOffset, int yOffset, int sourceWidth, int sourceHeight, int targetWidth, int targetHeight, Object hint, boolean highQuality) Deprecated.Returns a scaled instance of the providedBufferedImage
.- Parameters:
img
- the original image to be scaledxOffset
- the X-coordinate of the top-left corner in the source image, in pixels, to use for the scaled instance.yOffset
- the Y-coordinate of the top-left corner in the source image, in pixels, to use for the scaled instance.sourceWidth
- the width of the source image, in pixels relative to xOffset, to use for the scaled instance.sourceHeight
- the height of the source image, in pixels relative to yOffset, to use for the scaled instance.targetWidth
- the desired width of the scaled instance, in pixelstargetHeight
- the desired height of the scaled instance, in pixelshint
- one of the rendering hints that corresponds toRenderingHints.KEY_INTERPOLATION
(e.g.RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR
,RenderingHints.VALUE_INTERPOLATION_BILINEAR
,RenderingHints.VALUE_INTERPOLATION_BICUBIC
)highQuality
- if true, this method will use a multi-step scaling technique that provides higher quality than the usual one-step technique (only useful in downscaling cases, wheretargetWidth
ortargetHeight
is smaller than the original dimensions, and generally only when theBILINEAR
hint is specified)- Returns:
- a scaled version of the original
BufferedImage
, ornull
if either the target width or target height is 0 or less.
-
convertToRGB
public static InputStream convertToRGB(InputStream is, ColorModel colorModel) throws IOException, UnsupportedImageException Converts image raster data to a JPEG with RGB color space. Only images with color space CMYK and YCCK are converted, other images are left untouched.Rationale: Java's ImageIO can't process 4-component images and Java2D can't apply AffineTransformOp either, so we have to convert raster data to a JPG with RGB color space.
The technique used in this method is due to Mark Stephens, and free for any use. See http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4799903 or http://www.mail-archive.com/java2d-interest@capra.eng.sun.com/msg03247.html
- Parameters:
is
- the image datacolorModel
- the color model of the image- Returns:
- the RGB version of the supplied image
- Throws:
IOException
UnsupportedImageException
-
scaleImage(BufferedImage, Rectangle, Dimension, Object, boolean)
instead.