public class EncodedImage
ImplementsActionSource
Known subtypesFileEncodedImage, FileEncodedImageAsync, ReplaceableImage, StorageImage, StorageImageAsync, ComponentImage.EncodedWrapper, URLImage
EncodedImage is the workhorse of Codename One. Images returned from resource files are
EncodedImage and many API’s expect it.
EncodedImage is effectively an image that is “hidden” and extracted as needed to remove the
memory overhead associated with loaded image. When creating an EncodedImage only the PNG
(or JPEG etc.) is loaded to an array in RAM. Normally, such images are very small (relatively) so they can be
kept in memory without much overhead.
When image information is needed (pixels) the image is decoded into RAM and kept in a weak/sort
reference (see com.codename1.ui.Display#createSoftWeakRef(java.lang.Object)). This allows the
image to be cached for performance and allows the garbage collector to reclaim it when the memory becomes
scarce.
Since the fully decoded image can be pretty big (width X height X 4) the ability to store just the
encoded image can be pretty stark. E.g. A standard 50x100 image will take up 20,000 bytes of RAM for a
standard image but an EncodedImage can reduce that to 1kb-2kb of RAM.
When drawing an EncodedImage it checks the weak reference cache and if the image is cached then
it is shown otherwise the image is loaded the encoded image cache it then drawn.
EncodedImage is not final and can be derived to produce complex image fetching strategies
e.g. the com.codename1.ui.URLImage class that can dynamically download its content from the web.
EncodedImage can be instantiated via the create methods in the class. Pretty much any image
can be converted into an EncodedImage via the boolean)
method.
EncodedImage Locking
Naturally loading the image is more expensive so we want the images that are on the current form to remain in
cache (otherwise GC will thrash a lot). That’s where #lock() kicks in, when #lock() is active we
keep a hard reference to the actual native image so it won’t get GC’d. This significantly improves performance!
Internally this is invoked automatically for background images, icons etc. which results in a huge performance
boost. This makes sense since these images are currently showing and they will be in RAM anyway. However,
if you use a complex renderer or custom drawing UI you should #lock() your images where possible!
To verify that locking might be a problem you can launch the performance monitor tool (accessible from the simulator menu), if you get log messages that indicate that an unlocked image was drawn you might have a problem.
Constructors
protected EncodedImage(int width, int height) | Allows subclasses to create more advanced variations of this class that lazily store the data in an arbitrary location. |
Methods
public static EncodedImage createMulti(int[] dpis, byte[][] data) | Deprecated Creates an encoded image that acts as a multi-image, DO NOT USE THIS METHOD. |
public static EncodedImage createFromImage(Image i, boolean jpeg) | Converts an image to encoded image |
public static Image createFromRGB(int[] argb, int width, int height, boolean jpeg) | Tries to create an encoded image from RGB which is more efficient, however if this fails it falls back to regular RGB image. |
public static EncodedImage create(byte[] data) | Creates an image from the given byte array |
public static EncodedImage create(byte[] data, int width, int height, boolean opacity) | Creates an image from the given byte array with the variables set appropriately. |
public static EncodedImage create(InputStream i)
throws IOException | Creates an image from the input stream |
public static EncodedImage create(InputStream i, int size)
throws IOException | Creates an image from the input stream, this version of the method is somewhat faster than the version that doesn’t accept size |
public static EncodedImage create(String i)
throws IOException | Creates an image from the input stream |
protected void resetCache() | A subclass might choose to load asynchroniously and reset the cache when the image is ready. |
public void dispose() | Releases the decoded image cache and the encoded byte data backing this instance. |
public boolean isDisposed() | Returns true if dispose has been called on this image. |
public byte[] getImageData() | Returns the byte array data backing the image allowing the image to be stored and discarded completely from RAM. |
public Object getImage() | Returns the platform specific image implementation, warning the implementation class can change between revisions of Codename One and platforms. |
protected Image getInternal() | Returns the actual image represented by the encoded image, this image will be cached in a weak/soft reference internally. |
public boolean isLocked() | Returns true if the image is locked |
public void asyncLock(Image internal) | Async lock is the equivalent of a lock operation, however it uses the given image as the hard cache and performs the actual image loading asynchronously. |
public void lock() | This callback indicates that a component pointing at this image is initialized, this allows an image to make performance sensitive considerations e.g. an encoded image might choose to cache itself in RAM. |
public void unlock() | This callback indicates that a component pointing at this image is now deinitilized This method may be invoked multiple times. |
public Image subImage(int x, int y, int width, int height, boolean processAlpha) | Extracts a subimage from the given image allowing us to breakdown a single large image into multiple smaller images in RAM, this actually creates a standalone version of the image for use. |
public Image rotate(int degrees) | Returns an instance of this image rotated by the given number of degrees. |
public Image modifyAlpha(byte alpha) | Creates a new image instance with the alpha channel of opaque/translucent pixels within the image using the new alpha value. |
public Image modifyAlpha(byte alpha, int removeColor) | Creates a new image instance with the alpha channel of opaque/translucent pixels within the image using the new alpha value. |
public Graphics getGraphics() | If this is a mutable image a graphics object allowing us to draw on it is returned. |
public int getWidth() | Returns the width of the image |
public int getHeight() | Returns the height of the image |
protected void drawImage(Graphics g, Object nativeGraphics, int x, int y) | Callback invoked internally by Codename One to draw the image/frame onto the display. |
protected void drawImage(Graphics g, Object nativeGraphics, int x, int y, int w, int h) | Callback invoked internally by Codename One to draw the image/frame onto the display. |
public void toRGB(RGBImage image, int destX, int destY, int x, int y, int width, int height) | Extracts data from this image into the given RGBImage |
public Image scaledWidth(int width) | Scales the image to the given width while updating the height based on the aspect ratio of the width |
public Image scaledHeight(int height) | Scales the image to the given height while updating the width based on the aspect ratio of the height |
public Image scaledSmallerRatio(int width, int height) | Scales the image while maintaining the aspect ratio to the smaller size image |
public EncodedImage scaledEncoded(int width, int height) | Performs scaling using ImageIO to generate an encoded Image |
public Image scaled(int width, int height) | Returns a scaled version of this image image using the given width and height, this is a fast algorithm that preserves translucent information. |
public void scale(int width, int height) | Scale the image to the given width and height, this is a fast algorithm that preserves translucent information |
public boolean isAnimation() | Returns true if this is an animated image |
public boolean isOpaque() | Indicates whether this image is opaque or not |
Inherited methods
From Image
isSimdOptimizationsEnabled, setSimdOptimizationsEnabled, resetSimdOptimizationsEnabled, isSVGSupported, createSVG, createIndexed, createImage, createImage, createImage, createImage, createImage, isAlphaMutableImageSupported, createImage, createImage, exifRotation, exifRotation, exifRotation, getExifOrientationTag, getExifOrientationTag, isJPEG, isPNG, getSVGDocument, isSVG, createMask, applyMask, applyMask, applyMaskAutoScale, mirror, modifyAlphaWithTranslucency, getRGB, getRGB, getRGBCached, scaledLargerRatio, fill, animate, getImageName, setImageName, rotate90Degrees, rotate180Degrees, rotate270Degrees, flipHorizontally, flipVertically, requiresDrawImage, addActionListener, removeActionListener, fireChangedEvent
Constructor details
EncodedImage
protected EncodedImage(int width, int height)Parameters
widthint- -1 if unknown ideally the width/height should be known in advance
heightint- -1 if unknown ideally the width/height should be known in advance
Method details
createMulti
public static EncodedImage createMulti(int[] dpis, byte[][] data)Parameters
dpisint[]- device DPI’s
databyte[][]- the data matching each multi-image DPI
Returns
createFromImage
public static EncodedImage createFromImage(Image i, boolean jpeg)Parameters
iImage- image
jpegboolean- true to try and set jpeg, will do a best effort but this isn’t guaranteed
Returns
createFromRGB
public static Image createFromRGB(int[] argb, int width, int height, boolean jpeg)Parameters
argbint[]- an argb array
widthint- the width for the image
heightint- the height for the image
jpegboolean- uses jpeg format internally which is opaque and could be faster/smaller
Returns
create
public static EncodedImage create(byte[] data)Parameters
databyte[]- the data of the image
Returns
create
public static EncodedImage create(byte[] data, int width, int height, boolean opacity)Parameters
databyte[]- the data of the image
widthint- the width of the image
heightint- the height of the image
opacityboolean- true for an opaque image
Returns
create
public static EncodedImage create(InputStream i)
throws IOExceptionParameters
iInputStream- the input stream
Returns
Throws
java.io.IOException- if thrown by the input stream
create
public static EncodedImage create(InputStream i, int size)
throws IOExceptionParameters
iInputStream- the input stream
sizeint- the size of the stream
Returns
Throws
java.io.IOException- if thrown by the input stream
create
public static EncodedImage create(String i)
throws IOExceptionParameters
iString- the resource
Returns
Throws
java.io.IOException- if thrown by the input stream
resetCache
protected void resetCache()dispose
public void dispose()IllegalStateException. Intended for tight memory budgets where
the caller knows the image is no longer needed; see Image.dispose.isDisposed
public boolean isDisposed()dispose has been called on this image.getImageData
public byte[] getImageData()Returns
getImage
public Object getImage()Returns
getInternal
protected Image getInternal()Returns
isLocked
public boolean isLocked()Returns
asyncLock
public void asyncLock(Image internal)Parameters
internalImage- the image to show while the actual image loads.
lock
public void lock()unlock
public void unlock()subImage
public Image subImage(int x, int y, int width, int height, boolean processAlpha)Parameters
xint- the x offset from the image
yint- the y offset from the image
widthint- the width of internal images
heightint- the height of internal images
processAlphaboolean- whether alpha should be processed as well as part of the cutting
Returns
rotate
public Image rotate(int degrees)Returns an instance of this image rotated by the given number of degrees. By default 90 degree angle divisions are supported, anything else is implementation dependent. This method assumes a square image. Notice that it is inefficient in the current implementation to rotate to non-square angles,
E.g. rotating an image to 45, 90 and 135 degrees is inefficient. Use rotatate to 45, 90 and then rotate the 45 to another 90 degrees to achieve the same effect with less memory.
Parameters
degreesint- A degree in right angle must be larger than 0 and up to 359 degrees
Returns
modifyAlpha
public Image modifyAlpha(byte alpha)Parameters
alphabyte- New value for the entire alpha channel
Returns
modifyAlpha
public Image modifyAlpha(byte alpha, int removeColor)Parameters
alphabyte- New value for the entire alpha channel
removeColorint- pixels matching this color are made transparent (alpha channel ignored)
Returns
getGraphics
public Graphics getGraphics()Returns
getWidth
public int getWidth()Returns
getHeight
public int getHeight()Returns
drawImage
protected void drawImage(Graphics g, Object nativeGraphics, int x, int y)Parameters
gGraphics- the graphics object
nativeGraphicsObject- the underlying native graphics which might be essential for some image types
xint- the x coordinate
yint- the y coordinate
drawImage
protected void drawImage(Graphics g, Object nativeGraphics, int x, int y, int w, int h)Parameters
gGraphics- the graphics object
nativeGraphicsObject- the underlying native graphics which might be essential for some image types
xint- the x coordinate
yint- the y coordinate
wint- the width to occupy
hint- the height to occupy
toRGB
public void toRGB(RGBImage image, int destX, int destY, int x, int y, int width, int height)Parameters
imageRGBImage- RGBImage that would receive pixel data
destXint- x location within RGBImage into which the data will be written
destYint- y location within RGBImage into which the data will be written
xint- location within the source image
yint- location within the source image
widthint- size of the image to extract from the source image
heightint- size of the image to extract from the source image
scaledWidth
public Image scaledWidth(int width)Parameters
widthint- the given new image width
Returns
scaledHeight
public Image scaledHeight(int height)Parameters
heightint- the given new image height
Returns
scaledSmallerRatio
public Image scaledSmallerRatio(int width, int height)Parameters
widthint- the given new image width
heightint- the given new image height
Returns
scaledEncoded
public EncodedImage scaledEncoded(int width, int height)Parameters
widthint- the width of the image, -1 to scale based on height and preserve aspect ratio
heightint- the height of the image, -1 to scale based on width and preserve aspect ratio
Returns
scaled
public Image scaled(int width, int height)Parameters
widthint- width for the scaling
heightint- height of the scaled image
Returns
scale
public void scale(int width, int height)Parameters
widthint- width for the scaling
heightint- height of the scaled image
isAnimation
public boolean isAnimation()Returns
isOpaque
public boolean isOpaque()