public class ResourceImage extends Image implements Closeable
ResourceImage
represents an image which may require dynamic allocation in order to be created.
A resource image should be closed in order to free the dynamic allocation used to store the image pixels.
This class provides statics method in order to load an image from a resource or from a given input stream.
Modifier and Type | Class and Description |
---|---|
static class |
ResourceImage.OutputFormat
Enumerates the different formats in which a resource image can store pixel colors.
|
Modifier and Type | Method and Description |
---|---|
static boolean |
canLoadImage(String path)
Returns whether an image can be loaded from a resource.
|
void |
close()
Closes this image and its associated resources.
|
boolean |
isClosed()
Returns whether this image has been closed.
|
static ResourceImage |
loadImage(InputStream stream,
int size)
Loads an immutable image from an
InputStream . |
static ResourceImage |
loadImage(InputStream stream,
int size,
ResourceImage.OutputFormat format)
Loads an immutable image from an
InputStream . |
static ResourceImage |
loadImage(String path)
Loads an immutable image from a resource.
|
static ResourceImage |
loadImage(String path,
ResourceImage.OutputFormat format)
Loads an immutable image from a resource.
|
canGetImage, getHeight, getImage, getSNIContext, getWidth, isTransparent, readPixel, readPixels
public static boolean canLoadImage(String path)
This method may be used to know whether calling loadImage(String)
with the given path would
be successful or not.
path
- the resource path.true
if the image can be loaded, false
otherwise.MicroUIException
- if the resource path doesn't start with "/".MicroUIException
- if MicroUI is not started.SecurityException
- if a security manager exists and does not allow the caller to load an image.loadImage(String)
public void close()
Calling this method on an image which has already been closed has no effect.
close
in interface Closeable
close
in interface AutoCloseable
public boolean isClosed()
true
if the image has been closed, false
otherwise.public static ResourceImage loadImage(InputStream stream, int size)
InputStream
.
Refer to loadImage(String)
for more information.
Calling this method is equivalent to calling loadImage(stream, size, null)
.
stream
- the input stream providing the image data.size
- the number of bytes to read from the input stream.MicroUIException
- if the image could not be loaded for any reason (see MicroUIException.getErrorCode()
).MicroUIException
- if MicroUI is not started.SecurityException
- if a security manager exists and does not allow the caller to load an image.public static ResourceImage loadImage(InputStream stream, int size, @Nullable ResourceImage.OutputFormat format)
InputStream
.
Refer to loadImage(String, OutputFormat)
for more information.
stream
- the input stream providing the image data.size
- the number of bytes to read from the input stream.format
- the desired output format, or null
to use the default output format.MicroUIException
- if the image could not be loaded for any reason (see MicroUIException.getErrorCode()
).MicroUIException
- if MicroUI is not started.SecurityException
- if a security manager exists and does not allow the caller to load an image.public static ResourceImage loadImage(String path)
This method can load images which are in the internal image format defined by the MicroUI implementation, and images which are in standard image formats (such as PNG), depending on the capabilities of the MicroUI implementation (such as having a PNG decoder).
The MicroUI implementation is responsible for retrieving the correct image load/decoder in order to load the image.
The output format of the returned image is the best format for the input format. For example, the output format
for PNG images is ResourceImage.OutputFormat.ARGB8888
. The output format for images in internal MicroUI image format is
the output format specified in the images.list
file.
Calling this method is equivalent to calling loadImage(name, null)
.
path
- the resource path.MicroUIException
- if the image could not be loaded for any reason (see MicroUIException.getErrorCode()
).MicroUIException
- if the resource path doesn't start with "/".MicroUIException
- if MicroUI is not started.SecurityException
- if a security manager exists and does not allow the caller to load an image.public static ResourceImage loadImage(String path, @Nullable ResourceImage.OutputFormat format)
This method can load images which are in the internal image format defined by the MicroUI implementation, and images which are in standard image formats (such as PNG), depending on the capabilities of the MicroUI implementation (such as having a PNG decoder).
The MicroUI implementation is responsible for retrieving the correct image load/decoder in order to load the image.
The output format of the image is the output format given as parameter. This format may be different from the
default output format (@see loadImage(String)
).
path
- the resource path.format
- the desired output format, or null
to use the default output format.MicroUIException
- if the image could not be loaded for any reason (see MicroUIException.getErrorCode()
).MicroUIException
- if the resource path doesn't start with "/".MicroUIException
- if MicroUI is not started.SecurityException
- if a security manager exists and does not allow the caller to load an image.