public class ImageUtils extends Object
Modifier and Type | Class and Description |
---|---|
static class |
ImageUtils.ScalingStrategy
The available strategies for scaling images.
|
Modifier | Constructor and Description |
---|---|
protected |
ImageUtils()
Prevent instantiation
|
Modifier and Type | Method and Description |
---|---|
static 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 the
BufferedImage provided by the ImageReader . |
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 targetWidth,
int targetHeight,
ImageUtils.ScalingStrategy strategy)
Returns a scaled instance of the provided
BufferedImage . |
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 original,
Rectangle rectangle,
Dimension targetDimension,
Object hint,
boolean highQuality)
Deprecated.
Use
cropImage(BufferedImage, Rectangle) combined with
scaleImage(BufferedImage, int, int, ScalingStrategy) instead |
static ByteArrayOutputStream |
writeImage(ImageWriter writer,
BufferedImage image)
Returns the data of a
BufferedImage as a binary output stream. |
static ByteArrayOutputStream |
writeImage(ImageWriter writer,
BufferedImage image,
float compressionQuality)
Returns the data of a
BufferedImage as a binary output stream. |
public static ImageReader getImageReader(String aMimeType)
aMimeType
- MIME typenull
if no image reader could be created for the
given MIME type.public static ImageWriter getImageWriter(String aMimeType)
aMimeType
- MIME typenull
if no image writer could be created for the
given MIME type.public static ByteArrayOutputStream writeImage(ImageWriter writer, BufferedImage image, float compressionQuality) throws IOException
BufferedImage
as a binary output stream. If the image is null
, a
stream of zero bytes is returned.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.IOException
- when creating the binary output stream failed.public static ByteArrayOutputStream writeImage(ImageWriter writer, BufferedImage image) throws IOException
BufferedImage
as a binary output stream. If the image is null
, a
stream of zero bytes is returned. The data is written with a compression quality of 1.writer
- the writer to use for writing the image data.image
- the image to write.IOException
- when creating the binary output stream failed.public static BufferedImage scaleImage(BufferedImage img, int targetWidth, int targetHeight, ImageUtils.ScalingStrategy strategy)
BufferedImage
.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 imageBufferedImage
, or null
if either the target width
or target height is 0 or less.@Deprecated public static BufferedImage scaleImage(BufferedImage original, Rectangle rectangle, Dimension targetDimension, Object hint, boolean highQuality)
cropImage(BufferedImage, Rectangle)
combined with
scaleImage(BufferedImage, int, int, ScalingStrategy)
insteadBufferedImage
.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 to RenderingHints.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, where targetWidth
or targetHeight
is smaller than the original dimensions, and generally
only when the BILINEAR
hint is specified)BufferedImage
, or null
if either the target width
or target height is 0 or less.public static double determineScalingFactor(double width, double height, double maxWidth, double maxHeight)
#MAX_PREVIEW_WIDTH
by #MAX_PREVIEW_HEIGHT
).width
- width of imageheight
- height of imagemaxWidth
- max width of imagemaxHeight
- max height of imagepublic static Dimension normalizeDimension(Dimension original, Dimension variant)
With this function a new dimension is created according to the original dimension.
original
- dimension of the original imagevariant
- dimension of the variant imagepublic static double determineResizeRatio(double sourceWidth, double sourceHeight, int targetWidth, int targetHeight)
sourceWidth
- The width of the source imagesourceHeight
- The height of the source imagetargetWidth
- The width of the target imagetargetHeight
- The height of the target imagepublic static BufferedImage cropImage(BufferedImage image, Rectangle cropArea)
BufferedImage
provided by the ImageReader
.image
- The original imagecropArea
- The rectangle used to crop the image@Deprecated public static BufferedImage scaleImage(BufferedImage img, int xOffset, int yOffset, int sourceWidth, int sourceHeight, int targetWidth, int targetHeight, Object hint, boolean highQuality)
scaleImage(BufferedImage, Rectangle, Dimension, Object, boolean)
instead.BufferedImage
.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 to RenderingHints.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, where targetWidth
or targetHeight
is smaller than the original dimensions, and generally
only when the BILINEAR
hint is specified)BufferedImage
, or null
if either the target width
or target height is 0 or less.public static InputStream convertToRGB(InputStream is, ColorModel colorModel) throws IOException, UnsupportedImageException
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
is
- the image datacolorModel
- the color model of the imageIOException
UnsupportedImageException
Copyright © 2007–2019 Hippo B.V. (http://www.onehippo.com). All rights reserved.