public abstract class ImageIO

  1. Object
  2. ImageIO
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.

Fields

public static final String FORMAT_JPEG = "jpeg"Indicates the JPEG output format
public static final String FORMAT_PNG = "png"Indicates the PNG output format

Constructors

public ImageIO()

Methods

public static ImageIO getImageIO()Gets the ImageIO instance
public abstract void save(InputStream image, OutputStream response, String format, int width, int height, float quality) throws IOExceptionSaves an image file at the given resolution, scaling if necessary
public void save(Image img, OutputStream response, String format, float quality) throws IOExceptionSaves an image object to the given format
public void save(String imageFilePath, OutputStream response, String format, int width, int height, float quality) throws IOExceptionSaves an image file at the given resolution, scaling if necessary
public Dimension getImageSize(String imageFilePath) throws IOExceptionReturns the image size in pixels
public String saveAndKeepAspect(String imageFilePath, String preferredOutputPath, String format, int width, int height, float quality, boolean onlyDownscale, boolean scaleToFill) throws IOExceptionScales 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) throws IOExceptionSaves an image object to the given format
public abstract boolean isFormatSupported(String format)Indicates if the given format for output is supported by this implementation

Inherited methods

Field details

FORMAT_JPEG

public static final String FORMAT_JPEG = "jpeg"
Indicates the JPEG output format

FORMAT_PNG

public static final String FORMAT_PNG = "png"
Indicates the PNG output format

Constructor details

ImageIO

public ImageIO()

Method details

getImageIO

public static ImageIO getImageIO()
Gets the ImageIO instance

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 InputStream
source image stream
response OutputStream
resulting image output will be written to this stream
format String
the format for the image either FORMAT_PNG or FORMAT_JPEG
width int
the width for the resulting image, use -1 to not scale
height int
the height of the resulting image, use -1 to not scale
quality float
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.

save

public void save(Image img, OutputStream response, String format, float quality) throws IOException
Saves an image object to the given format

Parameters

img Image
the image object
response OutputStream
resulting image output will be written to this stream
format String
the format for the image either FORMAT_PNG or FORMAT_JPEG
quality float
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.

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 String
the image file path
response OutputStream
resulting image output will be written to this stream
format String
the format for the image either FORMAT_PNG or FORMAT_JPEG
width int
the width for the resulting image, use -1 to not scale
height int
the height of the resulting image, use -1 to not scale
quality float
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.

getImageSize

public Dimension getImageSize(String imageFilePath) throws IOException
Returns the image size in pixels

Parameters

imageFilePath String
the path to the image

Returns

the size in pixels

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 String
the path to the image
preferredOutputPath String
the url where the image will be saved
format String
the format for the image either FORMAT_JPEG or FORMAT_PNG
width int
the desired width, either width or height will be respected based on aspect dimensions
height int
the desired height, either width or height will be respected based on aspect dimensions
quality float
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 boolean
will not scale if the resolution to scale will be higher in this case will return the imageFilePath
scaleToFill boolean
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

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 Image
the image object
response OutputStream
resulting image output will be written to this stream
format String
the format for the image either FORMAT_PNG or FORMAT_JPEG
quality float
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.

isFormatSupported

public abstract boolean isFormatSupported(String format)
Indicates if the given format for output is supported by this implementation

Parameters

format String
the format for the image either FORMAT_PNG or FORMAT_JPEG

Returns

true if supported