biz.varkon.shelvesom.util.ImageUtilities.java Source code

Java tutorial

Introduction

Here is the source code for biz.varkon.shelvesom.util.ImageUtilities.java

Source

/*
 * Copyright (C) 2008 Romain Guy
 * Copyright (C) 2010 Garen J. Torikian
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package biz.varkon.shelvesom.util;

import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.ref.SoftReference;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.HttpGet;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.LinearGradient;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Shader;

import biz.varkon.shelvesom.drawable.FastBitmapDrawable;

public class ImageUtilities {
    private static final String LOG_TAG = "ImageUtilities";

    private static final boolean FLAG_DECODE_BITMAP_WITH_SKIA = false;

    private static final float EDGE_START = 0.0f;
    private static final float EDGE_END = 4.0f;
    private static final int EDGE_COLOR_START = 0x7F000000;
    private static final int EDGE_COLOR_END = 0x00000000;
    private static final Paint EDGE_PAINT = new Paint();

    private static final int END_EDGE_COLOR_START = 0x00000000;
    private static final int END_EDGE_COLOR_END = 0x4F000000;
    private static final Paint END_EDGE_PAINT = new Paint();

    private static final float FOLD_START = 5.0f;
    private static final float FOLD_END = 13.0f;
    private static final int FOLD_COLOR_START = 0x00000000;
    private static final int FOLD_COLOR_END = 0x26000000;
    private static final Paint FOLD_PAINT = new Paint();

    private static final float SHADOW_RADIUS = 12.0f;
    private static final int SHADOW_COLOR = 0x99000000;
    private static final Paint SHADOW_PAINT = new Paint();

    private static final Paint SCALE_PAINT = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);

    private static final FastBitmapDrawable NULL_DRAWABLE = new FastBitmapDrawable(null);

    // TODO: Use a concurrent HashMap to support multiple threads
    private static final HashMap<String, SoftReference<FastBitmapDrawable>> sArtCache = new HashMap<String, SoftReference<FastBitmapDrawable>>();

    private static volatile Matrix sScaleMatrix;
    private static SimpleDateFormat sLastModifiedFormat;

    static {
        Shader shader = new LinearGradient(EDGE_START, 0.0f, EDGE_END, 0.0f, EDGE_COLOR_START, EDGE_COLOR_END,
                Shader.TileMode.CLAMP);
        EDGE_PAINT.setShader(shader);

        shader = new LinearGradient(EDGE_START, 0.0f, EDGE_END, 0.0f, END_EDGE_COLOR_START, END_EDGE_COLOR_END,
                Shader.TileMode.CLAMP);
        END_EDGE_PAINT.setShader(shader);

        shader = new LinearGradient(FOLD_START, 0.0f, FOLD_END, 0.0f,
                new int[] { FOLD_COLOR_START, FOLD_COLOR_END, FOLD_COLOR_START }, new float[] { 0.0f, 0.5f, 1.0f },
                Shader.TileMode.CLAMP);
        FOLD_PAINT.setShader(shader);

        SHADOW_PAINT.setShadowLayer(SHADOW_RADIUS / 2.0f, 0.0f, 0.0f, SHADOW_COLOR);
        SHADOW_PAINT.setAntiAlias(true);
        SHADOW_PAINT.setFilterBitmap(true);
        SHADOW_PAINT.setColor(0xFF000000);
        SHADOW_PAINT.setStyle(Paint.Style.FILL);
    }

    private ImageUtilities() {
    }

    /**
     * A Bitmap associated with its last modification date. This can be used to
     * check whether the book covers should be downloaded again.
     */
    public static class ExpiringBitmap {
        public Bitmap bitmap;
        public Calendar lastModified;
    }

    /**
     * Deletes the specified drawable from the cache. Calling this method will
     * remove the drawable from the in-memory cache and delete the corresponding
     * file from the external storage.
     * 
     * @param id
     *            The id of the drawable to delete from the cache
     */
    public static void deleteCachedCover(String id) {
        if (id != null) {
            new File(IOUtilities.getCacheDirectory(), id).delete();
            sArtCache.remove(id);
        }
    }

    /**
     * Retrieves a drawable from the book covers cache, identified by the
     * specified id. If the drawable does not exist in the cache, it is loaded
     * and added to the cache. If the drawable cannot be added to the cache, the
     * specified default drawable is returned.
     * 
     * @param id
     *            The id of the drawable to retrieve
     * @param defaultCover
     *            The default drawable returned if no drawable can be found that
     *            matches the id
     * 
     * @return The drawable identified by id or defaultCover
     * @throws IOException
     * @throws FileNotFoundException
     */
    public static FastBitmapDrawable getCachedCover(String id, FastBitmapDrawable defaultCover) {
        FastBitmapDrawable drawable = null;

        SoftReference<FastBitmapDrawable> reference = sArtCache.get(id);
        if (reference != null) {
            drawable = reference.get();
        }

        if (drawable == null) {
            final Bitmap bitmap = loadCover(id);
            if (bitmap != null) {
                drawable = new FastBitmapDrawable(bitmap);
            } else {
                drawable = NULL_DRAWABLE;
            }

            sArtCache.put(id, new SoftReference<FastBitmapDrawable>(drawable));
        }

        return drawable == NULL_DRAWABLE ? defaultCover : drawable;
    }

    /**
     * Removes all the callbacks from the drawables stored in the memory cache.
     * This method must be called from the onDestroy() method of any activity
     * using the cached drawables. Failure to do so will result in the entire
     * activity being leaked.
     */
    public static void cleanupCache() {
        for (SoftReference<FastBitmapDrawable> reference : sArtCache.values()) {
            final FastBitmapDrawable drawable = reference.get();
            if (drawable != null)
                drawable.setCallback(null);
        }
    }

    /**
     * Loads an image from the specified URL.
     * 
     * @param url
     *            The URL of the image to load.
     * 
     * @return The image at the specified URL or null if an error occured.
     */
    public static ExpiringBitmap load(String url) {
        return load(url, null);
    }

    /**
     * Loads an image from the specified URL with the specified cookie.
     * 
     * @param url
     *            The URL of the image to load.
     * @param cookie
     *            The cookie to use to load the image.
     * 
     * @return The image at the specified URL or null if an error occured.
     */
    public static ExpiringBitmap load(String url, String cookie) {
        ExpiringBitmap expiring = new ExpiringBitmap();

        final HttpGet get = new HttpGet(url);
        if (cookie != null)
            get.setHeader("cookie", cookie);

        HttpEntity entity = null;
        try {
            final HttpResponse response = HttpManager.execute(get);
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                setLastModified(expiring, response);

                entity = response.getEntity();

                InputStream in = null;
                OutputStream out = null;

                try {
                    in = entity.getContent();

                    if (FLAG_DECODE_BITMAP_WITH_SKIA) {
                        expiring.bitmap = BitmapFactory.decodeStream(in);
                    } else {
                        final ByteArrayOutputStream dataStream = new ByteArrayOutputStream();
                        out = new BufferedOutputStream(dataStream, IOUtilities.IO_BUFFER_SIZE);
                        IOUtilities.copy(in, out);
                        out.flush();

                        final byte[] data = dataStream.toByteArray();

                        final double ratio = getScale(in);

                        final BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
                        bitmapOptions.inSampleSize = getPowerOfTwoForSampleRatio(ratio);
                        bitmapOptions.inDither = true;
                        bitmapOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;

                        expiring.bitmap = BitmapFactory.decodeByteArray(data, 0, data.length, bitmapOptions);
                    }
                } catch (IOException e) {
                    android.util.Log.e(LOG_TAG, "Could not load image from " + url, e);
                } catch (OutOfMemoryError oom) {
                    expiring.bitmap = null;
                } finally {
                    IOUtilities.closeStream(in);
                    IOUtilities.closeStream(out);
                }
            }
        } catch (IOException e) {
            android.util.Log.e(LOG_TAG, "Could not load image from " + url, e);
        } finally {
            if (entity != null) {
                try {
                    entity.consumeContent();
                } catch (IOException e) {
                    android.util.Log.e(LOG_TAG, "Could not load image from " + url, e);
                }
            }
        }

        return expiring;
    }

    private static void setLastModified(ExpiringBitmap expiring, HttpResponse response) {
        expiring.lastModified = null;

        final Header header = response.getFirstHeader("Last-Modified");
        if (header == null)
            return;

        if (sLastModifiedFormat == null) {
            sLastModifiedFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z");
        }

        final Calendar calendar = Calendar.getInstance();
        try {
            calendar.setTime(sLastModifiedFormat.parse(header.getValue()));
            expiring.lastModified = calendar;
        } catch (ParseException e) {
            // Ignore
        }
    }

    /**
     * Return the same image with a shadow, scaled by the specified amount..
     * 
     * @param bitmap
     *            The bitmap to decor with a shadow
     * @param width
     *            The target width of the decored bitmap
     * @param height
     *            The target height of the decored bitmap
     * 
     * @return A new Bitmap based on the original bitmap
     */
    public static Bitmap createShadow(Bitmap bitmap, int width, int height) {
        if (bitmap == null)
            return null;

        final int bitmapWidth = bitmap.getWidth();
        final int bitmapHeight = bitmap.getHeight();

        final float scale = Math.min((float) width / (float) bitmapWidth, (float) height / (float) bitmapHeight);

        final int scaledWidth = (int) (bitmapWidth * scale);
        final int scaledHeight = (int) (bitmapHeight * scale);

        return createScaledBitmap(bitmap, scaledWidth, scaledHeight, SHADOW_RADIUS, false, SHADOW_PAINT);
    }

    /**
     * Create a book cover with the specified bitmap. This method applies
     * several lighting effects to the original bitmap and returns a new decored
     * bitmap.
     * 
     * @param bitmap
     *            The bitmap to decor with lighting effects
     * @param width
     *            The target width of the decored bitmap
     * @param height
     *            The target height of the decored bitmap
     * 
     * @return A new Bitmap based on the original bitmap
     */
    public static Bitmap createCover(Bitmap bitmap, int width, int height) {
        final int bitmapWidth = bitmap.getWidth();
        final int bitmapHeight = bitmap.getHeight();

        final float scale = Math.min((float) width / (float) bitmapWidth, (float) height / (float) bitmapHeight);

        final int scaledWidth = (int) (bitmapWidth * scale);
        final int scaledHeight = (int) (bitmapHeight * scale);

        final Bitmap decored = createScaledBitmap(bitmap, scaledWidth, scaledHeight, SHADOW_RADIUS, true,
                SHADOW_PAINT);
        bitmap.recycle();

        final Canvas canvas = new Canvas(decored);

        canvas.translate(SHADOW_RADIUS / 2.0f, SHADOW_RADIUS / 2.0f);
        canvas.drawRect(EDGE_START, 0.0f, EDGE_END, scaledHeight, EDGE_PAINT);
        canvas.drawRect(FOLD_START, 0.0f, FOLD_END, scaledHeight, FOLD_PAINT);
        // noinspection PointlessArithmeticExpression
        canvas.translate(scaledWidth - (EDGE_END - EDGE_START), 0.0f);
        canvas.drawRect(EDGE_START, 0.0f, EDGE_END, scaledHeight, END_EDGE_PAINT);

        return decored;
    }

    private static Bitmap loadCover(String id) {
        if (id == null)
            return null;
        final File file = new File(IOUtilities.getCacheDirectory(), id);

        try {
            if (file.exists()) {
                return decodeFile(file);
            } else {
                // final File newFile = new File(IOUtilities.ensureCache(), id);
                // if (newFile.exists())
                // return decodeFile(newFile);
            }
        } catch (Exception e) {
            return null;
        }
        return null;
    }

    private static Bitmap createScaledBitmap(Bitmap src, int dstWidth, int dstHeight, float offset,
            boolean clipShadow, Paint paint) {
        Matrix m;
        synchronized (Bitmap.class) {
            m = sScaleMatrix;
            sScaleMatrix = null;
        }

        if (m == null) {
            m = new Matrix();
        }

        final int width = src.getWidth();
        final int height = src.getHeight();
        final float sx = dstWidth / (float) width;
        final float sy = dstHeight / (float) height;
        m.setScale(sx, sy);

        Bitmap b = createBitmap(src, 0, 0, width, height, m, offset, clipShadow, paint);

        synchronized (Bitmap.class) {
            sScaleMatrix = m;
        }

        src.recycle();
        return b;
    }

    private static Bitmap createBitmap(Bitmap source, int x, int y, int width, int height, Matrix m, float offset,
            boolean clipShadow, Paint paint) {

        int scaledWidth = width;
        int scaledHeight = height;

        final Canvas canvas = new Canvas();
        canvas.translate(offset / 2.0f, offset / 2.0f);

        Bitmap bitmap;

        final Rect from = new Rect(x, y, x + width, y + height);
        final RectF to = new RectF(0, 0, width, height);

        if (m == null || m.isIdentity()) {
            bitmap = Bitmap.createBitmap(scaledWidth + (int) offset,
                    scaledHeight + (int) (clipShadow ? (offset / 2.0f) : offset), Bitmap.Config.ARGB_8888);
            paint = null;
        } else {
            RectF mapped = new RectF();
            m.mapRect(mapped, to);

            scaledWidth = Math.round(mapped.width());
            scaledHeight = Math.round(mapped.height());

            bitmap = Bitmap.createBitmap(scaledWidth + (int) offset,
                    scaledHeight + (int) (clipShadow ? (offset / 2.0f) : offset), Bitmap.Config.ARGB_8888);
            canvas.translate(-mapped.left, -mapped.top);
            canvas.concat(m);
        }

        canvas.setBitmap(bitmap);
        canvas.drawRect(0.0f, 0.0f, width, height, paint);
        canvas.drawBitmap(source, from, to, SCALE_PAINT);

        return bitmap;
    }

    // GJT: Solves "java.lang.OutOfMemoryError: bitmap size exceeds VM budget"
    public static double getScale(InputStream stream) {
        final BitmapFactory.Options onlyBoundsOptions = new BitmapFactory.Options();
        onlyBoundsOptions.inJustDecodeBounds = true;
        onlyBoundsOptions.inDither = true;
        onlyBoundsOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;
        BitmapFactory.decodeStream(stream, null, onlyBoundsOptions);

        try {
            stream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        if ((onlyBoundsOptions.outWidth == -1) || (onlyBoundsOptions.outHeight == -1))
            return 1.0;

        // Determine the limiting size of the width and height sizes
        final int originalSize = (onlyBoundsOptions.outHeight > onlyBoundsOptions.outWidth)
                ? onlyBoundsOptions.outHeight
                : onlyBoundsOptions.outWidth;

        // How much do we need to shrink it to make it smaller than the advised
        // thumbnail size, either direction.
        final double ratio = (originalSize > Preferences.getHeightForManager())
                ? (originalSize / Preferences.getHeightForManager())
                : 1.0;

        return ratio;
    }

    public static int getPowerOfTwoForSampleRatio(double ratio) {
        int k = Integer.highestOneBit((int) Math.floor(ratio));
        if (k == 0)
            return 1;
        else
            return k;
    }

    public static Bitmap decodeFile(File f) {
        Bitmap b = null;
        try {
            // Decode image size
            BitmapFactory.Options o = new BitmapFactory.Options();
            o.inJustDecodeBounds = true;

            FileInputStream fis = new FileInputStream(f);
            BitmapFactory.decodeStream(fis, null, o);
            fis.close();

            final int REQUIRED_WIDTH = Preferences.getWidthForManager();
            final int REQUIRED_HEIGHT = Preferences.getHeightForManager();

            int scale = 1;
            if (o.outHeight > REQUIRED_HEIGHT || o.outWidth > REQUIRED_WIDTH) {
                scale = (int) Math.pow(2, (int) Math.round(
                        Math.log(REQUIRED_HEIGHT / (double) Math.max(o.outHeight, o.outWidth)) / Math.log(0.5)));
            }

            // Decode with inSampleSize
            BitmapFactory.Options o2 = new BitmapFactory.Options();
            o2.inSampleSize = scale;
            fis = new FileInputStream(f);
            b = BitmapFactory.decodeStream(fis, null, o2);
            fis.close();
        } catch (FileNotFoundException e) {
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return b;
    }

    /*
     * public static Bitmap decodeFile(FileInputStream f) { Bitmap b = null; try
     * { // Decode image size BitmapFactory.Options o = new
     * BitmapFactory.Options(); o.inJustDecodeBounds = true;
     * 
     * FileInputStream fis = f; BitmapFactory.decodeStream(fis, null, o);
     * fis.close();
     * 
     * final int REQUIRED_WIDTH = Preferences.getWidthForManager(); final int
     * REQUIRED_HEIGHT = Preferences.getHeightForManager();
     * 
     * int scale = 1; if (o.outHeight > REQUIRED_HEIGHT || o.outWidth >
     * REQUIRED_WIDTH) { scale = (int) Math.pow( 2, (int)
     * Math.round(Math.log(REQUIRED_HEIGHT / (double) Math.max(o.outHeight,
     * o.outWidth)) / Math.log(0.5))); }
     * 
     * // Decode with inSampleSize BitmapFactory.Options o2 = new
     * BitmapFactory.Options(); o2.inSampleSize = scale; fis = f; b =
     * BitmapFactory.decodeStream(fis, null, o2); fis.close(); } catch
     * (FileNotFoundException e) { } catch (IOException e) { // TODO
     * Auto-generated catch block e.printStackTrace(); } return b; }
     */

    /*
     * public static int getScale(Context context, int resource) {
     * BitmapFactory.Options o = new BitmapFactory.Options();
     * o.inJustDecodeBounds = true;
     * BitmapFactory.decodeResource(context.getResources(), resource, o);
     * 
     * int width_tmp = o.outWidth, height_tmp = o.outHeight; int scale = 1;
     * 
     * final int REQUIRED_WIDTH = Preferences.getWidthForManager(); final int
     * REQUIRED_HEIGHT = Preferences.getHeightForManager();
     * 
     * while (true) { if (width_tmp / 2 < REQUIRED_WIDTH || height_tmp / 2 <
     * REQUIRED_HEIGHT) break; width_tmp /= 2; height_tmp /= 2; scale++; }
     * 
     * return scale; }
     */
}