Example usage for android.graphics BitmapFactory decodeFile

List of usage examples for android.graphics BitmapFactory decodeFile

Introduction

In this page you can find the example usage for android.graphics BitmapFactory decodeFile.

Prototype

public static Bitmap decodeFile(String pathName, Options opts) 

Source Link

Document

Decode a file path into a bitmap.

Usage

From source file:Main.java

public static Bitmap decodeSampledBitmapFromFile(String filename, int reqWidth, int reqHeight) {

    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;//from  www .ja  va2s  .  com
    BitmapFactory.decodeFile(filename, options);

    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeFile(filename, options);
}

From source file:Main.java

public static Bitmap decodeSampleBitmapFromFile(String path, int reqWidth, int reqHeight) {

    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;//from  www.j a va 2 s .  c  om
    BitmapFactory.decodeFile(path, options);

    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeFile(path, options);

}

From source file:Main.java

public static Bitmap decodeSampledBitmapFromFile(String imgPath, int reqWidth, int reqHeight) {

    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;/*from   w ww . j  a  v a 2 s  .  c o  m*/
    BitmapFactory.decodeFile(imgPath, options);

    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeFile(imgPath, options);
}

From source file:Main.java

public static Bitmap getSampleBitmap(Activity activity, String filepath) {
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;/* w w  w.  j a va 2s  .co m*/
    BitmapFactory.decodeFile(filepath, options);
    options.inJustDecodeBounds = false;
    int width = options.outWidth;
    int height = options.outHeight;

    DisplayMetrics dm = new DisplayMetrics();
    activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
    int scale = Math.min(width / dm.widthPixels, height / dm.heightPixels);
    options.inSampleSize = scale;
    Bitmap bitmap2 = BitmapFactory.decodeFile(filepath, options);
    return bitmap2;
}

From source file:Main.java

static Bitmap decodeSampledBitmapFromFile(String filePath, int reqWidth, int reqHeight) {
    /// first avoid allocate memory to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;//from   w  ww  .j a va 2s.  c  o m
    BitmapFactory.decodeFile(filePath, options);

    options.inSampleSize = calcInSampleSizeDuo(options, reqWidth, reqHeight);

    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeFile(filePath, options);
}

From source file:Main.java

public static void setImageViewWidthBitmap(String imgPath, ImageView imageView) throws IOException {
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;/*from   w  w w.j ava2  s.co  m*/
    BitmapFactory.decodeFile(imgPath, options);
    options.inSampleSize = Math.min(options.outWidth / imageView.getWidth(),
            options.outHeight / imageView.getWidth());
    options.inJustDecodeBounds = false;
    options.inPurgeable = true;
    options.inInputShareable = true;

    FileInputStream fis = new FileInputStream(imgPath);
    imageView.setImageBitmap(BitmapFactory.decodeFileDescriptor(fis.getFD(), null, options));
}

From source file:Main.java

public static Bitmap decodeImageFile(String strImagePath, BitmapFactory.Options options,
        boolean checkOrientation) {
    if (checkOrientation == false) {
        return BitmapFactory.decodeFile(strImagePath, options);
    } else {//from   w  ww.ja va 2  s . co m
        Bitmap bm = BitmapFactory.decodeFile(strImagePath, options);
        int degree = getExifDegree(strImagePath);
        return getRotatedBitmap(bm, degree);
    }
}

From source file:Main.java

public static Bitmap scaleBitmap(int height, int width, String pathImage) {
    Bitmap desBitmap = null;/*from w ww  . ja  v  a  2  s.  co m*/
    BitmapFactory.Options bmOptions = new BitmapFactory.Options();
    bmOptions.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(pathImage, bmOptions);
    int photoW = bmOptions.outWidth;
    int photoH = bmOptions.outHeight;

    // Determine how much to scale down the image
    int scaleFactor = Math.min(photoW / width, photoH / height);

    // Decode the image file into a Bitmap sized to fill the View
    bmOptions.inJustDecodeBounds = false;
    bmOptions.inSampleSize = scaleFactor << 1;
    bmOptions.inPurgeable = true;

    Bitmap bitmap = BitmapFactory.decodeFile(pathImage, bmOptions);

    Matrix mtx = new Matrix();
    mtx.postRotate(90);
    // Rotating Bitmap
    Bitmap rotatedBMP = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), mtx, true);

    if (rotatedBMP != bitmap) {
        bitmap.recycle();
    }

    return rotatedBMP;
}

From source file:Main.java

public static Bitmap decodeSampleBitmap(String path, float reqWidth, float reqHeight) {
    Bitmap bm = null;// w  ww  .  j  a  va 2 s.c  om
    Options opts = new Options();
    opts.inJustDecodeBounds = true;
    opts.inPreferredConfig = Config.RGB_565;
    BitmapFactory.decodeFile(path, opts);
    opts.inSampleSize = calculateInSampleSize(opts, reqWidth, reqHeight);
    opts.inJustDecodeBounds = false;
    bm = BitmapFactory.decodeFile(path, opts);
    return bm;
}

From source file:Main.java

public static Bitmap decodeSampledBitmapFromFile(String path, int reqWidth, int reqHeight) { // BEST QUALITY MATCH

    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;/*from  www . ja  v a 2s  .  c o m*/
    BitmapFactory.decodeFile(path, options);

    // Calculate inSampleSize, Raw height and width of image
    final int height = options.outHeight;
    final int width = options.outWidth;
    options.inPreferredConfig = Bitmap.Config.RGB_565;
    int inSampleSize = 1;

    if (height > reqHeight) {
        inSampleSize = Math.round((float) height / (float) reqHeight);
    }
    int expectedWidth = width / inSampleSize;

    if (expectedWidth > reqWidth) {
        // if(Math.round((float)width / (float)reqWidth) > inSampleSize) //
        // If bigger SampSize..
        inSampleSize = Math.round((float) width / (float) reqWidth);
    }

    options.inSampleSize = inSampleSize;

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;

    return BitmapFactory.decodeFile(path, options);
}