Android Bitmap Decode decodeBitmap(String path, int maxImageSize)

Here you can find the source of decodeBitmap(String path, int maxImageSize)

Description

decode Bitmap

Declaration

public static Bitmap decodeBitmap(String path, int maxImageSize) 

Method Source Code

//package com.java2s;

import android.graphics.Bitmap;

import android.graphics.BitmapFactory;

public class Main {
    /**// ww  w  .jav a 2 s.  c om
     * get a bitamp can center inside the rect of displayWidth * displayHeight
     *
     * @param path
     * @param displayWidth
     * @param displayHeight
     * @return
     */
    public static Bitmap decodeBitmap(String path, int displayWidth,
            int displayHeight) {
        BitmapFactory.Options op = new BitmapFactory.Options();
        op.inJustDecodeBounds = true;
        Bitmap bmp = BitmapFactory.decodeFile(path, op);
        int wRatio = (int) Math.ceil(op.outWidth / (float) displayWidth);
        int hRatio = (int) Math.ceil(op.outHeight / (float) displayHeight);
        if (wRatio > 1 && hRatio > 1) {
            if (wRatio > hRatio) {
                op.inSampleSize = wRatio;
            } else {
                op.inSampleSize = hRatio;
            }
        }
        op.inJustDecodeBounds = false;
        bmp = BitmapFactory.decodeFile(path, op);
        return Bitmap.createScaledBitmap(bmp, displayWidth, displayHeight,
                true);
    }

    public static Bitmap decodeBitmap(String path, int maxImageSize) {
        BitmapFactory.Options op = new BitmapFactory.Options();
        op.inJustDecodeBounds = true;
        Bitmap bmp = BitmapFactory.decodeFile(path, op);
        int scale = 1;
        if (op.outWidth > maxImageSize || op.outHeight > maxImageSize) {
            scale = (int) Math.pow(
                    2,
                    (int) Math.round(Math.log(maxImageSize
                            / (double) Math.max(op.outWidth, op.outHeight))
                            / Math.log(0.5)));
        }
        op.inJustDecodeBounds = false;
        op.inSampleSize = scale;
        bmp = BitmapFactory.decodeFile(path, op);
        return bmp;
    }
}

Related

  1. decodeBitmap(String path, int displayWidth, int displayHeight)
  2. decodeSampledBitmapFromFile(String filename, int reqWidth, int reqHeight)
  3. decodeSampledBitmap(Uri uri, int reqWidth, int reqHeight, Activity act)
  4. decodeBitmapFromAssets(Context context, String fileName)
  5. decodeImageFile(String strImagePath, BitmapFactory.Options options, boolean checkOrientation)