Search in sources :

Example 21 with EncodedImage

use of com.codename1.ui.EncodedImage in project CodenameOne by codenameone.

the class PulsateEditor method pulsateWizard.

public void pulsateWizard(EditableResources res, JComponent parent) {
    File[] f = ResourceEditorView.showOpenFileChooser("Image", ".png", ".jpg", ".jpeg", ".gif");
    if (f != null && f.length > 0) {
        try {
            timelineName.setText(f[0].getName());
            sourceImage = ImageIO.read(f[0]);
            updateTimeline();
            timer = new javax.swing.Timer(130, new java.awt.event.ActionListener() {

                public void actionPerformed(ActionEvent e) {
                    if (previewLabel.animate()) {
                        previewLabel.repaint();
                        preview.repaint();
                    }
                }
            });
            timer.setRepeats(true);
            timer.setCoalesce(true);
            timer.start();
            int val = JOptionPane.showConfirmDialog(parent, this, "Edit Effect", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
            timer.stop();
            if (val == JOptionPane.OK_OPTION) {
                store(res, currentImage, timelineName.getText() + ": TL ");
                for (EncodedImage img : internalImages) {
                    store(res, img, timelineName.getText() + ": Fr ");
                }
            }
        } catch (IOException ex) {
            ex.printStackTrace();
            JOptionPane.showMessageDialog(parent, "Error in reading image file", "File Read Error", JOptionPane.ERROR_MESSAGE);
        }
    }
}
Also used : ActionEvent(java.awt.event.ActionEvent) IOException(java.io.IOException) File(java.io.File) EncodedImage(com.codename1.ui.EncodedImage)

Example 22 with EncodedImage

use of com.codename1.ui.EncodedImage in project CodenameOne by codenameone.

the class ImageDownloadService method readResponse.

/**
 * {@inheritDoc}
 */
protected void readResponse(InputStream input) throws IOException {
    int imageScaleWidth = -1, imageScaleHeight = -1;
    if (fastScale) {
        if (toScale != null) {
            imageScaleWidth = toScale.getWidth();
            imageScaleHeight = toScale.getHeight();
        } else {
            if (placeholder != null) {
                imageScaleWidth = placeholder.getWidth();
                imageScaleHeight = placeholder.getHeight();
            }
        }
    }
    if (cacheImages) {
        if (destinationFile != null) {
            result = FileEncodedImage.create(destinationFile, input, imageScaleWidth, imageScaleHeight);
        } else {
            EncodedImage e = EncodedImage.create(input);
            if (maintainAspectRatio) {
                float actualW = e.getWidth();
                float actualH = e.getHeight();
                float r2 = Math.min(((float) imageScaleWidth) / actualW, ((float) imageScaleHeight) / actualH);
                imageScaleWidth = (int) (actualW * r2);
                imageScaleHeight = (int) (actualH * r2);
            }
            // workaround for http://stackoverflow.com/questions/35347353/label-image-scale-issue-in-codename-one-library-3-3/35354605
            if (imageScaleWidth > -1 || imageScaleHeight > -1) {
                e = e.scaledEncoded(imageScaleWidth, imageScaleHeight);
            }
            result = StorageImage.create(cacheId, e.getImageData(), imageScaleWidth, imageScaleHeight, keep);
            // if the storage has failed create the image from the stream
            if (result == null) {
                result = e;
            }
        }
    } else {
        result = EncodedImage.create(input);
    }
}
Also used : EncodedImage(com.codename1.ui.EncodedImage) FileEncodedImage(com.codename1.components.FileEncodedImage)

Example 23 with EncodedImage

use of com.codename1.ui.EncodedImage in project CodenameOne by codenameone.

the class Util method writeObject.

/**
 * <p>Writes an object to the given output stream, notice that it should be externalizable or one of
 * the supported types.</p>
 *
 * <p>
 * The sample below demonstrates the usage and registration of the {@link com.codename1.io.Externalizable} interface:
 * </p>
 * <script src="https://gist.github.com/codenameone/858d8634e3cf1a82a1eb.js"></script>
 *
 * @param o the object to write which can be null
 * @param out the destination output stream
 * @throws IOException thrown by the stream
 */
public static void writeObject(Object o, DataOutputStream out) throws IOException {
    if (o == null) {
        out.writeBoolean(false);
        return;
    }
    out.writeBoolean(true);
    if (o instanceof Externalizable) {
        Externalizable e = (Externalizable) o;
        out.writeUTF(e.getObjectId());
        out.writeInt(e.getVersion());
        e.externalize(out);
        return;
    }
    if (o instanceof PropertyBusinessObject) {
        Externalizable e = ((PropertyBusinessObject) o).getPropertyIndex().asExternalizable();
        out.writeUTF(e.getObjectId());
        out.writeInt(e.getVersion());
        e.externalize(out);
        return;
    }
    if (o instanceof Vector) {
        Vector v = (Vector) o;
        out.writeUTF("java.util.Vector");
        int size = v.size();
        out.writeInt(size);
        for (int iter = 0; iter < size; iter++) {
            writeObject(v.elementAt(iter), out);
        }
        return;
    }
    if (o instanceof Collection) {
        Collection v = (Collection) o;
        out.writeUTF("java.util.Collection");
        int size = v.size();
        out.writeInt(size);
        for (Object cur : v) {
            writeObject(cur, out);
        }
        return;
    }
    if (o instanceof Hashtable) {
        Hashtable v = (Hashtable) o;
        out.writeUTF("java.util.Hashtable");
        out.writeInt(v.size());
        Enumeration k = v.keys();
        while (k.hasMoreElements()) {
            Object key = k.nextElement();
            writeObject(key, out);
            writeObject(v.get(key), out);
        }
        return;
    }
    if (o instanceof Map) {
        Map v = (Map) o;
        out.writeUTF("java.util.Map");
        out.writeInt(v.size());
        for (Object key : v.keySet()) {
            writeObject(key, out);
            writeObject(v.get(key), out);
        }
        return;
    }
    if (o instanceof String) {
        String v = (String) o;
        out.writeUTF("String");
        out.writeUTF(v);
        return;
    }
    if (o instanceof Date) {
        Date v = (Date) o;
        out.writeUTF("Date");
        out.writeLong(v.getTime());
        return;
    }
    if (o instanceof Integer) {
        Integer v = (Integer) o;
        out.writeUTF("int");
        out.writeInt(v.intValue());
        return;
    }
    if (o instanceof Long) {
        Long v = (Long) o;
        out.writeUTF("long");
        out.writeLong(v.longValue());
        return;
    }
    if (o instanceof Byte) {
        Byte v = (Byte) o;
        out.writeUTF("byte");
        out.writeByte(v.byteValue());
        return;
    }
    if (o instanceof Short) {
        Short v = (Short) o;
        out.writeUTF("short");
        out.writeShort(v.shortValue());
        return;
    }
    if (o instanceof Float) {
        Float v = (Float) o;
        out.writeUTF("float");
        out.writeFloat(v.floatValue());
        return;
    }
    if (o instanceof Double) {
        Double v = (Double) o;
        out.writeUTF("double");
        out.writeDouble(v.doubleValue());
        return;
    }
    if (o instanceof Boolean) {
        Boolean v = (Boolean) o;
        out.writeUTF("bool");
        out.writeBoolean(v.booleanValue());
        return;
    }
    if (o instanceof EncodedImage) {
        out.writeUTF("EncodedImage");
        EncodedImage e = (EncodedImage) o;
        out.writeInt(e.getWidth());
        out.writeInt(e.getHeight());
        out.writeBoolean(e.isOpaque());
        byte[] b = e.getImageData();
        out.writeInt(b.length);
        out.write(b);
        return;
    }
    if (instanceofObjArray(o)) {
        Object[] v = (Object[]) o;
        out.writeUTF("ObjectArray");
        int size = v.length;
        out.writeInt(size);
        for (int iter = 0; iter < size; iter++) {
            writeObject(v[iter], out);
        }
        return;
    }
    if (instanceofByteArray(o)) {
        byte[] v = (byte[]) o;
        out.writeUTF("ByteArray");
        int size = v.length;
        out.writeInt(size);
        out.write(v);
        return;
    }
    if (instanceofShortArray(o)) {
        short[] v = (short[]) o;
        out.writeUTF("ShortArray");
        int size = v.length;
        out.writeInt(size);
        for (int iter = 0; iter < size; iter++) {
            out.writeShort(v[iter]);
        }
        return;
    }
    if (instanceofDoubleArray(o)) {
        double[] v = (double[]) o;
        out.writeUTF("DoubleArray");
        int size = v.length;
        out.writeInt(size);
        for (int iter = 0; iter < size; iter++) {
            out.writeDouble(v[iter]);
        }
        return;
    }
    if (instanceofFloatArray(o)) {
        float[] v = (float[]) o;
        out.writeUTF("FloatArray");
        int size = v.length;
        out.writeInt(size);
        for (int iter = 0; iter < size; iter++) {
            out.writeFloat(v[iter]);
        }
        return;
    }
    if (instanceofIntArray(o)) {
        int[] v = (int[]) o;
        out.writeUTF("IntArray");
        int size = v.length;
        out.writeInt(size);
        for (int iter = 0; iter < size; iter++) {
            out.writeInt(v[iter]);
        }
        return;
    }
    if (instanceofLongArray(o)) {
        long[] v = (long[]) o;
        out.writeUTF("LongArray");
        int size = v.length;
        out.writeInt(size);
        for (int iter = 0; iter < size; iter++) {
            out.writeLong(v[iter]);
        }
        return;
    }
    throw new IOException("Object type not supported: " + o.getClass().getName() + " value: " + o);
}
Also used : Vector(java.util.Vector) Enumeration(java.util.Enumeration) PropertyBusinessObject(com.codename1.properties.PropertyBusinessObject) Hashtable(java.util.Hashtable) IOException(java.io.IOException) EncodedImage(com.codename1.ui.EncodedImage) Date(java.util.Date) Collection(java.util.Collection) Externalizable(com.codename1.io.Externalizable) PropertyBusinessObject(com.codename1.properties.PropertyBusinessObject) HashMap(java.util.HashMap) Map(java.util.Map)

Example 24 with EncodedImage

use of com.codename1.ui.EncodedImage 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 25 with EncodedImage

use of com.codename1.ui.EncodedImage 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

EncodedImage (com.codename1.ui.EncodedImage)21 IOException (java.io.IOException)14 ByteArrayOutputStream (java.io.ByteArrayOutputStream)7 Image (com.codename1.ui.Image)6 AnimationObject (com.codename1.ui.animations.AnimationObject)5 BufferedImage (java.awt.image.BufferedImage)5 ByteArrayInputStream (java.io.ByteArrayInputStream)5 File (java.io.File)5 Hashtable (java.util.Hashtable)5 Map (java.util.Map)5 Form (com.codename1.ui.Form)4 ActionListener (com.codename1.ui.events.ActionListener)4 ImageIO (com.codename1.ui.util.ImageIO)4 ConnectionRequest (com.codename1.io.ConnectionRequest)3 ArrayList (java.util.ArrayList)3 InfiniteProgress (com.codename1.components.InfiniteProgress)2 MapObject (com.codename1.googlemaps.MapContainer.MapObject)2 Externalizable (com.codename1.io.Externalizable)2 JSONParser (com.codename1.io.JSONParser)2 Coord (com.codename1.maps.Coord)2