Package com.jalios.io
Class ImageUtil
java.lang.Object
com.jalios.io.ImageUtil
Utility class to manipulate image.
- Since:
- jcms-5.5.0
- Author:
- Olivier Jaquemet
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic FilecreateGIFThumbnail(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 FilecreateJPEGThumbnail(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 FilecreatePNGThumbnail(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 FilecreateThumbnail(File imageFile, ImageFormat format, int maxWidth, int maxHeight) Creates a thumbnail of the given file image in a temporary locationstatic FilecreateThumbnail(File imageFile, ImageFormat format, int maxWidth, int maxHeight, String background) Creates a thumbnail of the given file image in a temporary location in the given format, return the corresponding File.static FilecreateThumbnail(File imageFile, ImageFormat format, int maxWidth, int maxHeight, 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 Filecrop(File imageFile, ImageFormat format, int x1, int y1, int x2, int y2, String background) static BufferedImagecropImage(BufferedImage image, int x1, int y1, int x2, int y2, String background) static BufferedImagecropSquare(BufferedImage image, String background) Crop the given image.static FilecropSquare(File file, ImageFormat format, String background) Crop the given file image.static BufferedImageflipHorizontally(BufferedImage image) Flip the buffered image horizontally.static BufferedImageflipVertically(BufferedImage image) Flip the buffered image vertically.static DimensiongetDimension(File file) Returns the dimension of the given image.static ImageFormatgetImageFormat(File file, ImageFormat defaultImageFormat) Returns the ImageFormat of the specified File using its extension.static BufferedImagegetScaledInstance(BufferedImage image, int targetWidth, int targetHeight, Object hint, boolean higherQuality) Convenience method that returns a scaled instance of the providedBufferedImage.static String[]static String[]static booleanDetects if the given file is an image file, using its extension.static booleanisImageGreaterThan(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 BufferedImageRead the given image file and return the BufferedImagestatic BufferedImageresizeImage(BufferedImage image, int maxWidth, int maxHeight, boolean doNotEnlarge, String background) Resizes the given BufferedImage to a new BufferedImage file with the given sizes.static Filerotate(File imageFile, ImageFormat format, int angle, String background) static FilerotateAndFlipHorizontally(File imageFile, ImageFormat format, int angle, String background) Flip the image horizontally.static FilerotateAndFlipVertically(File imageFile, ImageFormat format, int angle, String background) Flip the image vertically.static BufferedImagerotateImage(BufferedImage image, int angle, String background) static voidstatic FilewriteGIF(BufferedImage image, File outFile) Write the given BufferedImage to the given outfile in GIF format.static FilewriteJPEG(BufferedImage image, File outFile) Write the given BufferedImage to the given outfile in JPEG format.static FilewritePNG(BufferedImage image, File outFile) Write the given BufferedImage to the given outfile in PNG format.
-
Constructor Details
-
ImageUtil
public ImageUtil()
-
-
Method Details
-
isImage
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
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
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:
-
createJPEGThumbnail
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:
-
createPNGThumbnail
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
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.JPEGformat 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
public static File createThumbnail(File imageFile, ImageFormat format, int maxWidth, int maxHeight, 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.JPEGformat 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 File createThumbnail(File imageFile, ImageFormat format, int maxWidth, int maxHeight, 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.JPEGformat 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
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:
IllegalArgumentException- if file is null.
-
setQualityRenderingHints
-
getScaledInstance
public static BufferedImage getScaledInstance(BufferedImage image, int targetWidth, int targetHeight, 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, wheretargetWidthortargetHeightis smaller than the original dimensions, and generally only when theBILINEARhint is specified)- Returns:
- a scaled version of the original
BufferedImage
-
rotate
-
rotateAndFlipHorizontally
public static File rotateAndFlipHorizontally(File imageFile, ImageFormat format, int angle, 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 File rotateAndFlipVertically(File imageFile, ImageFormat format, int angle, 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
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
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
-
crop
public static File crop(File imageFile, ImageFormat format, int x1, int y1, int x2, int y2, String background) -
cropSquare
public static File cropSquare(File file, ImageFormat format, String background) throws FileNotFoundException Crop the given file image.- Parameters:
file- the image to cropformat- the image format to use,ImageFormat.GIF,ImageFormat.JPEG, if null theImageFormat.JPEGformat is usedbackground- the background color- Returns:
- the cropped image or null if it has not been cropped.
- Throws:
FileNotFoundException- if an error occurs- Since:
- jcms-9.0.0
-
cropSquare
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
Returns the dimension of the given image.- Parameters:
file- the image- Returns:
- the dimension of the given image.
- Throws:
FileNotFoundException- if an error occurs- Since:
- jcms-9.0.0
-
cropImage
public static BufferedImage cropImage(BufferedImage image, int x1, int y1, int x2, int y2, String background) -
resizeImage
public static BufferedImage resizeImage(BufferedImage image, int maxWidth, int maxHeight, boolean doNotEnlarge, 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:
IllegalArgumentException- if image is null.
-
writeJPEG
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:
IllegalArgumentException- if image or outFile is null.
-
writeGIF
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:
IllegalArgumentException- if image or outFile is null.
-
writePNG
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:
IllegalArgumentException- if image or outFile is null.- Since:
- JCMS-4762
-
getSupportedReadFormat
- Returns:
- an array of image format that can be read.
- See Also:
-
getSupportedWriteFormat
- Returns:
- an array of image format that can be written.
- See Also:
-
isImageGreaterThan
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
-