Class ImageUtils


  • public class ImageUtils
    extends Object
    Generic image manipulation utility methods.
    • Constructor Detail

      • ImageUtils

        protected ImageUtils()
        Prevent instantiation
    • Method Detail

      • getImageReader

        public static ImageReader getImageReader​(String aMimeType)
        Returns an image reader for a MIME type.
        Parameters:
        aMimeType - MIME type
        Returns:
        an image reader for the given MIME type, or null if no image reader could be created for the given MIME type.
      • getImageWriter

        public static ImageWriter getImageWriter​(String aMimeType)
        Returns an image writer for a MIME type.
        Parameters:
        aMimeType - MIME type
        Returns:
        an image writer for the given MIME type, or null if no image writer could be created for the given MIME type.
      • writeImage

        public static ByteArrayOutputStream writeImage​(ImageWriter writer,
                                                       BufferedImage image,
                                                       float compressionQuality)
                                                throws IOException
        Returns the data of a BufferedImage as a binary output stream. If the image is null, a stream of zero bytes is returned.
        Parameters:
        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.
        Returns:
        an output stream with the data of the given image.
        Throws:
        IOException - when creating the binary output stream failed.
      • writeImage

        public static ByteArrayOutputStream writeImage​(ImageWriter writer,
                                                       BufferedImage image)
                                                throws IOException
        Returns the data of a 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.
        Parameters:
        writer - the writer to use for writing the image data.
        image - the image to write.
        Returns:
        an output stream with the data of the given image.
        Throws:
        IOException - when creating the binary output stream failed.
      • scaleImage

        public static BufferedImage scaleImage​(BufferedImage img,
                                               int targetWidth,
                                               int targetHeight,
                                               ImageUtils.ScalingStrategy strategy)
        Returns a scaled instance of the provided BufferedImage.
        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
        strategy - the strategy to use for scaling the image
        Returns:
        a scaled version of the original BufferedImage, or null if either the target width or target height is 0 or less.
      • determineScalingFactor

        public 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. #MAX_PREVIEW_WIDTH by #MAX_PREVIEW_HEIGHT).
        Parameters:
        width - width of image
        height - height of image
        maxWidth - max width of image
        maxHeight - max height of image
        Returns:
        the scaling factor of the image
      • normalizeDimension

        public static Dimension normalizeDimension​(Dimension original,
                                                   Dimension variant)
        If height or width in the variant dimension is equal to 0 it is a special case. The value 0 represents a value that according to the dimension of the original image.

        With this function a new dimension is created according to the original dimension.

        Parameters:
        original - dimension of the original image
        variant - dimension of the variant image
        Returns:
        scaled dimension based on width or height value
      • determineResizeRatio

        public 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.
        Parameters:
        sourceWidth - The width of the source image
        sourceHeight - The height of the source image
        targetWidth - The width of the target image
        targetHeight - The height of the target image
        Returns:
        The resize ratio
      • cropImage

        public static BufferedImage cropImage​(BufferedImage image,
                                              Rectangle cropArea)
        Returns a cropped instance of the BufferedImage provided by the ImageReader.
        Parameters:
        image - The original image
        cropArea - The rectangle used to crop the image
        Returns:
        a cropped version of the original image.
      • scaleImage

        @Deprecated
        public static BufferedImage scaleImage​(BufferedImage img,
                                               int xOffset,
                                               int yOffset,
                                               int sourceWidth,
                                               int sourceHeight,
                                               int targetWidth,
                                               int targetHeight,
                                               Object hint,
                                               boolean highQuality)
        Returns a scaled instance of the provided BufferedImage.
        Parameters:
        img - the original image to be scaled
        xOffset - 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 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)
        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)
        Returns:
        a scaled version of the original BufferedImage, or null if either the target width or target height is 0 or less.
      • convertToRGB

        public static InputStream convertToRGB​(InputStream is,
                                               ColorModel colorModel)
                                        throws IOException,
                                               UnsupportedImageException
        Converts image raster data to a JPEG with RGB color space. Only images with color space CMYK and YCCK are converted, other images are left untouched.

        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

        Parameters:
        is - the image data
        colorModel - the color model of the image
        Returns:
        the RGB version of the supplied image
        Throws:
        IOException
        UnsupportedImageException