# 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 Summary

  Fields
  Modifier and Type
  Field
  Description
  `static final String`
  `FORMAT_JPEG`
  Indicates the JPEG output format
  `static final String`
  `FORMAT_PNG`
  Indicates the PNG output format

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `ImageIO()`

- ## Method Summary

  Modifier and Type
  Method
  Description
  `static ImageIO`
  `getImageIO()`
  Gets the ImageIO instance
  `Dimension`
  `getImageSize(String imageFilePath)`
  Returns the image size in pixels
  `abstract boolean`
  `isFormatSupported(String format)`
  Indicates if the given format for output is supported by this implementation
  `void`
  `save(Image img, OutputStream response, String format, float quality)`
  Saves an image object to the given format
  `abstract void`
  `save(InputStream image, OutputStream response, String format, int width, int height, float quality)`
  Saves an image file at the given resolution, scaling if necessary
  `void`
  `save(String imageFilePath, OutputStream response, String format, int width, int height, float quality)`
  Saves an image file at the given resolution, scaling if necessary
  `String`
  `saveAndKeepAspect(String imageFilePath, String preferredOutputPath, String format, int width, int height, float quality, boolean onlyDownscale, boolean scaleToFill)`
  Scales an image on disk while maintaining an aspect ratio, the appropriate aspect size will be picked based on the status of scaleToFill
  `protected abstract void`
  `saveImage(Image img, OutputStream response, String format, float quality)`
  Saves an image object to the given format

  ### Methods inherited from class Object

  `clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`
  Modifier and Type
  Method
  Description
  `protected Object`
  `clone()`
  `boolean`
  `equals(Object obj)`
  Indicates whether some other object is "equal to" this one.
  `final Class`
  `getClass()`
  Returns the runtime class of an object.
  `int`
  `hashCode()`
  Returns a hash code value for the object.
  `final void`
  `notify()`
  Wakes up a single thread that is waiting on this object's monitor.
  `final void`
  `notifyAll()`
  Wakes up all threads that are waiting on this object's monitor.
  `String`
  `toString()`
  Returns a string representation of the object.
  `final void`
  `wait()`
  Causes current thread to wait until another thread invokes the method or the method for this object.
  `final void`
  `wait(long timeout)`
  Causes current thread to wait until either another thread invokes the method or the method for this object, or a specified amount of time has elapsed.
  `final void`
  `wait(long timeout, int nanos)`
  Causes current thread to wait until another thread invokes the method or the method for this object, or some other thread interrupts the current thread, or a certain amount of real time has elapsed.

- ## Field Details

  - ### FORMAT_JPEG

    public static final String FORMAT_JPEG
    Indicates the JPEG output format
    See Also:
      - Constant Field Values

  - ### FORMAT_PNG

    public static final String FORMAT_PNG
    Indicates the PNG output format
    See Also:
      - Constant Field Values

- ## 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