Android Open Source - AndroidImageCache Bitmaps






From Project

Back to project page AndroidImageCache.

License

The source code is released under:

Przemys?aw Jakubczyk Polidea Sp. z o.o. Copyright (c) 2012 All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the follo...

If you think the Android project AndroidImageCache listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package pl.polidea.webimageview;
/*w  w w .  j a v  a  2 s  . c o  m*/
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;
import android.graphics.Matrix;
import android.media.ExifInterface;
import java.io.File;
import java.io.IOException;

/**
 * @author Przemys?aw Jakubczyk <przemyslaw.jakubczyk@pl.polidea.pl>
 */
public class Bitmaps {

    String path;

    Options options;

    public Bitmaps(String path) {
        if (path == null || !new File(path).exists()) {
            throw new IllegalArgumentException("Can't find a bitmap under path: " + path);
        }

        this.path = path;
        options = getOptions();
    }

    public Bitmap generateBitmap() throws BitmapDecodeException {
        return getBitmap(options.outWidth, options.outHeight);
    }

    public Bitmap generateScaledWidthBitmap(int width) throws BitmapDecodeException {
        final float scale = options.outWidth / (float) width;
        final int height = (int) (options.outHeight / scale);
        return getBitmap(width, height);
    }

    public Bitmap generateScaledHeightBitmap(int height) throws BitmapDecodeException {
        final float scale = options.outHeight / (float) height;
        final int width = (int) (options.outWidth / scale);
        return getBitmap(width, height);
    }

    public Bitmap generateBitmap(int desiredWidth, int desiredHeight) throws BitmapDecodeException {
        return getBitmap(desiredWidth, desiredHeight);
    }

    Bitmap getBitmap(float desiredWidth, float desiredHeight) throws BitmapDecodeException {
        try {
            float scale;
            int width, height;
            int originalHeight = options.outHeight;
            int originalWidth = options.outWidth;
            float scaleH = originalHeight / desiredHeight;
            float scaleW = originalWidth / desiredWidth;

            scale = Math.max(1, Math.max(scaleH, scaleW));

            width = (int) (originalWidth / scale);
            height = (int) (originalHeight / scale);
            options.inJustDecodeBounds = false;
            options.inSampleSize = (int) scale;
            options.inScaled = false;

            Bitmap bitmapFromFile = BitmapFactory.decodeFile(path, options);
            Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmapFromFile, width, height, true);

            if (scaledBitmap != bitmapFromFile) {// LOL :)
                bitmapFromFile.recycle();
            }

            int orientation = getOrientation();
            if (orientation != 0) {
                final Bitmap rotatedBitmap;
                final Matrix matrix = new Matrix();
                matrix.postRotate(orientation);
                rotatedBitmap = Bitmap.createBitmap(scaledBitmap, 0, 0, scaledBitmap.getWidth(), scaledBitmap.getHeight(),
                        matrix, true);

                scaledBitmap.recycle();

                return rotatedBitmap;
            }

            return scaledBitmap;
        } catch (OutOfMemoryError e) {
            throw new BitmapDecodeException(e);
        }
    }

    Options getOptions() {
        final Options options = new Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(path, options);
        return options;
    }

    int getOrientation() {
        try {
            final ExifInterface exif = new ExifInterface(path);
            switch (exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED)) {
                case ExifInterface.ORIENTATION_ROTATE_270:
                    return 270;
                case ExifInterface.ORIENTATION_ROTATE_180:
                    return 180;
                case ExifInterface.ORIENTATION_ROTATE_90:
                    return 90;
                default:
                    return 0;
            }

        } catch (final IOException e) {
            return 0;
        }
    }

}




Java Source Code List

pl.polidea.imagecache.BitmapLRUCache.java
pl.polidea.imagecache.CacheConfig.java
pl.polidea.imagecache.ImageCacheFactory.java
pl.polidea.imagecache.ImageCache.java
pl.polidea.imagecache.MemoryCache.java
pl.polidea.imagecache.OnCacheResultListener.java
pl.polidea.imagecache.StaticCachedImageCacheFactory.java
pl.polidea.thridparty.DiskCache.java
pl.polidea.thridparty.LinkedBlockingDeque.java
pl.polidea.thridparty.LruCache.java
pl.polidea.utils.Dimensions.java
pl.polidea.utils.StackBlockingDeque.java
pl.polidea.utils.StackPoolExecutor.java
pl.polidea.utils.TempFile.java
pl.polidea.utils.Utils.java
pl.polidea.webimagesampleapp.AndroidImageCacheExample.java
pl.polidea.webimageview.BitmapDecodeException.java
pl.polidea.webimageview.Bitmaps.java
pl.polidea.webimageview.DefaultBitmapProcessor.java
pl.polidea.webimageview.ImageViewUpdater.java
pl.polidea.webimageview.WebImageListener.java
pl.polidea.webimageview.WebImageView.java
pl.polidea.webimageview.net.StaticCachedWebClientFactory.java
pl.polidea.webimageview.net.WebCallback.java
pl.polidea.webimageview.net.WebClientFactory.java
pl.polidea.webimageview.net.WebClient.java
pl.polidea.webimageview.net.WebInterfaceImpl.java
pl.polidea.webimageview.net.WebInterface.java
pl.polidea.webimageview.processor.AbstractBitmapProcessorCreationChain.java
pl.polidea.webimageview.processor.BitmapProcessor.java
pl.polidea.webimageview.processor.BothWidthAndHeightFixed.java
pl.polidea.webimageview.processor.BothWidthAndHeightNotFixed.java
pl.polidea.webimageview.processor.OnlyHeightFixed.java
pl.polidea.webimageview.processor.OnlyWidthFixed.java
pl.polidea.webimageview.processor.ProcessorFactory.java
pl.polidea.webimageview.processor.Processor.java
pl.polidea.webimageview.processor.ProgramaticallyCreated.java
pl.polidea.webimageview.processor.Unknown.java