Search in sources :

Example 1 with ReplaceableImage

use of com.codename1.components.ReplaceableImage in project CodenameOne by codenameone.

the class CloudImageProperty method propertyValue.

/**
 * {@inheritDoc}
 */
public Object propertyValue(CloudObject obj, String propertyName) {
    final String key = (String) obj.getObject(idProperty);
    if (key == null) {
        return placeholderImage;
    }
    Image image = (Image) getCache().get(key);
    if (image == null) {
        ReplaceableImage r = inProgress.get(key);
        if (r != null) {
            return r;
        }
        final ReplaceableImage rp = ReplaceableImage.create(placeholderImage);
        inProgress.put(key, rp);
        ConnectionRequest cr = new ConnectionRequest() {

            private EncodedImage e;

            protected void readResponse(InputStream input) throws IOException {
                e = EncodedImage.create(input);
                ;
                if (e.getWidth() != placeholderImage.getWidth() || e.getHeight() != placeholderImage.getHeight()) {
                    ImageIO io = ImageIO.getImageIO();
                    if (io != null) {
                        ByteArrayOutputStream bo = new ByteArrayOutputStream();
                        io.save(new ByteArrayInputStream(e.getImageData()), bo, ImageIO.FORMAT_JPEG, placeholderImage.getWidth(), placeholderImage.getHeight(), 0.9f);
                        e = EncodedImage.create(bo.toByteArray());
                    }
                }
            }

            protected void postResponse() {
                rp.replace(e);
                getCache().put(key, e);
                inProgress.remove(key);
            }
        };
        cr.setPost(false);
        cr.setUrl(CloudStorage.getInstance().getUrlForCloudFileId(key));
        NetworkManager.getInstance().addToQueue(cr);
        return rp;
    }
    return image;
}
Also used : ConnectionRequest(com.codename1.io.ConnectionRequest) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ReplaceableImage(com.codename1.components.ReplaceableImage) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Image(com.codename1.ui.Image) ReplaceableImage(com.codename1.components.ReplaceableImage) EncodedImage(com.codename1.ui.EncodedImage) EncodedImage(com.codename1.ui.EncodedImage) ImageIO(com.codename1.ui.util.ImageIO)

Aggregations

ReplaceableImage (com.codename1.components.ReplaceableImage)1 ConnectionRequest (com.codename1.io.ConnectionRequest)1 EncodedImage (com.codename1.ui.EncodedImage)1 Image (com.codename1.ui.Image)1 ImageIO (com.codename1.ui.util.ImageIO)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 InputStream (java.io.InputStream)1