Package com.jalios.io

Class ImageUtil

java.lang.Object
com.jalios.io.ImageUtil

public class ImageUtil extends Object
Utility class to manipulate image.
Since:
jcms-5.5.0
Author:
Olivier Jaquemet
  • Constructor Details

    • ImageUtil

      public ImageUtil()
  • Method Details

    • isImage

      public static boolean isImage(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 by ImageFormat :

      • "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(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 check
      defaultImageFormat - 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 File createGIFThumbnail(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 image
      maxHeight - the maximum height of the output image
      Returns:
      the thumbnail File or null if it could not be created.
      See Also:
    • createJPEGThumbnail

      public static File createJPEGThumbnail(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 image
      maxHeight - the maximum height of the output image
      Returns:
      the thumbnail File or null if it could not be created.
      See Also:
    • createPNGThumbnail

      public static File createPNGThumbnail(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 image
      maxHeight - 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

      public static File createThumbnail(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 the ImageFormat.JPEG format is used
      maxWidth - the maximum width of the output image
      maxHeight - 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 the ImageFormat.JPEG format is used
      maxWidth - the maximum width of the output image
      maxHeight - the maximum height of the output image
      background - 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 the ImageFormat.JPEG format is used
      maxWidth - the maximum width of the output image
      maxHeight - the maximum height of the output image
      background - the background color (eg "FFFFFF") if sets the image dimension will be set maxWidth and maxHeight
      square - 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 BufferedImage readImage(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:
      IllegalArgumentException - if file is null.
    • setQualityRenderingHints

      public static void setQualityRenderingHints(Graphics2D g2d)
    • getScaledInstance

      public static BufferedImage getScaledInstance(BufferedImage image, int targetWidth, int targetHeight, Object hint, boolean higherQuality)
      Convenience method that returns a scaled instance of the provided BufferedImage. http://today.java.net/pub/a/today/2007/04/03/perils-of-image-getscaledinstance.html
      Parameters:
      image - the original image to be scaled
      targetWidth - the desired width of the scaled instance, in pixels
      targetHeight - the desired height of the scaled instance, in pixels
      hint - 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)
      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, where targetWidth or targetHeight is smaller than the original dimensions, and generally only when the BILINEAR hint is specified)
      Returns:
      a scaled version of the original BufferedImage
    • rotate

      public static File rotate(File imageFile, ImageFormat format, int angle, String background)
    • rotateAndFlipHorizontally

      public static File rotateAndFlipHorizontally(File imageFile, ImageFormat format, int angle, String background)
      Flip the image horizontally.
      Parameters:
      imageFile - the image File
      format - the image format to use
      angle - rotation to apply, must be a multiple of 90
      background - 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 File
      format - the image format to use
      angle - rotation to apply, must be a multiple of 90
      background - the background color
      Returns:
      re-oriented file image or null
      Since:
      jcms-10.0.8 / JCMS-9549
    • flipHorizontally

      public static BufferedImage flipHorizontally(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 BufferedImage flipVertically(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 BufferedImage rotateImage(BufferedImage image, int angle, String background)
    • 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 crop
      format - the image format to use, ImageFormat.GIF, ImageFormat.JPEG, if null the ImageFormat.JPEG format is used
      background - 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

      public static BufferedImage cropSquare(BufferedImage image, String background)
      Crop the given image.
      Parameters:
      image - the image to crop
      background - the background color
      Returns:
      the cropped image or null if it has not been cropped.
      Since:
      jcms-9.0.0
    • getDimension

      public static Dimension getDimension(File file) throws FileNotFoundException
      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 image
      maxHeight - the maximum height of the output image
      doNotEnlarge - 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

      public static File writeJPEG(BufferedImage image, 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:
      IllegalArgumentException - if image or outFile is null.
    • writeGIF

      public static File writeGIF(BufferedImage image, 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:
      IllegalArgumentException - if image or outFile is null.
    • writePNG

      public static File writePNG(BufferedImage image, 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:
      IllegalArgumentException - if image or outFile is null.
      Since:
      JCMS-4762
    • getSupportedReadFormat

      public static String[] getSupportedReadFormat()
      Returns:
      an array of image format that can be read.
      See Also:
    • getSupportedWriteFormat

      public static String[] getSupportedWriteFormat()
      Returns:
      an array of image format that can be written.
      See Also:
    • isImageGreaterThan

      public static boolean isImageGreaterThan(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 File
      maxWidth - max width
      maxHeight - 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