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

package com.nkming.utils.graphic;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

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

import java.io.File;

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);
        }
    }

	/**
	 * Load a bitmap file at a specific size
	 *
	 * @param path
	 * @param size The target size
	 * @param calc The resizing strategy used
	 * @return The loaded bitmap if successful, null otherwise
	 */
	public static Bitmap loadImageAtSize(String path, Size size, SizeCalc calc)
	{
		if (!new File(path).exists())
		{
			Log.e(LOG_TAG + ".loadImageAtSize", "File not exists");
			return null;
		}

		Size origSize = getSize(path);
		Size bestSize = calc.calc(new Size(origSize.w(), origSize.h()), size);
		int ratio = Math.min(size.w() / bestSize.w(), size.h() / bestSize.h());

		BitmapFactory.Options ops = new BitmapFactory.Options();
		if (ratio >= 2)
		{
			ops.inSampleSize = ratio;
		}
		Bitmap product = BitmapFactory.decodeFile(path, ops);
		if (product == null)
		{
			Log.e(LOG_TAG + ".loadImageAtSize", "Failed while decodeFile");
			return null;
		}

		Size loadedSize = new Size(ops.outWidth, ops.outHeight);
		if (!loadedSize.equals(bestSize))
		{
			Bitmap resized = Bitmap.createScaledBitmap(product, bestSize.w(),
					bestSize.h(), true);
			product.recycle();
			return resized;
		}
		else
		{
			return product;
		}
	}

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

@@ -327,20 +327,147 @@
 java.io.
-File
+BufferedOutputStream;%0Aimport java.io.File;%0Aimport java.io.FileOutputStream;%0Aimport java.io.IOException;%0Aimport java.io.OutputStream
 ;%0A%0Apubli
@@ -2230,16 +2230,705 @@
 %09%09%7D%0A%09%7D%0A%0A
+%09/**%0A%09 * Save a bitmap to a specific path%0A%09 *%0A%09 * @param bmp%0A%09 * @param path%0A%09 * @param format%0A%09 * @param quality%0A%09 * @return%0A%09 */%0A%09public static boolean saveBitmap(Bitmap bmp, String path,%0A%09%09%09Bitmap.CompressFormat format, int quality)%0A%09%7B%0A%09%09File file = new File(path);%0A%09%09OutputStream os = null;%0A%09%09try%0A%09%09%7B%0A%09%09%09os = new FileOutputStream(file);%0A%09%09%09os = new BufferedOutputStream(os);%0A%09%09%09bmp.compress(format, quality, os);%0A%09%09%09return true;%0A%09%09%7D%0A%09%09catch (IOException e)%0A%09%09%7B%0A%09%09%09Log.e(LOG_TAG + %22.saveBitmap%22, %22IOException%22, e);%0A%09%09%09return false;%0A%09%09%7D%0A%09%09finally%0A%09%09%7B%0A%09%09%09if (os != null)%0A%09%09%09%7B%0A%09%09%09%09try%0A%09%09%09%09%7B%0A%09%09%09%09%09os.close();%0A%09%09%09%09%7D%0A%09%09%09%09catch (Exception e)%0A%09%09%09%09%7B%0A%09%09%09%09%09e.printStackTrace();%0A%09%09%09%09%7D%0A%09%09%09%7D%0A%09%09%7D%0A%09%7D%0A%0A
 %09private

Add fn to easily save a bitmap