Class ImageIO

java.lang.Object
com.codename1.ui.util.ImageIO

public abstract class ImageIO extends Object
Enable simple operations on image file & image objects such as dynamic scaling and storage to binary formats such as JPEG. Use Display.getImageIO() to get an instance of this class.
  • Field Details

  • Constructor Details

    • ImageIO

      public ImageIO()
  • Method Details

    • getImageIO

      public static ImageIO getImageIO()

      Gets the ImageIO instance

      Returns
      Returns:
      the image IO instance or null if image IO isn't supported for the given platform
    • save

      public abstract void save(InputStream image, OutputStream response, String format, int width, int height, float quality) throws IOException

      Saves an image file at the given resolution, scaling if necessary

      Parameters
      • image: source image stream

      • response: resulting image output will be written to this stream

      • format: the format for the image either FORMAT_PNG or FORMAT_JPEG

      • width: the width for the resulting image, use -1 to not scale

      • height: the height of the resulting image, use -1 to not scale

      • quality: @param quality the quality for the resulting image output (applicable mostly for JPEG), a value between 0 and 1 notice that this isn't implemented in all platforms.

      Throws:
      IOException
    • save

      public void save(Image img, OutputStream response, String format, float quality) throws IOException

      Saves an image object to the given format

      Parameters
      • img: the image object

      • response: resulting image output will be written to this stream

      • format: the format for the image either FORMAT_PNG or FORMAT_JPEG

      • quality: @param quality the quality for the resulting image output (applicable mostly for JPEG), a value between 0 and 1 notice that this isn't implemented in all platforms.

      Throws:
      IOException
    • save

      public void save(String imageFilePath, OutputStream response, String format, int width, int height, float quality) throws IOException

      Saves an image file at the given resolution, scaling if necessary

      Parameters
      • imageFilePath: the image file path

      • response: resulting image output will be written to this stream

      • format: the format for the image either FORMAT_PNG or FORMAT_JPEG

      • width: the width for the resulting image, use -1 to not scale

      • height: the height of the resulting image, use -1 to not scale

      • quality: @param quality the quality for the resulting image output (applicable mostly for JPEG), a value between 0 and 1 notice that this isn't implemented in all platforms.

      Throws:
      IOException
    • getImageSize

      public Dimension getImageSize(String imageFilePath) throws IOException

      Returns the image size in pixels

      Parameters
      • imageFilePath: the path to the image
      Returns

      the size in pixels

      Throws:
      IOException
    • saveAndKeepAspect

      public String saveAndKeepAspect(String imageFilePath, String preferredOutputPath, String format, int width, int height, float quality, boolean onlyDownscale, boolean scaleToFill) throws IOException

      Scales an image on disk while maintaining an aspect ratio, the appropriate aspect size will be picked based on the status of scaleToFill

      Parameters
      • imageFilePath: the path to the image

      • preferredOutputPath: the url where the image will be saved

      • format: the format for the image either FORMAT_JPEG or FORMAT_PNG

      • width: the desired width, either width or height will be respected based on aspect dimensions

      • height: the desired height, either width or height will be respected based on aspect dimensions

      • quality: @param quality the quality for the resulting image output (applicable mostly for JPEG), a value between 0 and 1 notice that this isn't implemented in all platforms.

      • onlyDownscale: will not scale if the resolution to scale will be higher in this case will return the imageFilePath

      • scaleToFill: @param scaleToFill when set to true will pick the larger value so the resulting image will be at least as big as width x height, when set to false will create an image that is no bigger than width x height

      Returns

      the url for the scaled image or the url of the unscaled image

      Throws
      • IOException: if the operation fails
      Throws:
      IOException
    • saveImage

      protected abstract void saveImage(Image img, OutputStream response, String format, float quality) throws IOException

      Saves an image object to the given format

      Parameters
      • img: the image object

      • response: resulting image output will be written to this stream

      • format: the format for the image either FORMAT_PNG or FORMAT_JPEG

      • quality: @param quality the quality for the resulting image output (applicable mostly for JPEG), a value between 0 and 1 notice that this isn't implemented in all platforms.

      Throws:
      IOException
    • isFormatSupported

      public abstract boolean isFormatSupported(String format)

      Indicates if the given format for output is supported by this implementation

      Parameters
      • format: the format for the image either FORMAT_PNG or FORMAT_JPEG
      Returns

      true if supported