Class ImageIOUtil


  • public final class ImageIOUtil
    extends java.lang.Object
    Handles some ImageIO operations.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private static org.apache.commons.logging.Log LOG
      Log instance
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private ImageIOUtil()  
    • Method Summary

      All Methods Static Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method Description
      private static byte[] getAsDeflatedBytes​(java.awt.color.ICC_Profile profile)  
      private static javax.imageio.metadata.IIOMetadataNode getOrCreateChildNode​(javax.imageio.metadata.IIOMetadataNode parentNode, java.lang.String name)
      Gets the named child node, or creates and attaches it.
      private static boolean hasICCProfile​(java.awt.image.BufferedImage image)
      Determine if the given image has a ICC profile that should be embedded.
      private static void setDPI​(javax.imageio.metadata.IIOMetadata metadata, int dpi, java.lang.String formatName)  
      static boolean writeImage​(java.awt.image.BufferedImage image, java.lang.String filename, int dpi)
      Writes a buffered image to a file using the given image format.
      static boolean writeImage​(java.awt.image.BufferedImage image, java.lang.String filename, int dpi, float compressionQuality)
      Writes a buffered image to a file using the given image format.
      static boolean writeImage​(java.awt.image.BufferedImage image, java.lang.String formatName, java.io.OutputStream output)
      Writes a buffered image to a file using the given image format.
      static boolean writeImage​(java.awt.image.BufferedImage image, java.lang.String formatName, java.io.OutputStream output, int dpi)
      Writes a buffered image to a file using the given image format.
      static boolean writeImage​(java.awt.image.BufferedImage image, java.lang.String formatName, java.io.OutputStream output, int dpi, float compressionQuality)
      Writes a buffered image to a file using the given image format.
      static boolean writeImage​(java.awt.image.BufferedImage image, java.lang.String formatName, java.io.OutputStream output, int dpi, float compressionQuality, java.lang.String compressionType)
      Writes a buffered image to a file using the given image format.
      static boolean writeImage​(java.awt.image.BufferedImage image, java.lang.String formatName, java.lang.String filename, int dpi)
      Deprecated.
      use writeImage(BufferedImage image, String filename, int dpi), which uses the full filename instead of just the prefix.
      • Methods inherited from class java.lang.Object

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

      • LOG

        private static final org.apache.commons.logging.Log LOG
        Log instance
    • Constructor Detail

      • ImageIOUtil

        private ImageIOUtil()
    • Method Detail

      • writeImage

        public static boolean writeImage​(java.awt.image.BufferedImage image,
                                         java.lang.String filename,
                                         int dpi)
                                  throws java.io.IOException
        Writes a buffered image to a file using the given image format. The compression is set for maximum compression for PNG and maximum quality for all other file formats. See writeImage(BufferedImage image, String formatName, OutputStream output, int dpi, float compressionQuality) for more details.
        Parameters:
        image - the image to be written
        filename - used to construct the filename for the individual image. Its suffix will be used as the image format.
        dpi - the resolution in dpi (dots per inch) to be used in metadata
        Returns:
        true if the image file was produced, false if there was an error.
        Throws:
        java.io.IOException - if an I/O error occurs
      • writeImage

        public static boolean writeImage​(java.awt.image.BufferedImage image,
                                         java.lang.String filename,
                                         int dpi,
                                         float compressionQuality)
                                  throws java.io.IOException
        Writes a buffered image to a file using the given image format. See writeImage(BufferedImage image, String formatName, OutputStream output, int dpi, float compressionQuality) for more details.
        Parameters:
        image - the image to be written
        filename - used to construct the filename for the individual image. Its suffix will be used as the image format.
        dpi - the resolution in dpi (dots per inch) to be used in metadata
        compressionQuality - quality to be used when compressing the image (0 < compressionQuality < 1.0f). See ImageWriteParam.setCompressionQuality(float) for more details.
        Returns:
        true if the image file was produced, false if there was an error.
        Throws:
        java.io.IOException - if an I/O error occurs
      • writeImage

        @Deprecated
        public static boolean writeImage​(java.awt.image.BufferedImage image,
                                         java.lang.String formatName,
                                         java.lang.String filename,
                                         int dpi)
                                  throws java.io.IOException
        Deprecated.
        use writeImage(BufferedImage image, String filename, int dpi), which uses the full filename instead of just the prefix.
        Writes a buffered image to a file using the given image format. See writeImage(BufferedImage image, String formatName, OutputStream output, int dpi, float compressionQuality) for more details.
        Parameters:
        image - the image to be written
        formatName - the target format (ex. "png") which is also the suffix for the filename
        filename - used to construct the filename for the individual image. The formatName parameter will be used as the suffix.
        dpi - the resolution in dpi (dots per inch) to be used in metadata
        Returns:
        true if the image file was produced, false if there was an error.
        Throws:
        java.io.IOException - if an I/O error occurs
      • writeImage

        public static boolean writeImage​(java.awt.image.BufferedImage image,
                                         java.lang.String formatName,
                                         java.io.OutputStream output)
                                  throws java.io.IOException
        Writes a buffered image to a file using the given image format. The compression is set for maximum compression for PNG and maximum quality for all other file formats. See writeImage(BufferedImage image, String formatName, OutputStream output, int dpi, float compressionQuality) for more details.
        Parameters:
        image - the image to be written
        formatName - the target format (ex. "png")
        output - the output stream to be used for writing
        Returns:
        true if the image file was produced, false if there was an error.
        Throws:
        java.io.IOException - if an I/O error occurs
      • writeImage

        public static boolean writeImage​(java.awt.image.BufferedImage image,
                                         java.lang.String formatName,
                                         java.io.OutputStream output,
                                         int dpi)
                                  throws java.io.IOException
        Writes a buffered image to a file using the given image format. The compression is set for maximum compression for PNG and maximum quality for all other file formats. See writeImage(BufferedImage image, String formatName, OutputStream output, int dpi, float compressionQuality) for more details.
        Parameters:
        image - the image to be written
        formatName - the target format (ex. "png")
        output - the output stream to be used for writing
        dpi - the resolution in dpi (dots per inch) to be used in metadata
        Returns:
        true if the image file was produced, false if there was an error.
        Throws:
        java.io.IOException - if an I/O error occurs
      • writeImage

        public static boolean writeImage​(java.awt.image.BufferedImage image,
                                         java.lang.String formatName,
                                         java.io.OutputStream output,
                                         int dpi,
                                         float compressionQuality)
                                  throws java.io.IOException
        Writes a buffered image to a file using the given image format. Compression is fixed for PNG, GIF, BMP and WBMP, dependent of the compressionQuality parameter for JPG, and dependent of bit count for TIFF (a bitonal image will be compressed with CCITT G4, a color image with LZW). Creating a TIFF image is only supported if the jai_imageio library (or equivalent) is in the class path.
        Parameters:
        image - the image to be written
        formatName - the target format (ex. "png")
        output - the output stream to be used for writing
        dpi - the resolution in dpi (dots per inch) to be used in metadata
        compressionQuality - quality to be used when compressing the image (0 < compressionQuality < 1.0f). See ImageWriteParam.setCompressionQuality(float) for more details.
        Returns:
        true if the image file was produced, false if there was an error.
        Throws:
        java.io.IOException - if an I/O error occurs
      • writeImage

        public static boolean writeImage​(java.awt.image.BufferedImage image,
                                         java.lang.String formatName,
                                         java.io.OutputStream output,
                                         int dpi,
                                         float compressionQuality,
                                         java.lang.String compressionType)
                                  throws java.io.IOException
        Writes a buffered image to a file using the given image format. Compression is fixed for PNG, GIF, BMP and WBMP, dependent of the compressionQuality parameter for JPG, and dependent of bit count for TIFF (a bitonal image will be compressed with CCITT G4, a color image with LZW). Creating a TIFF image is only supported if the jai_imageio library is in the class path.
        Parameters:
        image - the image to be written
        formatName - the target format (ex. "png")
        output - the output stream to be used for writing
        dpi - the resolution in dpi (dots per inch) to be used in metadata
        compressionQuality - quality to be used when compressing the image (0 < compressionQuality < 1.0f). See ImageWriteParam.setCompressionQuality(float) for more details.
        compressionType - Advanced users only, and only relevant for TIFF files: If null, save uncompressed; if empty string, use logic explained above; other valid values are found in the javadoc of TIFFImageWriteParam.
        Returns:
        true if the image file was produced, false if there was an error.
        Throws:
        java.io.IOException - if an I/O error occurs
      • hasICCProfile

        private static boolean hasICCProfile​(java.awt.image.BufferedImage image)
        Determine if the given image has a ICC profile that should be embedded.
        Parameters:
        image - the image to analyse
        Returns:
        true if this image has an ICC profile, that is different from sRGB.
      • getAsDeflatedBytes

        private static byte[] getAsDeflatedBytes​(java.awt.color.ICC_Profile profile)
                                          throws java.io.IOException
        Throws:
        java.io.IOException
      • getOrCreateChildNode

        private static javax.imageio.metadata.IIOMetadataNode getOrCreateChildNode​(javax.imageio.metadata.IIOMetadataNode parentNode,
                                                                                   java.lang.String name)
        Gets the named child node, or creates and attaches it.
        Parameters:
        parentNode - the parent node
        name - name of the child node
        Returns:
        the existing or just created child node
      • setDPI

        private static void setDPI​(javax.imageio.metadata.IIOMetadata metadata,
                                   int dpi,
                                   java.lang.String formatName)
                            throws javax.imageio.metadata.IIOInvalidTreeException
        Throws:
        javax.imageio.metadata.IIOInvalidTreeException