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

@NonNull
private static Bitmap decodeSampledBitmap(@NonNull final File file, final int reqWidth) {

    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;/* w  ww . j  a v a 2 s.  co  m*/

    BitmapFactory.decodeFile(file.getAbsolutePath(), options);

    options.inJustDecodeBounds = false;
    options.inMutable = true;

    if (options.outWidth != -1 && options.outHeight != -1) {
        final int originalSize = (options.outHeight > options.outWidth) ? options.outWidth : options.outHeight;
        options.inSampleSize = originalSize / reqWidth;
    }

    final Bitmap inBitmap = getBitmapFromReusableSet(options);
    if (inBitmap != null) {
        options.inBitmap = inBitmap;
    }

    return BitmapFactory.decodeFile(file.getAbsolutePath(), options);
}

From source file:Main.java

public static Bitmap createBitmap(String filePath) {
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inSampleSize = 4;/*from  ww w . ja  v a2 s  .c o m*/
    Bitmap b = null;
    try {
        b = BitmapFactory.decodeFile(filePath, options);
    } catch (OutOfMemoryError e) {
        System.gc();
    }
    return b;
}

From source file:Main.java

/**
 * author: liuxu// ww w . j a v a2 s .  c  o m
 * de-sample according to given width and height. if required width or height is
 * smaller than the origin picture's with or height, de-sample it.
 * NOTE: if image quality is your first concern, do not use this method.
 * @param path full path for the picture
 * @param width the required width
 * @param height the required height
 * @param considerExifRotate true and the bitmap will be rotated according to exif (if exists)
 * @return bitmap
 */
public static Bitmap file2bitmap(String path, int width, int height, boolean considerExifRotate) {
    final BitmapFactory.Options options = new BitmapFactory.Options();

    if (width != 0 && height != 0) {
        // decode with inJustDecodeBounds=true to check size
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(path, options);
        // calculate inSampleSize according to the requested size
        options.inSampleSize = calculateInSampleSize(options, width, height);
        options.inJustDecodeBounds = false;
    }

    // decode bitmap with the calculated inSampleSize
    options.inPreferredConfig = Bitmap.Config.RGB_565;
    options.inPurgeable = true;
    options.inInputShareable = true;
    Bitmap bitmap = BitmapFactory.decodeFile(path, options);

    if (considerExifRotate) {
        int exifDegree = getFileExifDegree(path);
        if (exifDegree == 0) {
            return bitmap;
        } else {
            return rotateBitmap(bitmap, exifDegree);
        }
    } else {
        return bitmap;
    }
}

From source file:Main.java

/**
 * Resamples the captured photo to fit the screen for better memory usage.
 *
 * @param context   The application context.
 * @param imagePath The path of the photo to be resampled.
 * @return The resampled bitmap// w w  w.  ja  v a 2 s  . c om
 */
static Bitmap resamplePic(Context context, String imagePath) {

    // Get device screen size information
    DisplayMetrics metrics = new DisplayMetrics();
    WindowManager manager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    manager.getDefaultDisplay().getMetrics(metrics);

    int targetH = metrics.heightPixels;
    int targetW = metrics.widthPixels;

    // Get the dimensions of the original bitmap
    BitmapFactory.Options bmOptions = new BitmapFactory.Options();
    bmOptions.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(imagePath, bmOptions);
    int photoW = bmOptions.outWidth;
    int photoH = bmOptions.outHeight;

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

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

    return BitmapFactory.decodeFile(imagePath);
}

From source file:Main.java

/**
 * Decodes the image file specified by path to full image or it's sub-sampled version.
 * @param path Path of image file/*  w  w  w. jav  a  2  s  .c o m*/
 * @param reqWidth  Width used to sub-sample image
 * @param reqHeight Height used to sub-sample image
 * @return B
 */
