Search in sources :

Example 6 with ImageIO

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;
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ImageIO(com.codename1.ui.util.ImageIO)

Example 7 with ImageIO

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;
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ImageIO(com.codename1.ui.util.ImageIO)

Aggregations

ImageIO (com.codename1.ui.util.ImageIO)7 IOException (java.io.IOException)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 Image (com.codename1.ui.Image)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 FileTree (com.codename1.components.FileTree)1 FileTreeModel (com.codename1.components.FileTreeModel)1 ReplaceableImage (com.codename1.components.ReplaceableImage)1 ConnectionRequest (com.codename1.io.ConnectionRequest)1 EncodedImage (com.codename1.ui.EncodedImage)1 ActionEvent (com.codename1.ui.events.ActionEvent)1 ActionListener (com.codename1.ui.events.ActionListener)1 BorderLayout (com.codename1.ui.layouts.BorderLayout)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 Hashtable (java.util.Hashtable)1