com.jalios.io
Class ImageUtil

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

public class ImageUtil
extends Object

Utility class to manipulate image.

Since:
jcms-5.5.0
Version:
$Revision: 29654 $
Author:
Olivier Jaquemet

Field Summary
static String REVISION
           
 
Constructor Summary
ImageUtil()
           
 
Method Summary
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.
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.
static File createThumbnail(File imageFile, ImageFormat format, int maxWidth, int maxHeight)
           
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.
static File crop(File imageFile, ImageFormat format, int x1, int y1, int x2, int y2, String background)
           
static BufferedImage cropImage(BufferedImage image, int x1, int y1, int x2, int y2, String background)
           
static BufferedImage getScaledInstance(BufferedImage img, int targetWidth, int targetHeight, Object hint, boolean higherQuality)
          Convenience method that returns a scaled instance of the provided BufferedImage.
static String[] getSupportedReadFormat()
           
static String[] getSupportedWriteFormat()
           
static boolean isImage(File file)
          Detects if the given file is an image file, using its extension.
static BufferedImage readImage(File file)
          Read the given image file and return the BufferedImage
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.
static File rotate(File imageFile, ImageFormat format, int angle, String background)
           
static BufferedImage rotateImage(BufferedImage image, int angle, String background)
           
static void setQualityRenderingHints(Graphics2D g2d)
           
static File writeGIF(BufferedImage image, File outFile)
          Write the given BufferedImage to the given outfile in GIF format.
static File writeJPEG(BufferedImage image, File outFile)
          Write the given BufferedImage to the given outfile in JPEG format.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

REVISION

public static final String REVISION
See Also:
Constant Field Values
Constructor Detail

ImageUtil

public ImageUtil()
Method Detail

isImage

public static boolean isImage(File file)
Detects if the given file is an image file, using its extension.
Use those possibles extension:
  • "jpg"
  • "jpeg"
  • "gif"
  • "png"
  • Parameters:
    file - the file to check
    Returns:
    true if the given file is an image, false otherwise.

    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:
    createThumbnail(File, ImageFormat, int, int)

    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:
    createThumbnail(File, ImageFormat, int, int)

    createThumbnail

    public static File createThumbnail(File imageFile,
                                       ImageFormat format,
                                       int maxWidth,
                                       int maxHeight)
    See Also:
    createThumbnail(File, ImageFormat, int, int, String)

    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.

    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 img,
                                                  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:
    img - 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)

    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)

    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.

    getSupportedReadFormat

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

    getSupportedWriteFormat

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


    Copyright © 2001-2010 Jalios SA. All Rights Reserved.