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 decodeInSampleSize(String imagePath, 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  . ja  v  a  2s  .  co m*/
    BitmapFactory.decodeFile(imagePath, options);

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

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

From source file:Main.java

public static Bitmap getBitmap(String path, BitmapFactory.Options options, float maxH, float maxW) {
    Bitmap cameraBitmap = null;/*from w  ww.  ja v  a 2s .com*/
    int be = 1;
    float maxh = (int) ((maxH / 100.0) + 0.5) * 100;
    float maxw = (int) ((maxW / 100.0) + 0.5) * 100;
    Log.e("maxh", maxh + "");
    Log.e("maxw", maxw + "");
    options.inJustDecodeBounds = true;
    cameraBitmap = BitmapFactory.decodeFile(path, options);
    options.inJustDecodeBounds = false;
    int h = options.outHeight;
    int w = options.outWidth;
    Log.e("h", h + "");
    Log.e("w", w + "");
    if (h > maxh || w > maxw) {
        int beh = (int) ((h / maxh) + 0.5);
        int bew = (int) ((w / maxw) + 0.5);
        Log.e("beh", beh + "");
        Log.e("bew", bew + "");
        if (beh > bew) {
            be = beh;
        } else {
            be = bew;
        }
    }
    options.inSampleSize = be;
    cameraBitmap = BitmapFactory.decodeFile(path, options);
    return cameraBitmap;
}

From source file:Main.java

public static synchronized Bitmap decodeSampledBitmapFromFile(String path) {

    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inSampleSize = 1;//  ww w  . j a  va2 s . c  o  m
    options.outMimeType = "image/jpeg";
    options.inDither = false;
    options.inPurgeable = true;
    options.inInputShareable = true;
    options.inPreferredConfig = Bitmap.Config.ARGB_8888;
    options.inTempStorage = new byte[256];

    return BitmapFactory.decodeFile(path, options);
}

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  w  ww. j a v a  2 s .  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 = 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 bitmapResult = BitmapFactory.decodeFile(path, options);
}

From source file:Main.java

public static Bitmap loadThumb(Context context, String thumbName) {
    if (thumbName == null || thumbName.length() <= 0)
        return null;
    File pictureFile = getOutputMediaFile(context, thumbName);
    try {//from www  .j ava2 s .com
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inPreferredConfig = Bitmap.Config.ARGB_8888;
        Bitmap bitmap = BitmapFactory.decodeFile(pictureFile.getAbsolutePath(), options);
        return bitmap;
    } catch (Exception e) {
        return null;
    }
}

From source file:Main.java

public static Bitmap decodeSampledBitmapFromUri(Uri uri, int reqWidth, int reqHeight) {

    System.out.println("k9d3 sampling " + uri.getPath() + " for width=" + reqWidth + " height=" + reqHeight);
    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;//from   w w  w .jav a 2 s.  c  o m
    BitmapFactory.decodeFile(uri.getPath(), options);

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

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

From source file:Main.java

public static Bitmap decode(File file, int targetWidth, int targetHeight) {

    if (file == null || !file.exists() || file.length() == 0) {

        return null;
    }//ww w  .  j  a  va  2  s  .  c o  m

    String pathName = file.getPath();
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(pathName, options);

    /*
     * If set to a value > 1, requests the decoder to subsample the original
     * image, returning a smaller image to save memory. The sample size is
     * the number of pixels in either dimension that correspond to a single
     * pixel in the decoded bitmap. For example, inSampleSize == 4 returns
     * an image that is 1/4 the width/height of the original, and 1/16 the
     * number of pixels. Any value <= 1 is treated the same as 1. Note: the
     * decoder uses a final value based on powers of 2, any other value will
     * be rounded down to the nearest power of 2.
     */
    options.inSampleSize = computeImageSampleSize(options, targetWidth, targetHeight);

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

}

From source file:Main.java

public static BitmapFactory.Options getBitmapSize(String strImagePath) {
    //==========================================
    // Loaded the temporary bitmap for getting size.
    //==========================================
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;//from   w w  w  .j  a  v  a  2s  . c  o  m
    //Bitmap photo = BitmapFactory.decodeFile(strPath, options);
    BitmapFactory.decodeFile(strImagePath, options);

    return options;
}

From source file:Main.java

/**
 * This will return a bitmap that is loaded and appropriately scaled from
 * the filePath parameter./*from   w  w w  . j  av a  2  s  .c om*/
 */
public static Bitmap decodeSampledBitmapFromFile(String pathName, int width, int height) {

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

    // If either width or height is passed in as 0, then use the actual
    // stored image dimension.
    if (width == 0) {
        width = options.outWidth;
    }
    if (height == 0) {
        height = options.outHeight;
    }

    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, width, height);

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

From source file:Main.java

public static String scaleImageFile(String filepath, int targetSize) {
    BitmapFactory.Options options = new BitmapFactory.Options();
    File file = new File(filepath);
    float scaleFactor = file.length() / targetSize;
    options.inSampleSize = Math.round(scaleFactor);
    Bitmap bitmap = BitmapFactory.decodeFile(filepath, options);

    int dotPos = filepath.lastIndexOf('.');
    String newFilePath = String.format("%s_scaled.%s", filepath.substring(0, dotPos),
            filepath.substring(dotPos + 1, filepath.length()));
    FileOutputStream fos = null;//from  w w  w.j  a va2 s  .  co  m
    try {
        fos = new FileOutputStream(newFilePath);
        if (!bitmap.compress(CompressFormat.JPEG, 90, fos)) {
            newFilePath = null;
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        newFilePath = null;
    }
    bitmap.recycle();
    return newFilePath;
}