Class ImageUtil


  • public class ImageUtil
    extends java.lang.Object
    Static utility methods for working with images. Meant to suggest "best practices" for the most straightforward cases of working with images.
    • Constructor Summary

      Constructors 
      Constructor Description
      ImageUtil()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.awt.image.BufferedImage convertToBufferedImage​(java.awt.Image awtImg, int type)
      Utility method to convert an AWT Image to a BufferedImage.
      static java.awt.image.BufferedImage createCompatibleBufferedImage​(int width, int height, int biType)
      Helper method to instantiate new BufferedImages; if the graphics environment is actually connected to real screen devices (e.g.
      static java.awt.image.BufferedImage createTransparentImage​(int width, int height)  
      static byte[] fromBase64Encoded​(java.lang.String b64encoded)  
      static byte[] getEmbeddedBase64Image​(java.lang.String imageDataUri)
      Get the binary content of an embedded base 64 image.
      private static java.awt.GraphicsConfiguration getGraphicsConfiguration()  
      static java.awt.image.BufferedImage getScaledInstance​(ScalingOptions opt, java.awt.image.BufferedImage orgImage)
      Scales an image to the requested width and height, assuming these are both >= 1; size given in pixels.
      static java.awt.image.BufferedImage getScaledInstance​(java.awt.image.BufferedImage orgImage, int targetWidth, int targetHeight)
      Scales an image to the requested width and height, assuming these are both >= 1; size given in pixels.
      static java.awt.image.BufferedImage makeCompatible​(java.awt.image.BufferedImage bimg)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • ImageUtil

        public ImageUtil()
    • Method Detail

      • makeCompatible

        public static java.awt.image.BufferedImage makeCompatible​(java.awt.image.BufferedImage bimg)
      • createCompatibleBufferedImage

        public static java.awt.image.BufferedImage createCompatibleBufferedImage​(int width,
                                                                                 int height,
                                                                                 int biType)
        Helper method to instantiate new BufferedImages; if the graphics environment is actually connected to real screen devices (e.g. not in headless mode), the image will be compatible with the screen device allowing for best performance. In a headless environment, simply creates a new BufferedImage. For non-headless environments, this just sets up and calls GraphicsConfiguration.createCompatibleImage(int,int,int). The image will not have anything drawn to it, not even a white background; you must do this yourself.
        Parameters:
        width - Target width for the image
        height - Target height for the image
        biType - Value from the BufferedImage class; see docs for BufferedImage(int,int,int). The actual type used will be the type specified in this parameter, if in headless mode, or the type most compatible with the screen, if in non-headless more.
        Returns:
        A BufferedImage compatible with the screen (best fit).
      • getGraphicsConfiguration

        private static java.awt.GraphicsConfiguration getGraphicsConfiguration()
      • getScaledInstance

        public static java.awt.image.BufferedImage getScaledInstance​(ScalingOptions opt,
                                                                     java.awt.image.BufferedImage orgImage)
        Scales an image to the requested width and height, assuming these are both >= 1; size given in pixels. If either width or height is <=0, the current image width or height will be used. This method assumes that, at the moment the method is called, the width and height of the image are available; it won't wait for them. Therefore, the method should be called once the image has completely loaded and not before.
        Override this method in a subclass to optimize image scaling operations; note that the legacy Image.getScaledInstance(int,int,int) is considered to perform poorly compared to more recent developed techniques.
        For a discussion of the options from a member of the Java2D team, see http://today.java.net/pub/a/today/2007/04/03/perils-of-image-getscaledinstance.html
        Parameters:
        orgImage - The image to scale
        Returns:
        The scaled image instance.
      • getScaledInstance

        public static java.awt.image.BufferedImage getScaledInstance​(java.awt.image.BufferedImage orgImage,
                                                                     int targetWidth,
                                                                     int targetHeight)
        Scales an image to the requested width and height, assuming these are both >= 1; size given in pixels. If either width or height is <=0, the current image width or height will be used. This method assumes y that, at the moment the method is called, the width and height of the image are available; it won't wait for them. Therefore, the method should be called once the image has completely loaded and not before.
        Override this method in a subclass to optimize image scaling operations; note that the legacy Image.getScaledInstance(int,int,int) is considered to perform poorly compared to more recent developed techniques.
        For a discussion of the options from a member of the Java2D team, see http://today.java.net/pub/a/today/2007/04/03/perils-of-image-getscaledinstance.html
        Parameters:
        orgImage - The image to scale
        targetWidth - The target width in pixels
        targetHeight - The target height in pixels
        Returns:
        The scaled image instance.
      • convertToBufferedImage

        public static java.awt.image.BufferedImage convertToBufferedImage​(java.awt.Image awtImg,
                                                                          int type)
        Utility method to convert an AWT Image to a BufferedImage. Size is preserved, BufferedImage is compatible with current display device.
        Parameters:
        awtImg - image to convert; if already a BufferedImage, returned unmodified
        type - the type of BufferedImage to create; see BufferedImage(int,int,int)
        Returns:
        BufferedImage with same content.
      • createTransparentImage

        public static java.awt.image.BufferedImage createTransparentImage​(int width,
                                                                          int height)
      • fromBase64Encoded

        public static byte[] fromBase64Encoded​(java.lang.String b64encoded)
      • getEmbeddedBase64Image

        public static byte[] getEmbeddedBase64Image​(java.lang.String imageDataUri)
        Get the binary content of an embedded base 64 image.
        Parameters:
        imageDataUri - URI of the embedded image
        Returns:
        The binary content