Class ImageUtils


  • public class ImageUtils
    extends java.lang.Object
    Various static helper methods for Images

    System properties associated with thiks class.

    • com.priint.pubserver.util.ImageUtils.HTTP_CONNECT_TIMEOUT
      Timeout in msec for HTTP connections triggered by ImageUtils()
      Default: 5000
    • com.priint.pubserver.util.ImageUtils.HTTP_READ_TIMEOUT
      Timeout in msec for HTTP responses triggered by ImageUtils()
      Default: 10000
    • com.priint.pubserver.util.ImageUtils.HTTPS_ALLOW_INSECURE
      Flag to handle secure connections. If set to 1 HTTPS connections triggered by ImageUtils() will not be rejected even if certificate in invalid or channel can not be assured to be secure. Only use this in testing mode!
      Default: 0
    • Method Summary

      All Methods Static Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method Description
      static byte[] downloadImage​(java.lang.String sessionId, java.net.URI uri)
      Download an image from URL.
      static byte[] downloadImage​(java.net.URI uri)
      Download an image from URL.
      static java.awt.image.BufferedImage getBufferedImage​(byte[] bytes)
      This method creates a buffered image from a given byte[] using the BufferedImage.TYPE_3BYTE_BGR color model
      static java.awt.image.BufferedImage getBufferedImage​(byte[] bytes, int imageType)
      Creates a buffered image from the given byte[] using the color model provided.
      static java.util.Map<java.lang.String,​java.lang.Object> getImageInfoOfMediaObject​(MediaObject mediaObject, java.lang.String mediaObjectSourceOrder)
      Get image info a MediaObject, such as width, height, resolution etc.
      static java.util.Map<java.lang.String,​java.lang.Object> getMediaAssetImageInfo​(MediaAsset mediaAsset)
      Get image info of the first valid MediaObject of a MediaAsset, such as width, height, resolution etc.
      static java.util.Map<java.lang.String,​java.lang.Object> getMediaAssetImageInfo​(MediaAsset mediaAsset, java.lang.String mediaObjectSourceOrder)
      Get image info of the first valid MediaObject of a MediaAsset, such as width, height, resolution etc.
      static java.util.Map<java.lang.String,​java.lang.Object> getMediaAssetImageInfo​(MediaAsset mediaAsset, java.lang.String mediaObjectFilePath, java.lang.String mediaObjectSourceOrder)
      Get image info of the first valid MediaObject of a MediaAsset, such as width, height, resolution etc.
      static byte[] getPreviewOfMediaAsset​(MediaAsset mediaAsset, java.lang.String prefixForCache, int width, int height, boolean defaultToDummyImage)
      Get a preview a MediaAsset.
      static byte[] getPreviewOfMediaAsset​(MediaAsset mediaAsset, java.lang.String prefixForCache, int width, int height, boolean defaultToDummyImage, java.lang.String mediaObjectSourceOrder)
      Get a preview a MediaAsset.
      static byte[] getPreviewOfMediaAsset​(java.lang.String sessionId, MediaAsset mediaAsset, java.lang.String prefixForCache, int width, int height, boolean defaultToDummyImage)
      Get a preview a MediaAsset.
      static byte[] getPreviewOfMediaAsset​(java.lang.String sessionId, MediaAsset mediaAsset, java.lang.String prefixForCache, int width, int height, boolean defaultToDummyImage, java.lang.String mediaObjectSourceOrder)
      Get a preview a MediaAsset.
      static byte[] getPreviewOfMediaObject​(MediaObject mediaObject, int width, int height, java.lang.String mediaObjectSourceOrder)
      Get a preview a MediaObject.
      static byte[] getPreviewOfMediaObject​(java.lang.String sessionId, MediaObject mediaObject, int width, int height, java.lang.String mediaObjectSourceOrder)
      Get a preview a MediaObject.
      static byte[] readMediaObject​(MediaObject mediaObject, java.lang.String mediaObjectSourceOrder)
      Reads an image from file or from the network.
      static byte[] readMediaObject​(java.lang.String sessionId, MediaObject mediaObject, java.lang.String mediaObjectSourceOrder)
      Reads an image from file or from the network.
      static boolean saveBytes​(java.io.File file, byte[] bytes)
      Deprecated.
      please use Apache commons FileUtils for similar functionality.
      static byte[] scaleProportional​(byte[] src, int width, int height)  
      • Methods inherited from class java.lang.Object

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

      • IMAGE_HEIGHT_METADATA_NAME

        public static final java.lang.String IMAGE_HEIGHT_METADATA_NAME
        Metadata key for the image height property.
        See Also:
        Constant Field Values
      • IMAGE_WIDTH_METADATA_NAME

        public static final java.lang.String IMAGE_WIDTH_METADATA_NAME
        Metadata key for the image width property.
        See Also:
        Constant Field Values
      • IMAGE_RESOLUTION_METADATA_NAME

        public static final java.lang.String IMAGE_RESOLUTION_METADATA_NAME
        Metadata key for the image resolution property.
        See Also:
        Constant Field Values
      • IMAGE_COLORDEPTH_METADATA_NAME

        public static final java.lang.String IMAGE_COLORDEPTH_METADATA_NAME
        Metadata key for the image color depth property.
        See Also:
        Constant Field Values
      • HTTP_CONNECT_TIMEOUT_PROP

        public static final java.lang.String HTTP_CONNECT_TIMEOUT_PROP
        System property to overwrite the default connection timeout
      • HTTP_READ_TIMEOUT_PROP

        public static final java.lang.String HTTP_READ_TIMEOUT_PROP
        System property to overwrite the default read timeout
      • HTTPS_ALLOW_INSECURE_PROP

        public static final java.lang.String HTTPS_ALLOW_INSECURE_PROP
        System property to overwrite the default flag to handle secure connections.
      • HTTPS_CONTEXT_PROP

        public static final java.lang.String HTTPS_CONTEXT_PROP
        System property to overwrite the default SSL context used.
      • HTTP_CONNECT_ADD_USER_AGENT

        public static final java.lang.String HTTP_CONNECT_ADD_USER_AGENT
        System property to add the user agent to the request
    • Method Detail

      • getPreviewOfMediaAsset

        public static byte[] getPreviewOfMediaAsset​(MediaAsset mediaAsset,
                                                    java.lang.String prefixForCache,
                                                    int width,
                                                    int height,
                                                    boolean defaultToDummyImage)
        Get a preview a MediaAsset.

        This iterates the list of media objects and try to read the images from file or URL resource. The first resource that can be read and parsed into an image object will be returned.

        Previews will be cached if prefixForCache is set to a non-empty string.

        If no preview was found and defaultToDummyImage is true a dummy preview will be generated (but never cached).

        If there are authentication problems with this method, please use the variant of this method with sessionId parameter

        If there are authentication problems with this method, please use the variant of this method with sessionId parameter

        Parameters:
        mediaAsset - The MediaAsset to process.
        prefixForCache - A prefix for the key used for caching the preview.
        width - preview width (min. 100px)
        height - preview height (min. 100px)
        defaultToDummyImage - Generate dummy preview if no preview was found.
        Returns:
        preview of first valid MediaObject or dummy preview (makeEmptyImage=true) or null result (if not null) is in PNG format.
      • getPreviewOfMediaAsset

        public static byte[] getPreviewOfMediaAsset​(java.lang.String sessionId,
                                                    MediaAsset mediaAsset,
                                                    java.lang.String prefixForCache,
                                                    int width,
                                                    int height,
                                                    boolean defaultToDummyImage)
        Get a preview a MediaAsset.

        This iterates the list of media objects and try to read the images from file or URL resource. The first resource that can be read and parsed into an image object will be returned.

        Previews will be cached if prefixForCache is set to a non-empty string.

        If no preview was found and defaultToDummyImage is true a dummy preview will be generated (but never cached).

        Parameters:
        sessionId - current sessionId.
        mediaAsset - The MediaAsset to process.
        prefixForCache - A prefix for the key used for caching the preview.
        width - preview width (min. 100px)
        height - preview height (min. 100px)
        defaultToDummyImage - Generate dummy preview if no preview was found.
        Returns:
        preview of first valid MediaObject or dummy preview (makeEmptyImage=true) or null result (if not null) is in PNG format.
      • getPreviewOfMediaAsset

        public static byte[] getPreviewOfMediaAsset​(MediaAsset mediaAsset,
                                                    java.lang.String prefixForCache,
                                                    int width,
                                                    int height,
                                                    boolean defaultToDummyImage,
                                                    java.lang.String mediaObjectSourceOrder)
        Get a preview a MediaAsset.

        This iterates the list of media objects and try to read the images from file or URL resource. The first resource that can be read and parsed into an image object will be returned.

        Previews will be cached if prefixForCache is set to a non-empty string.

        If no preview was found and defaultToDummyImage is true a dummy preview will be generated (but never cached).

        If there are authentication problems with this method, please use the variant of this method with sessionId parameter

        Parameters:
        mediaAsset - The MediaAsset to process.
        prefixForCache - A prefix for the key used for caching the preview.
        width - preview width (min. 100px)
        height - preview height (min. 100px)
        defaultToDummyImage - Generate dummy preview if no preview was found.
        Returns:
        preview of first valid MediaObject or dummy preview (makeEmptyImage=true) or null result (if not null) is in PNG format.
      • getPreviewOfMediaAsset

        public static byte[] getPreviewOfMediaAsset​(java.lang.String sessionId,
                                                    MediaAsset mediaAsset,
                                                    java.lang.String prefixForCache,
                                                    int width,
                                                    int height,
                                                    boolean defaultToDummyImage,
                                                    java.lang.String mediaObjectSourceOrder)
        Get a preview a MediaAsset.

        This iterates the list of media objects and try to read the images from file or URL resource. The first resource that can be read and parsed into an image object will be returned.

        Previews will be cached if prefixForCache is set to a non-empty string.

        If no preview was found and defaultToDummyImage is true a dummy preview will be generated (but never cached).

        Parameters:
        sessionId - current sessionId.
        mediaAsset - The MediaAsset to process.
        prefixForCache - A prefix for the key used for caching the preview.
        width - preview width (min. 100px)
        height - preview height (min. 100px)
        defaultToDummyImage - Generate dummy preview if no preview was found.
        Returns:
        preview of first valid MediaObject or dummy preview (makeEmptyImage=true) or null result (if not null) is in PNG format.
      • getPreviewOfMediaObject

        public static byte[] getPreviewOfMediaObject​(MediaObject mediaObject,
                                                     int width,
                                                     int height,
                                                     java.lang.String mediaObjectSourceOrder)
        Get a preview a MediaObject.

        If there are authentication problems with this method, please use the variant of this method with sessionId parameter

        Parameters:
        mediaObject - The mediaObject to process.
        width - preview width (min. 100px)
        height - preview height (min. 100px)
        mediaObjectSourceOrder -
        Returns:
        preview of MediaObject or null
      • getPreviewOfMediaObject

        public static byte[] getPreviewOfMediaObject​(java.lang.String sessionId,
                                                     MediaObject mediaObject,
                                                     int width,
                                                     int height,
                                                     java.lang.String mediaObjectSourceOrder)
        Get a preview a MediaObject.
        Parameters:
        sessionId - The current sessionId
        mediaObject - The mediaObject to process.
        width - preview width (min. 100px)
        height - preview height (min. 100px)
        mediaObjectSourceOrder -
        Returns:
        preview of MediaObject or null
      • getMediaAssetImageInfo

        public static java.util.Map<java.lang.String,​java.lang.Object> getMediaAssetImageInfo​(MediaAsset mediaAsset,
                                                                                                    java.lang.String mediaObjectFilePath,
                                                                                                    java.lang.String mediaObjectSourceOrder)
        Get image info of the first valid MediaObject of a MediaAsset, such as width, height, resolution etc.
        Parameters:
        mediaAsset - the MediaAsset
        mediaObjectFilePath - an optional path, if set (i.e. non null) the information is read from this particular file rather than from just the first MediaObject found.
        mediaObjectSourceOrder - order for getting preview (by defualt preview is taken from URL, if failed then looks for path)
        Returns:
        certain image information - or null
      • getMediaAssetImageInfo

        public static java.util.Map<java.lang.String,​java.lang.Object> getMediaAssetImageInfo​(MediaAsset mediaAsset,
                                                                                                    java.lang.String mediaObjectSourceOrder)
        Get image info of the first valid MediaObject of a MediaAsset, such as width, height, resolution etc.
        Parameters:
        mediaAsset - the MediaAsset
        mediaObjectSourceOrder - order for getting preview (by defualt preview is taken from URL, if failed then looks for path)
        Returns:
        certain image information - or null
      • getMediaAssetImageInfo

        public static java.util.Map<java.lang.String,​java.lang.Object> getMediaAssetImageInfo​(MediaAsset mediaAsset)
        Get image info of the first valid MediaObject of a MediaAsset, such as width, height, resolution etc.
        Parameters:
        mediaAsset - the MediaAsset
        Returns:
        certain image information - or null
      • getImageInfoOfMediaObject

        public static java.util.Map<java.lang.String,​java.lang.Object> getImageInfoOfMediaObject​(MediaObject mediaObject,
                                                                                                       java.lang.String mediaObjectSourceOrder)
        Get image info a MediaObject, such as width, height, resolution etc.
        Parameters:
        mediaObject -
        mediaObjectSourceOrder - order for getting preview (by defualt preview is taken from URL, if failed then looks for path)
        Returns:
        certain image information - or null
      • readMediaObject

        public static byte[] readMediaObject​(MediaObject mediaObject,
                                             java.lang.String mediaObjectSourceOrder)
        Reads an image from file or from the network.

        If there are authentication problems with this method, please use the variant of this method with sessionId parameter

        Parameters:
        mediaObject -
        mediaObjectSourceOrder -
        Returns:
      • readMediaObject

        public static byte[] readMediaObject​(java.lang.String sessionId,
                                             MediaObject mediaObject,
                                             java.lang.String mediaObjectSourceOrder)
        Reads an image from file or from the network.
        Parameters:
        sessionId - current session identifier
        mediaObject -
        mediaObjectSourceOrder -
        Returns:
      • downloadImage

        public static byte[] downloadImage​(java.net.URI uri)
        Download an image from URL.
        Connect and read timeouts can be set via system properties: HTTP_CONNECT_TIMEOUT, HTTP_READ_TIMEOUT.

        If there are authentication problems with this method, please use the variant of this method with sessionId parameter

        Parameters:
        uri -
        Returns:
        the byte array - or an empty array if anything fails.
      • downloadImage

        public static byte[] downloadImage​(java.lang.String sessionId,
                                           java.net.URI uri)
        Download an image from URL.
        Connect and read timeouts can be set via system properties:
        Parameters:
        sessionId - current session identifier
        uri -
        Returns:
        the byte array - or an empty array if anything fails.
      • scaleProportional

        public static byte[] scaleProportional​(byte[] src,
                                               int width,
                                               int height)
        Parameters:
        src - image (bmp, png, jpeg, gif, tiff) as bytes
        width -
        height -
        Returns:
      • saveBytes

        @Deprecated
        public static boolean saveBytes​(java.io.File file,
                                        byte[] bytes)
        Deprecated.
        please use Apache commons FileUtils for similar functionality.
        Save byte array to a file
        Parameters:
        file -
        bytes -
        Returns:
        true if data was successfully saved to file
      • getBufferedImage

        public static java.awt.image.BufferedImage getBufferedImage​(byte[] bytes)
        This method creates a buffered image from a given byte[] using the BufferedImage.TYPE_3BYTE_BGR color model
        Parameters:
        bytes - bytes to be converted into BufferedImage
        Returns:
        BufferedImage
      • getBufferedImage

        public static java.awt.image.BufferedImage getBufferedImage​(byte[] bytes,
                                                                    int imageType)
        Creates a buffered image from the given byte[] using the color model provided.
        Parameters:
        bytes - bytes to be converted into BufferedImage
        imageType - color model, see BufferedImage
        Returns:
        BufferedImage