public static Bitmap decodeSampledBitmapFromPath(String path, int reqWidth, int reqHeight) {
    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    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 createOriginalBitmap(String filePath) {
    Bitmap b = null;/*from w  ww . j a v  a 2s . c o m*/
    try {
        b = BitmapFactory.decodeFile(filePath, null);
    } catch (OutOfMemoryError e) {
        System.gc();
    }
    return b;
}

From source file:Main.java

/**
 * Decode a given image file with the specified dimensions
 *
 * @param filePath the file resource path
 * @param reqWidth the required width/* ww  w.ja v  a 2 s. c o  m*/
 * @param reqHeight the required height
 * @return the bitmap
 */
public static Bitmap decodeSampledBitmapFromFilePath(String filePath, int reqWidth, int reqHeight) {

    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(filePath, options);

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

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

}

From source file:Main.java

public static BitmapFactory.Options getBounds(String path) {
    BitmapFactory.Options bounds = new BitmapFactory.Options();
    bounds.inJustDecodeBounds = true;/*from www .  j  a  v a2s . co m*/
    BitmapFactory.decodeFile(path, bounds);
    return bounds;
}

From source file:Main.java

/**
 * Method is available if specific dimensions are specified
 * @param path/*from   w w  w  .ja v a 2s . com*/
 * @param destWidth
 * @param destHeight
 * @return
 */
public static Bitmap getScaledBitmap(String path, int destWidth, int destHeight) {
    // Read in the dimensions of the image on disk
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(path, options);

    float srcWidth = options.outWidth;
    float srcHeight = options.outHeight;

    // Figure out how much to scale down by
    int inSampleSize = 1;
    if (srcHeight > destHeight || srcWidth > destWidth) {
        if (srcWidth > srcHeight) {
            inSampleSize = Math.round(srcHeight / destHeight);
        } else {
            inSampleSize = Math.round(srcWidth / destWidth);
        }
    }

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

    //Read in and create final bitmap
    return BitmapFactory.decodeFile(path, options);
}

From source file:Main.java

/**
 * Load the image at {@code imagePath} as a {@link Bitmap}, scaling it to
 * the specified size and preserving the aspect ratio.
 * @param imagePath Path of the image to load.
 * @param width Required width of the resulting {@link Bitmap}.
 * @param height Required height of the resulting {@link Bitmap}.
 * @param fill {@code true} to fill the empty space with transparent color.
 * @param crop {@code true} to crop the image, {@code false} to resize without cutting the image.
 * @return {@link Bitmap} representing the image at {@code imagePath}.
 *//*w w w. ja  v  a  2 s . c  om*/
public static Bitmap loadResizedBitmap(String imagePath, int width, int height, boolean fill, boolean crop) {
    Bitmap retVal;

    BitmapFactory.Options opts = new BitmapFactory.Options();
    opts.inSampleSize = getScale(imagePath, width, height);
    opts.inJustDecodeBounds = false;

    Bitmap image = BitmapFactory.decodeFile(imagePath, opts);

    if (image == null) {
        return null;
    }

    if (image.getWidth() != width || image.getHeight() != height) {
        //Image need to be resized.
        //         int scaledWidth = image.getWidth();
        //         int scaledHeight = image.getHeight();
        //         final float factorWidth = scaledWidth / width;
        //         final float factorHeight = scaledHeight / height;
        //final float factor = (scaledWidth / width) - (scaledHeight / height);
        //         final long factor = (scaledWidth * height) - (scaledHeight * width);
        //         if ((crop && factor > 0) || (factor < 0)) {
        //            scaledHeight = (scaledHeight * width) / scaledWidth;
        //            scaledWidth = width;
        //         } else {
        //            scaledWidth = (scaledWidth * height) / scaledHeight;
        //            scaledHeight = height;
        //         }
        int scaledWidth = (image.getWidth() * height) / image.getHeight();
        int scaledHeight; // = (image.getHeight() * width) / image.getWidth();
        if ((crop && scaledWidth > width) || (!crop && scaledWidth < width)) {
            scaledHeight = height;
        } else {
            scaledWidth = width;
            scaledHeight = (image.getHeight() * width) / image.getWidth();
        }
        //image = Bitmap.createScaledBitmap(image, scaledWidth, scaledHeight, true);

        Rect src = new Rect(0, 0, image.getWidth(), image.getHeight());
        Rect dst = new Rect(0, 0, scaledWidth, scaledHeight);

        if (fill) {
            retVal = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
            dst.offset((width - scaledWidth) / 2, (height - scaledHeight) / 2);
        } else {
            retVal = Bitmap.createBitmap(scaledWidth, scaledHeight, Bitmap.Config.ARGB_8888);
        }
        retVal.eraseColor(Color.TRANSPARENT);

        synchronized (canvas) {
            if (antiAliasPaint == null) {
                antiAliasPaint = new Paint();
                antiAliasPaint.setAntiAlias(true);
                antiAliasPaint.setFilterBitmap(true);
                antiAliasPaint.setDither(true);
            }
            canvas.setBitmap(retVal);
            canvas.drawBitmap(image, src, dst, antiAliasPaint);
        }

        image.recycle();
    } else {
        //No need to scale.
        retVal = image;
    }

    return retVal;
}