/*
 * BitmapUtils.java
 *
 * Author: Ming Tsang
 * Copyright (c) 2015 Ming Tsang
 * Refer to LICENSE for details
 */

package com.nkming.utils.graphic;

import android.graphics.BitmapFactory;

import com.nkming.utils.Log;
import com.nkming.utils.Res;
import com.nkming.utils.type.Size;

public class BitmapUtils
{
	/**
	 * Return the size of a bitmap without loading the whole file
	 *
	 * @param path
	 * @return
	 */
    public static Size getSize(String path)
    {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;

        BitmapFactory.decodeFile(path, options);
        if (options.outWidth == -1 || options.outHeight == -1)
        {
	        Log.e(LOG_TAG + ".getSize", "Failed while decodeFile");
            return null;
        }
        else
        {
            return new Size(options.outWidth, options.outHeight);
        }
    }

	private static final String LOG_TAG = Res.LOG_TAG + "."
			+ BitmapUtils.class.getSimpleName();
}

@@ -146,16 +146,48 @@
 aphic;%0A%0A
+import android.graphics.Bitmap;%0A
 import a
@@ -217,16 +217,16 @@
 actory;%0A
-
 %0Aimport 
@@ -312,16 +312,38 @@
 .Size;%0A%0A
+import java.io.File;%0A%0A
 public c
@@ -954,15 +954,1161 @@
   %7D%0A
-
     %7D%0A%0A
+%09/**%0A%09 * Load a bitmap file at a specific size%0A%09 *%0A%09 * @param path%0A%09 * @param size The target size%0A%09 * @param calc The resizing strategy used%0A%09 * @return The loaded bitmap if successful, null otherwise%0A%09 */%0A%09public static Bitmap loadImageAtSize(String path, Size size, SizeCalc calc)%0A%09%7B%0A%09%09if (!new File(path).exists())%0A%09%09%7B%0A%09%09%09Log.e(LOG_TAG + %22.loadImageAtSize%22, %22File not exists%22);%0A%09%09%09return null;%0A%09%09%7D%0A%0A%09%09Size origSize = getSize(path);%0A%09%09Size bestSize = calc.calc(new Size(origSize.w(), origSize.h()), size);%0A%09%09int ratio = Math.min(size.w() / bestSize.w(), size.h() / bestSize.h());%0A%0A%09%09BitmapFactory.Options ops = new BitmapFactory.Options();%0A%09%09if (ratio %3E= 2)%0A%09%09%7B%0A%09%09%09ops.inSampleSize = ratio;%0A%09%09%7D%0A%09%09Bitmap product = BitmapFactory.decodeFile(path, ops);%0A%09%09if (product == null)%0A%09%09%7B%0A%09%09%09Log.e(LOG_TAG + %22.loadImageAtSize%22, %22Failed while decodeFile%22);%0A%09%09%09return null;%0A%09%09%7D%0A%0A%09%09Size loadedSize = new Size(ops.outWidth, ops.outHeight);%0A%09%09if (!loadedSize.equals(bestSize))%0A%09%09%7B%0A%09%09%09Bitmap resized = Bitmap.createScaledBitmap(product, bestSize.w(),%0A%09%09%09%09%09bestSize.h(), true);%0A%09%09%09product.recycle();%0A%09%09%09return resized;%0A%09%09%7D%0A%09%09else%0A%09%09%7B%0A%09%09%09return product;%0A%09%09%7D%0A%09%7D%0A%0A
 %09pri

Add fn to load a bitmap file at a specific size