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 decodeSampledBitmapFileForSize(File f, int reqWidth, int reqHeight) {
    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;/* w w  w  .j a v a  2  s  .c  o m*/
    BitmapFactory.decodeFile(f.getPath(), options);

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

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

    return BitmapFactory.decodeFile(f.getPath(), options);
}

From source file:Main.java

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

    Bitmap bm = null;//  w ww.  jav a  2 s . c o  m
    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    options.inScaled = false;
    BitmapFactory.decodeFile(path, options);

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

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

    return bm;
}

From source file:Main.java

public static Bitmap getDrawable(String path, int zoom, int mItemwidth, int mItemHerght) {

    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;/*from ww w.j a  v  a 2s  . c  om*/
    BitmapFactory.decodeFile(path, options);
    int mWidth = options.outWidth;
    int mHeight = options.outHeight;
    int s = 1;
    while ((mWidth / s > mItemwidth * 2 * zoom) || (mHeight / s > mItemHerght * 2 * zoom)) {
        s *= 2;
    }

    options = new BitmapFactory.Options();
    options.inPreferredConfig = Config.ARGB_8888;
    options.inSampleSize = s;
    Bitmap bm = BitmapFactory.decodeFile(path, options);

    if (bm != null) {
        int h = bm.getHeight();
        int w = bm.getWidth();

        float ft = (float) ((float) w / (float) h);
        float fs = (float) ((float) mItemwidth / (float) mItemHerght);

        int neww = ft >= fs ? mItemwidth * zoom : (int) (mItemHerght * zoom * ft);
        int newh = ft >= fs ? (int) (mItemwidth * zoom / ft) : mItemHerght * zoom;

        float scaleWidth = ((float) neww) / w;
        float scaleHeight = ((float) newh) / h;

        Matrix matrix = new Matrix();
        matrix.postScale(scaleWidth, scaleHeight);
        bm = Bitmap.createBitmap(bm, 0, 0, w, h, matrix, true);
        return bm;
    }
    return null;
}

From source file:Main.java

/**
 * @param imagePath/*  ww  w. j a  va  2  s .  com*/
 * @return
 */
public static int[] getActualImageDimension(String imagePath) {
    int[] imageSize = new int[2];
    BitmapFactory.Options decodeOptions = new BitmapFactory.Options();
    // If we have to resize this image, first get the natural bounds.
    decodeOptions.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(imagePath, decodeOptions);
    int actualWidth = decodeOptions.outWidth;
    int actualHeight = decodeOptions.outHeight;
    imageSize[0] = actualWidth;
    imageSize[1] = actualHeight;
    return imageSize;
}

From source file:Main.java

public static Bitmap decodeSampledBitmapFromFile(File file, int reqWidth, int reqHeight) {

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

    if (options.outHeight > 0 && options.outWidth > 0) {
        // Calculate inSampleSize
        options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeFile(file.getAbsolutePath(), options);
    } else {
        return null;
    }
}

From source file:Main.java

public static Bitmap fix(String path, int w, int h) {

    if (TextUtils.isEmpty(path))
        return null;
    BitmapFactory.Options options = new Options();
    options.inJustDecodeBounds = true;//from   w  ww .j  a  va2 s .  c o m
    BitmapFactory.decodeFile(path, options);
    int imageH = options.outHeight;
    int imageW = options.outWidth;

    int scaleX = imageH / w;
    int scaleY = imageW / h;
    int scale = 1;
    if (scaleX >= scaleY & scaleY >= 1) {
        scale = scaleX;
    }
    if (scaleY >= scaleX & scaleX >= 1) {
        scale = scaleY;
    }

    options.inJustDecodeBounds = false;
    options.inSampleSize = scale;
    return BitmapFactory.decodeFile(path, options);

}

From source file:Main.java

public static Bitmap decodeSampledBitmapFromFile(File file, int reqWidth, int reqHeight) {
    // First decode with inJustDecodeBounds=true to check dimensions
    final Options options = new Options();
    options.inJustDecodeBounds = true;// w w  w.  j a v  a2  s  .com
    BitmapFactory.decodeFile(file.getAbsolutePath(), options);

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

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

From source file:Main.java

public static Bitmap ReadBitmapBySD(String path, int screenWidth, int screenHight) {
    try {//from  www.  j ava  2 s  . c  o m
        if (path == null) {
            return null;
        }
        BitmapFactory.Options opts = new BitmapFactory.Options();
        opts.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(path, opts);

        opts.inSampleSize = computeSampleSize(opts, -1, screenWidth * screenHight);
        opts.inJustDecodeBounds = false;
        try {
            Bitmap bmp = BitmapFactory.decodeFile(path, opts);
            return bmp;
        } catch (OutOfMemoryError err) {
        }
    } catch (Exception e) {
        return null;
    }
    return null;
}

From source file:Main.java

public static Bitmap rotateBitmapFromExif(String filePath, Bitmap bitmap) {
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;//from ww w .ja v  a  2s  .  c  o m
    BitmapFactory.decodeFile(filePath, options);

    String orientString;
    try {
        ExifInterface exif = new ExifInterface(filePath);
        orientString = exif.getAttribute(ExifInterface.TAG_ORIENTATION);
    } catch (IOException e) {
        e.printStackTrace();
        return bitmap;
    }

    int orientation = (orientString != null) ? Integer.parseInt(orientString)
            : ExifInterface.ORIENTATION_NORMAL;
    int rotationAngle = 0, outHeight = 0, outWidth = 0;
    switch (orientation) {
    case ExifInterface.ORIENTATION_ROTATE_90:
        rotationAngle = 90;
        outHeight = bitmap.getWidth();
        outWidth = bitmap.getHeight();
        break;
    case ExifInterface.ORIENTATION_ROTATE_180:
        rotationAngle = 180;
        outHeight = bitmap.getHeight();
        outWidth = bitmap.getWidth();
        break;
    case ExifInterface.ORIENTATION_ROTATE_270:
        rotationAngle = 270;
        outHeight = bitmap.getWidth();
        outWidth = bitmap.getHeight();
        break;
    }

    if (rotationAngle == 0) {
        return bitmap;
    }

    Matrix matrix = new Matrix();
    matrix.setRotate(rotationAngle, (float) bitmap.getWidth() / 2, (float) bitmap.getHeight() / 2);
    Bitmap rotateBitmap = Bitmap.createBitmap(bitmap, 0, 0, outWidth, outHeight, matrix, true);
    if (bitmap != rotateBitmap) {
        bitmap.recycle();
    }

    return rotateBitmap;
}

From source file:Main.java

public static Bitmap decodeSampledBitmapFromFd(String pathName, int reqWidth, int reqHeight) {
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;/*from www  . jav  a 2  s.  co  m*/
    BitmapFactory.decodeFile(pathName, options);
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
    options.inJustDecodeBounds = false;
    Bitmap src = BitmapFactory.decodeFile(pathName, options);
    int degree = readPictureDegree(pathName);
    return rotateBitmap(src, degree);
}