Android Bitmap Scale getScalBitmap(Context context, Uri uri, int destW, int destH)

Here you can find the source of getScalBitmap(Context context, Uri uri, int destW, int destH)

Description

get Scal Bitmap

Declaration

public static Bitmap getScalBitmap(Context context, Uri uri, int destW,
            int destH) 

Method Source Code

//package com.java2s;

import java.io.FileNotFoundException;

import java.io.IOException;
import java.io.InputStream;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import android.net.Uri;

public class Main {
    public static Bitmap getScalBitmap(String file, int destW, int destH) {
        if (file == null)
            return null;

        BitmapFactory.Options options = new BitmapFactory.Options();

        options.inJustDecodeBounds = true;
        Bitmap bm = BitmapFactory.decodeFile(file, options);
        options.inJustDecodeBounds = false;

        float blW = (float) options.outWidth / destW;
        float blH = (float) options.outHeight / destH;

        options.inSampleSize = 1;//from w  w  w.  j  a va 2 s. c  o m
        if (blW > 1 || blH > 1) {
            float bl = (blW > blH ? blW : blH);
            options.inSampleSize = (int) (bl + 0.9f);
        }
        try {
            bm = BitmapFactory.decodeFile(file, options);

        } catch (OutOfMemoryError e) {
            e.printStackTrace();
        }

        return bm;
    }

    public static Bitmap getScalBitmap(Context context, Uri uri, int destW,
            int destH) {
        if (uri == null)
            return null;

        InputStream in = null;
        try {
            in = context.getContentResolver().openInputStream(uri);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return null;
        }
        BitmapFactory.Options options = new BitmapFactory.Options();

        options.inJustDecodeBounds = true;
        Bitmap bm = BitmapFactory.decodeStream(in, null, options);
        options.inJustDecodeBounds = false;
        try {
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        float blW = (float) options.outWidth / destW;
        float blH = (float) options.outHeight / destH;

        options.inSampleSize = 1;
        if (blW > 1 || blH > 1) {
            float bl = (blW > blH ? blW : blH);
            options.inSampleSize = (int) (bl + 0.9f);
        }

        try {
            in = context.getContentResolver().openInputStream(uri);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return null;
        }
        bm = BitmapFactory.decodeStream(in, null, options);
        try {
            in.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return bm;
    }
}

Related

  1. getScalBitmap(String file, int destW, int destH)
  2. getScaledBitmap(Bitmap bitmap, int width, int height)
  3. getScaledBitmapByHeight(Bitmap bm, int newHeight)
  4. scaleBitmap(Bitmap bitmap, int dstWidth, int dstHeight)