Class ImageUtil


  • public class ImageUtil
    extends Object
    Contains utility methods dealing with images like image size definition or creation of thumbnails (reduced copies of images).
    Since:
    4.0M1
    Version:
    $Id: 2b0c0e29d583c58caa42a22a0353693cbb8f7e3f $
    • Constructor Summary

      Constructors 
      Constructor Description
      ImageUtil()  
    • Constructor Detail

      • ImageUtil

        public ImageUtil()
    • Method Detail

      • createThumb

        public static void createThumb​(InputStream input,
                                       OutputStream output,
                                       int thumbWidth,
                                       int thumbHeight,
                                       String format)
                                throws IOException
        Create a reduced image (thumb) of an image from the given input stream. Possible output formats are "jpg" or "png" (no "gif"; it is possible to read "gif" files, but not to write). The resulting thumb is written to the output stream. The both streams will be explicitly closed by this method. This method keeps the ratio width/height of the initial image. If both dimensions of the initial image is less than the specified boundaries it is not re-scaled; it is written to the output stream as is.
        Parameters:
        input - the input stream containing the image to reduce
        output - the output stream where the resulting reduced image is written
        thumbWidth - the maximal width of the reduced image
        thumbHeight - the maximal height of the reduced image
        format - the output format of the reduced image; it can be "jpg", "png", ...
        Throws:
        IOException
      • getImageSize

        public static int[] getImageSize​(InputStream input)
                                  throws IOException
        Returns the size (width and height) of an image from the given input stream. This method closes the given stream.
        Parameters:
        input - the input stream with an image
        Returns:
        the size (width and height) of an image from the given input stream
        Throws:
        IOException
      • getImageSize

        public static int[] getImageSize​(InputStream input,
                                         int maxWidth,
                                         int maxHeight)
                                  throws IOException
        Returns the possible size of an image from the given input stream; the returned size does not overcome the specified maximal borders but keeps the ratio of the image. This method closes the given stream.
        Parameters:
        input - the input stream with an image
        maxWidth - the maximal width
        maxHeight - the maximal height
        Returns:
        the possible size of an image from the given input stream; the returned size does not overcome the specified maximal borders but keeps the ratio of the image
        Throws:
        IOException
      • getNewSize

        public static int[] getNewSize​(int width,
                                       int height,
                                       int maxWidth,
                                       int maxHeight)
        Calculates new size of an image with the specified max borders keeping the ratio between height and width of the image.
        Parameters:
        width - the initial width of an image
        height - the initial height of an image
        maxWidth - the maximal width of an image
        maxHeight - the maximal height of an image
        Returns:
        a new size of an image where the height and width don't overcome the specified borders; the size keeps the initial image ratio between width and height