Search in sources :

Example 1 with StorageImage

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

the class ImageDownloadService method cacheImage.

private static Image cacheImage(String cacheKey, boolean keep, String destFile, Dimension scale, Image placeholderImage, boolean maintainAspectRatio) {
    if (destFile != null) {
        if (FileSystemStorage.getInstance().exists(destFile)) {
            Image f;
            if (placeholderImage != null) {
                f = FileEncodedImageAsync.create(destFile, placeholderImage);
            } else {
                if (fastScale && scale != null) {
                    int w = scale.getWidth();
                    int h = scale.getHeight();
                    if (maintainAspectRatio) {
                        f = FileEncodedImage.create(destFile, -1, -1);
                        float actualW = f.getWidth();
                        float actualH = f.getHeight();
                        float r2 = Math.min(((float) w) / actualW, ((float) h) / actualH);
                        w = (int) (actualW * r2);
                        h = (int) (actualH * r2);
                    }
                    f = FileEncodedImage.create(destFile, w, h);
                } else {
                    f = FileEncodedImage.create(destFile, -1, -1);
                }
            }
            return f;
        }
    } else if (cacheKey != null) {
        if (Storage.getInstance().exists(cacheKey)) {
            Image s;
            if (placeholderImage != null) {
                s = StorageImageAsync.create(cacheKey, placeholderImage);
            } else {
                if (fastScale && scale != null) {
                    int w = scale.getWidth();
                    int h = scale.getHeight();
                    if (maintainAspectRatio) {
                        s = StorageImage.create(cacheKey, -1, -1);
                        float actualW = s.getWidth();
                        float actualH = s.getHeight();
                        float r2 = Math.min(((float) w) / actualW, ((float) h) / actualH);
                        w = (int) (actualW * r2);
                        h = (int) (actualH * r2);
                    }
                    s = StorageImage.create(cacheKey, w, h, keep);
                } else {
                    s = StorageImage.create(cacheKey, -1, -1, keep);
                }
                // due to the way the storage image works the data might be corrupted!
                if (((StorageImage) s).getImageData() == null) {
                    return null;
                }
            }
            return s;
        }
    }
    return null;
}
Also used : StorageImage(com.codename1.components.StorageImage) EncodedImage(com.codename1.ui.EncodedImage) Image(com.codename1.ui.Image) FileEncodedImage(com.codename1.components.FileEncodedImage) StorageImage(com.codename1.components.StorageImage)

Aggregations

FileEncodedImage (com.codename1.components.FileEncodedImage)1 StorageImage (com.codename1.components.StorageImage)1 EncodedImage (com.codename1.ui.EncodedImage)1 Image (com.codename1.ui.Image)1