Package com.jalios.io
Class ImageUtil
- java.lang.Object
-
- com.jalios.io.ImageUtil
-
public class ImageUtil extends java.lang.Object
Utility class to manipulate image.- Since:
- jcms-5.5.0
- Author:
- Olivier Jaquemet
-
-
Constructor Summary
Constructors Constructor Description ImageUtil()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static java.io.File
createGIFThumbnail(java.io.File imageFile, int maxWidth, int maxHeight)
Creates a thumbnail of the given file image in a temporary location in the GIF format, return the corresponding File.static java.io.File
createJPEGThumbnail(java.io.File imageFile, int maxWidth, int maxHeight)
Creates a thumbnail of the given file image in a temporary location in the JPEG format, return the corresponding File.static java.io.File
createPNGThumbnail(java.io.File imageFile, int maxWidth, int maxHeight)
Creates a thumbnail of the given file image in a temporary location in the JPEG format, return the corresponding File.static java.io.File
createThumbnail(java.io.File imageFile, ImageFormat format, int maxWidth, int maxHeight)
Creates a thumbnail of the given file image in a temporary locationstatic java.io.File
createThumbnail(java.io.File imageFile, ImageFormat format, int maxWidth, int maxHeight, java.lang.String background)
Creates a thumbnail of the given file image in a temporary location in the given format, return the corresponding File.static java.io.File
createThumbnail(java.io.File imageFile, ImageFormat format, int maxWidth, int maxHeight, java.lang.String background, boolean square)
Creates a thumbnail of the given file image in a temporary location in the given format, return the corresponding File.static java.io.File
crop(java.io.File imageFile, ImageFormat format, int x1, int y1, int x2, int y2, java.lang.String background)
static java.awt.image.BufferedImage
cropImage(java.awt.image.BufferedImage image, int x1, int y1, int x2, int y2, java.lang.String background)
static java.awt.image.BufferedImage
cropSquare(java.awt.image.BufferedImage image, java.lang.String background)
Crop the given image.static java.io.File
cropSquare(java.io.File file, ImageFormat format, java.lang.String background)
Crop the given file image.static java.awt.image.BufferedImage
flipHorizontally(java.awt.image.BufferedImage image)
Flip the buffered image horizontally.static java.awt.image.BufferedImage
flipVertically(java.awt.image.BufferedImage image)
Flip the buffered image vertically.static java.awt.Dimension
getDimension(java.io.File file)
Returns the dimension of the given image.static ImageFormat
getImageFormat(java.io.File file, ImageFormat defaultImageFormat)
Returns the ImageFormat of the specified File using its extension.static java.awt.image.BufferedImage
getScaledInstance(java.awt.image.BufferedImage image, int targetWidth, int targetHeight, java.lang.Object hint, boolean higherQuality)
Convenience method that returns a scaled instance of the providedBufferedImage
.static java.lang.String[]
getSupportedReadFormat()
static java.lang.String[]
getSupportedWriteFormat()
static boolean
isImage(java.io.File file)
Detects if the given file is an image file, using its extension.static boolean
isImageGreaterThan(java.io.File imageFile, int maxWidth, int maxHeight)
Returns true if image's width and height are both greater or equal to maxWidth and maxHeight ,false if imageFile is null or is not image.static java.awt.image.BufferedImage
readImage(java.io.File file)
Read the given image file and return the BufferedImagestatic java.awt.image.BufferedImage
resizeImage(java.awt.image.BufferedImage image, int maxWidth, int maxHeight, boolean doNotEnlarge, java.lang.String background)
Resizes the given BufferedImage to a new BufferedImage file with the given sizes.static java.io.File
rotate(java.io.File imageFile, ImageFormat format, int angle, java.lang.String background)
static java.io.File
rotateAndFlipHorizontally(java.io.File imageFile, ImageFormat format, int angle, java.lang.String background)
Flip the image horizontally.static java.io.File
rotateAndFlipVertically(java.io.File imageFile, ImageFormat format, int angle, java.lang.String background)
Flip the image vertically.static java.awt.image.BufferedImage
rotateImage(java.awt.image.BufferedImage image, int angle, java.lang.String background)
static void
setQualityRenderingHints(java.awt.Graphics2D g2d)
static java.io.File
writeGIF(java.awt.image.BufferedImage image, java.io.File outFile)
Write the given BufferedImage to the given outfile in GIF format.static java.io.File
writeJPEG(java.awt.image.BufferedImage image, java.io.File outFile)
Write the given BufferedImage to the given outfile in JPEG format.static java.io.File
writePNG(java.awt.image.BufferedImage image, java.io.File outFile)
Write the given BufferedImage to the given outfile in PNG format.
-
-
-
Method Detail
-
isImage
public static boolean isImage(java.io.File file)
Detects if the given file is an image file, using its extension.This method only use the extension of the specified file to check if file is an image, not the content of the file itself.
Use extensions supported byImageFormat
:"jpg"
"jpeg"
"gif"
"png"
Specified file must exists to be considered an image.
- Parameters:
file
- the file to check- Returns:
- true if the given file is an image, false otherwise.
-
getImageFormat
public static ImageFormat getImageFormat(java.io.File file, ImageFormat defaultImageFormat)
Returns the ImageFormat of the specified File using its extension.If file's image format could not be detected from its extension, return the specified default.
This method only use the extension of the specified file to detect the file format, not the content of the file itself.- Parameters:
file
- the file to checkdefaultImageFormat
- the ImageFormat to return if file's image format could not be detected- Returns:
- an ImageFormat instance (may return null if the defaultImageFormat specified is null)
- Since:
- jcms-10.0.4 / JCMS-7942
-
createGIFThumbnail
public static java.io.File createGIFThumbnail(java.io.File imageFile, int maxWidth, int maxHeight)
Creates a thumbnail of the given file image in a temporary location in the GIF format, return the corresponding File.- Parameters:
imageFile
- the image file of which to make a thumbnail.maxWidth
- the maximum width of the output imagemaxHeight
- the maximum height of the output image- Returns:
- the thumbnail File or null if it could not be created.
- See Also:
createThumbnail(File, ImageFormat, int, int)
-
createJPEGThumbnail
public static java.io.File createJPEGThumbnail(java.io.File imageFile, int maxWidth, int maxHeight)
Creates a thumbnail of the given file image in a temporary location in the JPEG format, return the corresponding File.- Parameters:
imageFile
- the image file of which to make a thumbnail.maxWidth
- the maximum width of the output imagemaxHeight
- the maximum height of the output image- Returns:
- the thumbnail File or null if it could not be created.
- See Also:
createThumbnail(File, ImageFormat, int, int)
-
createPNGThumbnail
public static java.io.File createPNGThumbnail(java.io.File imageFile, int maxWidth, int maxHeight)
Creates a thumbnail of the given file image in a temporary location in the JPEG format, return the corresponding File.- Parameters:
imageFile
- the image file of which to make a thumbnail.maxWidth
- the maximum width of the output imagemaxHeight
- the maximum height of the output image- Returns:
- the thumbnail File or null if it could not be created.
- Since:
- JCMS-4762
- See Also:
createThumbnail(File, ImageFormat, int, int)
-
createThumbnail
public static java.io.File createThumbnail(java.io.File imageFile, ImageFormat format, int maxWidth, int maxHeight)
Creates a thumbnail of the given file image in a temporary location- Parameters:
imageFile
- the image file of which to make a thumbnail.format
- the image format to use,ImageFormat.GIF
,ImageFormat.JPEG
, if null theImageFormat.JPEG
format is usedmaxWidth
- the maximum width of the output imagemaxHeight
- the maximum height of the output image- Returns:
- the thumbnail File or null if it could not be created.
- See Also:
createThumbnail(File, ImageFormat, int, int, String)
-
createThumbnail
public static java.io.File createThumbnail(java.io.File imageFile, ImageFormat format, int maxWidth, int maxHeight, java.lang.String background)
Creates a thumbnail of the given file image in a temporary location in the given format, return the corresponding File.- Parameters:
imageFile
- the image file of which to make a thumbnail.format
- the image format to use,ImageFormat.GIF
,ImageFormat.JPEG
, if null theImageFormat.JPEG
format is usedmaxWidth
- the maximum width of the output imagemaxHeight
- the maximum height of the output imagebackground
- the background color (eg "FFFFFF") if sets the image dimension will be set maxWidth and maxHeight- Returns:
- the thumbnail File or null if it could not be created.
-
createThumbnail
public static java.io.File createThumbnail(java.io.File imageFile, ImageFormat format, int maxWidth, int maxHeight, java.lang.String background, boolean square)
Creates a thumbnail of the given file image in a temporary location in the given format, return the corresponding File.- Parameters:
imageFile
- the image file of which to make a thumbnail.format
- the image format to use,ImageFormat.GIF
,ImageFormat.JPEG
, if null theImageFormat.JPEG
format is usedmaxWidth
- the maximum width of the output imagemaxHeight
- the maximum height of the output imagebackground
- the background color (eg "FFFFFF") if sets the image dimension will be set maxWidth and maxHeightsquare
- set to true to crop image into a square using the specified width during thumbnail creation.- Returns:
- the thumbnail File or null if it could not be created.
- Since:
- jcms-9.0
-
readImage
public static java.awt.image.BufferedImage readImage(java.io.File file)
Read the given image file and return the BufferedImage- Parameters:
file
- the image file to read- Returns:
- a new BufferedImage or null if the file could not be read.
- Throws:
java.lang.IllegalArgumentException
- if file is null.
-
setQualityRenderingHints
public static void setQualityRenderingHints(java.awt.Graphics2D g2d)
-
getScaledInstance
public static java.awt.image.BufferedImage getScaledInstance(java.awt.image.BufferedImage image, int targetWidth, int targetHeight, java.lang.Object hint, boolean higherQuality)
Convenience method that returns a scaled instance of the providedBufferedImage
. http://today.java.net/pub/a/today/2007/04/03/perils-of-image-getscaledinstance.html- Parameters:
image
- the original image to be scaledtargetWidth
- 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
)higherQuality
- 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
-
rotate
public static java.io.File rotate(java.io.File imageFile, ImageFormat format, int angle, java.lang.String background)
-
rotateAndFlipHorizontally
public static java.io.File rotateAndFlipHorizontally(java.io.File imageFile, ImageFormat format, int angle, java.lang.String background)
Flip the image horizontally.- Parameters:
imageFile
- the image Fileformat
- the image format to useangle
- rotation to apply, must be a multiple of 90background
- the background color- Returns:
- re-oriented file image or null
- Since:
- jcms-10.0.8 / JCMS-9549
-
rotateAndFlipVertically
public static java.io.File rotateAndFlipVertically(java.io.File imageFile, ImageFormat format, int angle, java.lang.String background)
Flip the image vertically.- Parameters:
imageFile
- the image Fileformat
- the image format to useangle
- rotation to apply, must be a multiple of 90background
- the background color- Returns:
- re-oriented file image or null
- Since:
- jcms-10.0.8 / JCMS-9549
-
flipHorizontally
public static java.awt.image.BufferedImage flipHorizontally(java.awt.image.BufferedImage image)
Flip the buffered image horizontally.- Parameters:
image
- the image File- Returns:
- re-oriented file image or null
- Since:
- jcms-10.0.8 / JCMS-9549
-
flipVertically
public static java.awt.image.BufferedImage flipVertically(java.awt.image.BufferedImage image)
Flip the buffered image vertically.- Parameters:
image
- the image File- Returns:
- re-oriented file image or null
- Since:
- jcms-10.0.8 / JCMS-9549
-
rotateImage
public static java.awt.image.BufferedImage rotateImage(java.awt.image.BufferedImage image, int angle, java.lang.String background)
-
crop
public static java.io.File crop(java.io.File imageFile, ImageFormat format, int x1, int y1, int x2, int y2, java.lang.String background)
-
cropSquare
public static java.io.File cropSquare(java.io.File file, ImageFormat format, java.lang.String background) throws java.io.FileNotFoundException
Crop the given file image.- Parameters:
file
- the image to cropformat
- the image format to use,ImageFormat.GIF
,ImageFormat.JPEG
, if null theImageFormat.JPEG
format is usedbackground
- the background color- Returns:
- the cropped image or null if it has not been cropped.
- Throws:
java.io.FileNotFoundException
- if an error occurs- Since:
- jcms-9.0.0
-
cropSquare
public static java.awt.image.BufferedImage cropSquare(java.awt.image.BufferedImage image, java.lang.String background)
Crop the given image.- Parameters:
image
- the image to cropbackground
- the background color- Returns:
- the cropped image or null if it has not been cropped.
- Since:
- jcms-9.0.0
-
getDimension
public static java.awt.Dimension getDimension(java.io.File file) throws java.io.FileNotFoundException
Returns the dimension of the given image.- Parameters:
file
- the image- Returns:
- the dimension of the given image.
- Throws:
java.io.FileNotFoundException
- if an error occurs- Since:
- jcms-9.0.0
-
cropImage
public static java.awt.image.BufferedImage cropImage(java.awt.image.BufferedImage image, int x1, int y1, int x2, int y2, java.lang.String background)
-
resizeImage
public static java.awt.image.BufferedImage resizeImage(java.awt.image.BufferedImage image, int maxWidth, int maxHeight, boolean doNotEnlarge, java.lang.String background)
Resizes the given BufferedImage to a new BufferedImage file with the given sizes.- Parameters:
image
- the image to resize.maxWidth
- the maximum width of the output imagemaxHeight
- the maximum height of the output imagedoNotEnlarge
- do not increase image size if image is smaller than given maximum dimension.background
- the background color (eg "FFFFFF") if sets the image dimension will be set maxWidth and maxHeight- Returns:
- the resized BufferedImage
- Throws:
java.lang.IllegalArgumentException
- if image is null.
-
writeJPEG
public static java.io.File writeJPEG(java.awt.image.BufferedImage image, java.io.File outFile)
Write the given BufferedImage to the given outfile in JPEG format.
Delete the given file if it already exists.- Parameters:
image
- the BufferedImage to write to a file.outFile
- the file in which to write the image- Returns:
- the given File or null if the image could not be written.
- Throws:
java.lang.IllegalArgumentException
- if image or outFile is null.
-
writeGIF
public static java.io.File writeGIF(java.awt.image.BufferedImage image, java.io.File outFile)
Write the given BufferedImage to the given outfile in GIF format.
Delete the given file if it already exists.- Parameters:
image
- the BufferedImage to write to a file.outFile
- the file in which to write the image- Returns:
- the given File or null if the image could not be written.
- Throws:
java.lang.IllegalArgumentException
- if image or outFile is null.
-
writePNG
public static java.io.File writePNG(java.awt.image.BufferedImage image, java.io.File outFile)
Write the given BufferedImage to the given outfile in PNG format.
Delete the given file if it already exists.- Parameters:
image
- the BufferedImage to write to a file.outFile
- the file in which to write the image- Returns:
- the given File or null if the image could not be written.
- Throws:
java.lang.IllegalArgumentException
- if image or outFile is null.- Since:
- JCMS-4762
-
getSupportedReadFormat
public static java.lang.String[] getSupportedReadFormat()
- Returns:
- an array of image format that can be read.
- See Also:
ImageIO.getReaderFormatNames()
-
getSupportedWriteFormat
public static java.lang.String[] getSupportedWriteFormat()
- Returns:
- an array of image format that can be written.
- See Also:
ImageIO.getWriterFormatNames()
-
isImageGreaterThan
public static boolean isImageGreaterThan(java.io.File imageFile, int maxWidth, int maxHeight)
Returns true if image's width and height are both greater or equal to maxWidth and maxHeight ,false if imageFile is null or is not image.- Parameters:
imageFile
- the image FilemaxWidth
- max widthmaxHeight
- max height- Returns:
- true if image's width and height are both greater or equal to maxWidth and maxHeight ,false if imageFile is null or is not image. ImageUtil.isImage(imageFile)
- Since:
- jcms-10.0.0
-
-