public class Image extends Object implements ActionSource
Abstracts the underlying platform images allowing us to treat them as a uniform object.
Modifier | Constructor and Description |
---|---|
protected |
Image(Object image)
Subclasses may use this and point to an underlying native image which might be
null for a case of an image that doesn't use native drawing
|
Modifier and Type | Method and Description |
---|---|
void |
addActionListener(ActionListener l)
Adds ActionListener to receive action events form this source.
|
boolean |
animate() |
Image |
applyMask(Object mask)
Applies the given alpha mask onto this image and returns the resulting image
see the createMask method for indication on how to convert an image into an alpha
mask.
|
Image |
applyMask(Object mask,
int x,
int y)
Applies the given alpha mask onto this image and returns the resulting image
see the createMask method for indication on how to convert an image into an alpha
mask.
|
Image |
applyMaskAutoScale(Object mask)
Applies the given alpha mask onto this image and returns the resulting image
see the createMask method for indication on how to convert an image into an alpha
mask.
|
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.
|
static Image |
createImage(byte[] bytes,
int offset,
int len)
creates an image from a given byte array data
|
static Image |
createImage(InputStream stream)
creates an image from an InputStream
|
static Image |
createImage(int[] rgb,
int width,
int height)
creates an image from an RGB image
|
static Image |
createImage(int width,
int height)
Creates a white opaque mutable image that may be manipulated using
getGraphics() .The sample below shows this method being used to create a screenshot for sharing the image: |
static Image |
createImage(int width,
int height,
int fillColor)
Creates a mutable image that may be manipulated using
getGraphics() .The sample below shows this method being used to create a screenshot for sharing the image: |
static Image |
createImage(Object nativeImage)
Deprecated.
this method is deprecated as a warning! Don't use this method unless you actually
know what you are doing, if you are invoking this method without understanding the distinction
between native image and Codename One image then you are using the wrong method.
|
static Image |
createImage(String path)
Creates an image from a path.
|
static Image |
createIndexed(int width,
int height,
int[] palette,
byte[] data)
Deprecated.
try to avoid using indexed images explicitly
|
Object |
createMask()
Creates a mask from the given image, a mask can be used to apply an arbitrary
alpha channel to any image.
|
static Image |
createSVG(String baseURL,
boolean animated,
byte[] data)
Creates an SVG Image from the given byte array data and the base URL, this method
will throw an exception if SVG is unsupported.
|
void |
dispose()
Deprecated.
SERIOUSLY, DON'T INVOKE THIS METHOD UNLESS YOU KNOW WHAT YOU ARE DOING IT WILL CAUSE PLATFORM SPECIFC CRASHES OTHERWISE. IF YOU INVOKED THIS METHOD YOU ARE PROBABLY MAKING A MISTAKE
|
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.
|
static Image |
exifRotation(String capturedImage)
The main use case of this method is the automatic rotation and flipping
of an image returned from the camera or from the gallery, preserving the
original format (jpeg or png); it detects the Exif Orientation Tag, if
available (all the possible Exif Orientation Tag values are
supported); transparency is not preserved.
|
static Image |
exifRotation(String capturedImage,
String rotatedImage)
The main use case of this method is the automatic rotation and flipping
of an image returned from the camera or from the gallery, preserving the
original format (jpeg or png); it detects the Exif Orientation Tag, if
available (all the possible Exif Orientation Tag values are
supported); transparency is not preserved.
|
static Image |
exifRotation(String capturedImage,
String rotatedImage,
int maxSize)
The main use case of this method is the automatic rotation and flipping
of an image returned from the camera or from the gallery, preserving the
original format (jpeg or png); it detects the Exif Orientation Tag, if
available (all the possible Exif Orientation Tag values are
supported); transparency is not preserved.
|
Image |
fill(int width,
int height)
Resizes/crops the image so that its center fills the given dimensions.
|
void |
fireChangedEvent() |
Image |
flipHorizontally(boolean maintainOpacity)
Flips this image on the horizontal axis
|
Image |
flipVertically(boolean maintainOpacity)
Flips this image on the vertical axis
|
static int |
getExifOrientationTag(InputStream is)
Gets the EXIF orientation tag of an image, if it's available.
|
static int |
getExifOrientationTag(String path)
Gets the EXIF orientation tag of an image, if it's available.
|
Graphics |
getGraphics()
If this is a mutable image a graphics object allowing us to draw on it
is returned.
|
int |
getHeight()
Returns the height of the image
|
Object |
getImage()
Returns the platform specific image implementation, warning the
implementation class can change between revisions of Codename One and platforms.
|
String |
getImageName()
The name of the image is set for some images mostly to ease the debugging of Codename One application
|
int[] |
getRGB()
Returns the content of this image as a newly created ARGB array.
|
void |
getRGB(int[] rgbData)
Returns the content of this image in the supplied ARGB array.
|
int[] |
getRGBCached()
Returns the content of this image as a newly created ARGB array or a cached
instance if possible.
|
Object |
getSVGDocument()
Returns a platform specific DOM object that can be manipulated by the user
to change the SVG Image
|
int |
getWidth()
Returns the width of the image
|
static boolean |
isAlphaMutableImageSupported()
Returns true if mutable images support alpha transparency
|
boolean |
isAnimation()
Returns true if this is an animated image
|
static boolean |
isJPEG(InputStream inputStream)
Very fast method to detect if the given inputStream is a JPEG image
(according to its guessed mime type)
|
boolean |
isLocked()
Returns true if the image is locked
|
boolean |
isOpaque()
Indicates whether this image is opaque or not
|
static boolean |
isPNG(InputStream inputStream)
Very fast method to detect if the given inputStream is a PNG image
(according to its guessed mime type)
|
boolean |
isSVG()
Indicates if this image represents an SVG file or a bitmap file
|
static boolean |
isSVGSupported()
Indicates whether the underlying platform supports creating an SVG Image
|
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.
|
Image |
mirror()
Creates a mirror image for the given image which is useful for some RTL scenarios.
|
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.
|
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.
|
Image |
modifyAlphaWithTranslucency(byte alpha)
Creates a new image instance with the alpha channel of opaque
pixels within the image using the new alpha value.
|
void |
removeActionListener(ActionListener l)
Removes ActionListener so that it will no longer receive events
from this source.
|
boolean |
requiresDrawImage()
New label optimizations don't invoke drawImage and instead just pass the native image directly to
the underlying renderer.
|
Image |
rotate(int degrees)
Returns an instance of this image rotated by the given number of degrees.
|
Image |
rotate180Degrees(boolean maintainOpacity)
Rotates the image by 180 degrees
|
Image |
rotate270Degrees(boolean maintainOpacity)
Rotates the image by 270 degrees while changing the ratio of the picture
|
Image |
rotate90Degrees(boolean maintainOpacity)
Rotates this image by 90 degrees while changing the ratio of the picture
|
void |
scale(int width,
int height)
Deprecated.
scale should return an image rather than modify the image in place
use scaled(int, int) instead
|
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.
|
Image |
scaledHeight(int height)
Scales the image to the given height while updating the width based on the
aspect ratio of the height
|
Image |
scaledLargerRatio(int width,
int height)
Scales the image while maintaining the aspect ratio to the larger size
image
|
Image |
scaledSmallerRatio(int width,
int height)
Scales the image while maintaining the aspect ratio to the smaller size
image
|
Image |
scaledWidth(int width)
Scales the image to the given width while updating the height based on the
aspect ratio of the width
|
void |
setImageName(String imageName)
The name of the image is set for some images mostly to ease the debugging of Codename One application
|
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.
|
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
|
void |
unlock()
This callback indicates that a component pointing at this image is now deinitilized
This method may be invoked multiple times.
|
protected Image(Object image)
image
- native image object passed to the Codename One implementationpublic void asyncLock(Image internal)
internal
- the image to show while the actual image loads.public void lock()
public boolean isLocked()
public void unlock()
public static boolean isSVGSupported()
public Object getSVGDocument()
public static Image createSVG(String baseURL, boolean animated, byte[] data) throws IOException
baseURL
- URL which is used to resolve relative references within the SVG fileanimated
- indicates if the SVG features an animationdata
- the conten of the SVG fileIOException
- if resource lookup fail SVG is unsupportedpublic boolean isSVG()
public Object createMask()
Creates a mask from the given image, a mask can be used to apply an arbitrary
alpha channel to any image. A mask is derived from the blue channel (LSB) of
the given image, other channels are ignored.
The generated mask can be used with the apply mask method.
The sample below demonstrates the masking of an image based on a circle drawn on a mutable image:
public Image applyMask(Object mask, int x, int y)
Applies the given alpha mask onto this image and returns the resulting image see the createMask method for indication on how to convert an image into an alpha mask.
The sample below demonstrates the masking of an image based on a circle drawn on a mutable image:mask
- mask object created by the createMask() method.x
- starting x where to apply the masky
- starting y where to apply the maskpublic Image applyMask(Object mask)
mask
- mask object created by the createMask() method.IllegalArgumentException
- if the image size doesn't match the mask sizepublic Image applyMaskAutoScale(Object mask)
mask
- mask object created by the createMask() method.public Image subImage(int x, int y, int width, int height, boolean processAlpha)
x
- the x offset from the imagey
- the y offset from the imagewidth
- the width of internal imagesheight
- the height of internal imagesprocessAlpha
- whether alpha should be processed as well as part of the cuttingpublic Image mirror()
public Image rotate(int degrees)
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.
degrees
- A degree in right angle must be larger than 0 and up to 359 degreespublic static Image createIndexed(int width, int height, int[] palette, byte[] data)
width
- image widthheight
- image heightpalette
- the color palette to use with the byte datadata
- byte data containing palette offsets to map to ARGB colorspublic Image modifyAlpha(byte alpha)
alpha
- New value for the entire alpha channelpublic Image modifyAlphaWithTranslucency(byte alpha)
alpha
- New value for the entire alpha channelpublic Image modifyAlpha(byte alpha, int removeColor)
alpha
- New value for the entire alpha channelremoveColor
- pixels matching this color are made transparent (alpha channel ignored)public static Image createImage(String path) throws IOException
path
- If path begins with file: then the image will be loaded from FileSystemStorage. Otherwise
it will load from the jar resources.IOException
public static Image createImage(Object nativeImage)
nativeImage
- public static Image createImage(InputStream stream) throws IOException
stream
- a given InputStreamIOException
public static Image createImage(int[] rgb, int width, int height)
rgb
- the RGB image array datawidth
- the image widthheight
- the image heightpublic static Image createImage(int width, int height)
Creates a white opaque mutable image that may be manipulated using getGraphics()
.
The sample below shows this method being used to create a screenshot for sharing the image:
The sample below demonstrates the drawing of a mask image to create a round image effect
width
- the image widthheight
- the image heightpublic static boolean isAlphaMutableImageSupported()
public static Image createImage(int width, int height, int fillColor)
Creates a mutable image that may be manipulated using getGraphics()
.
The sample below shows this method being used to create a screenshot for sharing the image:
width
- the image widthheight
- the image heightfillColor
- the color with which the image should be initially filledpublic static Image createImage(byte[] bytes, int offset, int len)
bytes
- the array of image data in a supported image formatoffset
- the offset of the start of the data in the arraylen
- the length of the data in the arraypublic Graphics getGraphics()
public int getWidth()
public int getHeight()
protected void drawImage(Graphics g, Object nativeGraphics, int x, int y)
g
- the graphics objectnativeGraphics
- the underlying native graphics which might be essential for some image typesx
- the x coordinatey
- the y coordinateprotected void drawImage(Graphics g, Object nativeGraphics, int x, int y, int w, int h)
g
- the graphics objectnativeGraphics
- the underlying native graphics which might be essential for some image typesx
- the x coordinatey
- the y coordinatew
- the width to occupyh
- the height to occupypublic void toRGB(RGBImage image, int destX, int destY, int x, int y, int width, int height)
image
- RGBImage that would receive pixel datadestX
- x location within RGBImage into which the data will
be writtendestY
- y location within RGBImage into which the data will
be writtenx
- location within the source imagey
- location within the source imagewidth
- size of the image to extract from the source imageheight
- size of the image to extract from the source imagepublic int[] getRGB()
public void getRGB(int[] rgbData)
rgbData
- public int[] getRGBCached()
public Image scaledWidth(int width)
width
- the given new image widthpublic Image scaledHeight(int height)
height
- the given new image heightpublic Image scaledSmallerRatio(int width, int height)
width
- the given new image widthheight
- the given new image heightpublic Image scaledLargerRatio(int width, int height)
width
- the given new image widthheight
- the given new image heightpublic Image scaled(int width, int height)
width
- width for the scalingheight
- height of the scaled imagepublic Image fill(int width, int height)
Style.BACKGROUND_IMAGE_SCALED_FILL
width
- the width to fillheight
- the height to fillpublic Object getImage()
public void scale(int width, int height)
width
- width for the scalingheight
- height of the scaled imagepublic boolean isAnimation()
public boolean animate()
public boolean isOpaque()
public String getImageName()
public void setImageName(String imageName)
imageName
- the imageName to setpublic void dispose()
public Image rotate90Degrees(boolean maintainOpacity)
maintainOpacity
- whether the opacity in the image should be maintainedpublic Image rotate180Degrees(boolean maintainOpacity)
maintainOpacity
- whether the opacity in the image should be maintainedpublic Image rotate270Degrees(boolean maintainOpacity)
maintainOpacity
- whether the opacity in the image should be maintainedpublic Image flipHorizontally(boolean maintainOpacity)
maintainOpacity
- whether the opacity in the image should be maintainedpublic Image flipVertically(boolean maintainOpacity)
maintainOpacity
- whether the opacity in the image should be maintainedpublic boolean requiresDrawImage()
public void addActionListener(ActionListener l)
ActionSource
addActionListener
in interface ActionSource
public void removeActionListener(ActionListener l)
ActionSource
removeActionListener
in interface ActionSource
public void fireChangedEvent()
public static Image exifRotation(String capturedImage) throws IOException
The main use case of this method is the automatic rotation and flipping of an image returned from the camera or from the gallery, preserving the original format (jpeg or png); it detects the Exif Orientation Tag, if available (all the possible Exif Orientation Tag values are supported); transparency is not preserved.
If there is no rotation or flipping, the image is only copied or scaled
if necessary; if the capturedImage has a format different from jpeg and
png, it is copied as it is.
Note that this method doesn't rely on the
file extension, but on the mime type of the capturedImage, since some
devices don't give appropriate extension to images returned from the
gallery.
You can test all the possible orientation values downloading the images from the repository EXIF Orientation-flag example images
Code example:
capturedImage
- is the FileSystemStorage path of a captured photo,
usually inside a temporary directoryIOException
public static Image exifRotation(String capturedImage, String rotatedImage) throws IOException
The main use case of this method is the automatic rotation and flipping of an image returned from the camera or from the gallery, preserving the original format (jpeg or png); it detects the Exif Orientation Tag, if available (all the possible Exif Orientation Tag values are supported); transparency is not preserved.
If there is no rotation or flipping, the image is only copied or scaled
if necessary; if the capturedImage has a format different from jpeg and
png, it is copied as it is.
Note that this method doesn't rely on the
file extension, but on the mime type of the capturedImage, since some
devices don't give appropriate extension to images returned from the
gallery.
You can test all the possible orientation values downloading the images from the repository EXIF Orientation-flag example images
Code example:
capturedImage
- is the FileSystemStorage path of a captured photo,
usually inside a temporary directoryrotatedImage
- is the FileSystemStorage path in which the rotated
photo is stored, normally this should be inside the
FileSystemStorage.getAppHomePath(); it can be null if you don't want to
save the rotated image to the FileSystemStorage.IOException
public static Image exifRotation(String capturedImage, String rotatedImage, int maxSize) throws IOException
The main use case of this method is the automatic rotation and flipping of an image returned from the camera or from the gallery, preserving the original format (jpeg or png); it detects the Exif Orientation Tag, if available (all the possible Exif Orientation Tag values are supported); transparency is not preserved.
However, rotating and/or flipping an hi-res image is very inefficient, that's why you should consider to pass a maxSize value as small as possible: it makes this method working faster.
If there is no rotation or flipping, the image is only copied or scaled
if necessary; if the capturedImage has a format different from jpeg and
png, it is copied as it is.
Note that this method doesn't rely on the
file extension, but on the mime type of the capturedImage, since some
devices don't give appropriate extension to images returned from the
gallery.
You can test all the possible orientation values downloading the images from the repository EXIF Orientation-flag example images
Code example:
capturedImage
- is the FileSystemStorage path of a captured photo,
usually inside a temporary directoryrotatedImage
- is the FileSystemStorage path in which the rotated
photo is stored, normally this should be inside the
FileSystemStorage.getAppHomePath(); it can be null if you don't want to
save the rotated image to the FileSystemStorage.maxSize
- is the maximum value of the width and height of the
rotated images, that is scaled if necessary, keeping the ratio.IOException
public static int getExifOrientationTag(String path) throws IOException
Gets the EXIF orientation tag of an image, if it's available.
The Exif Orientation Tag is a number from 0 to 8, for the explanation of each value see the Exif Orientation Tag page
You can test all the possible orientation values downloading the images from the repository EXIF Orientation-flag example images
path
- FileSystemStorage pathIOException
public static int getExifOrientationTag(InputStream is)
Gets the EXIF orientation tag of an image, if it's available.
The Exif Orientation Tag is a number from 0 to 8, for the explanation of each value see the Exif Orientation Tag page
You can test all the possible orientation values downloading the images from the repository EXIF Orientation-flag example images
is
- public static boolean isJPEG(InputStream inputStream) throws IOException
inputStream
- IOException
public static boolean isPNG(InputStream inputStream) throws IOException
inputStream
- IOException