use of com.codename1.ui.util.ImageIO in project CodenameOne by codenameone.
the class EncodedImage method createFromRGB.
/**
* Tries to create an encoded image from RGB which is more efficient,
* however if this fails it falls back to regular RGB image. This method
* is slower than creating an RGB image (not to be confused with the RGBImage class which is
* something ENTIRELY different!).
*
* @param argb an argb array
* @param width the width for the image
* @param height the height for the image
* @param jpeg uses jpeg format internally which is opaque and could be faster/smaller
* @return an image which we hope is an encoded image
*/
public static Image createFromRGB(int[] argb, int width, int height, boolean jpeg) {
Image i = Image.createImage(argb, width, height);
ImageIO io = ImageIO.getImageIO();
if (io != null) {
String format;
if (jpeg) {
if (!io.isFormatSupported(ImageIO.FORMAT_JPEG)) {
return i;
}
format = ImageIO.FORMAT_JPEG;
} else {
if (!io.isFormatSupported(ImageIO.FORMAT_PNG)) {
return i;
}
format = ImageIO.FORMAT_PNG;
}
try {
ByteArrayOutputStream bo = new ByteArrayOutputStream();
io.save(i, bo, format, 0.9f);
EncodedImage enc = EncodedImage.create(bo.toByteArray());
Util.cleanup(bo);
enc.width = width;
enc.height = height;
if (jpeg) {
enc.opaque = true;
enc.opaqueChecked = true;
}
enc.cache = Display.getInstance().createSoftWeakRef(i);
return enc;
} catch (IOException err) {
Log.e(err);
}
}
return i;
}
use of com.codename1.ui.util.ImageIO in project CodenameOne by codenameone.
the class EncodedImage method createFromImage.
/**
* Converts an image to encoded image
* @param i image
* @param jpeg true to try and set jpeg, will do a best effort but this isn't guaranteed
* @return an encoded image or null
*/
public static EncodedImage createFromImage(Image i, boolean jpeg) {
if (i instanceof EncodedImage) {
return ((EncodedImage) i);
}
ImageIO io = ImageIO.getImageIO();
if (io != null) {
String format;
if (jpeg) {
if (!io.isFormatSupported(ImageIO.FORMAT_JPEG)) {
format = ImageIO.FORMAT_PNG;
} else {
format = ImageIO.FORMAT_JPEG;
}
} else {
if (!io.isFormatSupported(ImageIO.FORMAT_PNG)) {
format = ImageIO.FORMAT_JPEG;
} else {
format = ImageIO.FORMAT_PNG;
}
}
try {
ByteArrayOutputStream bo = new ByteArrayOutputStream();
io.save(i, bo, format, 0.9f);
EncodedImage enc = EncodedImage.create(bo.toByteArray());
Util.cleanup(bo);
enc.width = i.getWidth();
enc.height = i.getHeight();
if (format == ImageIO.FORMAT_JPEG) {
enc.opaque = true;
enc.opaqueChecked = true;
}
enc.cache = Display.getInstance().createSoftWeakRef(i);
return enc;
} catch (IOException err) {
Log.e(err);
}
}
return null;
}
Aggregations