Android Bitmap Decode decodeBitmap(String path, int displayWidth, int displayHeight)

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

Description

get a bitamp can center inside the rect of displayWidth * displayHeight

Parameter

Parameter Description
path a parameter
displayWidth a parameter
displayHeight a parameter

Declaration

public static Bitmap decodeBitmap(String path, int displayWidth,
        int displayHeight) 

Method Source Code

//package com.java2s;

import android.graphics.Bitmap;

import android.graphics.BitmapFactory;

public class Main {
    /**// ww w  .j a  v a2  s  . co m
     * 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 maxImageSize)
  2. decodeSampledBitmapFromFile(String filename, int reqWidth, int reqHeight)
  3. decodeSampledBitmap(Uri uri, int reqWidth, int reqHeight, Activity act)
  4. decodeBitmapFromAssets(Context context, String fileName)