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 decodeBitmap(File file, int dstWidth, int dstHeight) {
    // First decode with inJustDecodeBounds=true to check dimensions
    final Options options = new Options();
    options.inScaled = false;//w  ww .java 2s . co  m
    options.inPreferredConfig = Bitmap.Config.ARGB_8888;
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(file.getAbsolutePath(), options);

    // Decode the bitmap with inSampleSize set
    options.inSampleSize = calculateBitmapRatio(options, dstWidth, dstHeight);
    options.inJustDecodeBounds = false;
    Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath(), options);
    if (bitmap == null) {
        return null;
    }

    // Test if the bitmap has exif format, and decode properly
    Bitmap out = decodeExifBitmap(file, bitmap);
    if (!out.equals(bitmap)) {
        bitmap.recycle();
    }
    return out;
}

From source file:Main.java

private static Bitmap getThumbnail(String filePath, int targetW, int targetH) {
    Bitmap bitmap = null;//from  ww  w  . j  a v  a 2  s .  c  om

    File file = new File(filePath);
    String fileName = file.getName();
    if (fileName.lastIndexOf(".") < 0) {
        return bitmap;
    }

    String fileExtension = fileName.substring(fileName.lastIndexOf(".") + 1);

    if (fileExtension.equalsIgnoreCase(IMAGE_TYPT_BMP) || fileExtension.equalsIgnoreCase(IMAGE_TYPT_JPG)
            || fileExtension.equalsIgnoreCase(IMAGE_TYPT_JPEG) || fileExtension.equalsIgnoreCase(IMAGE_TYPT_PNG)
            || fileExtension.equalsIgnoreCase(IMAGE_TYPT_GIF)) {

        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(filePath, options);
        int bitmapRealWidth = options.outWidth;
        int bitmapRealHeight = options.outHeight;
        options.outWidth = targetW;
        if (bitmapRealWidth > 0) {
            options.outHeight = bitmapRealHeight * targetW / bitmapRealWidth;
        }

        options.inJustDecodeBounds = false;

        if (targetW > 0) {
            options.inSampleSize = bitmapRealWidth / targetW;
        }

        options.inPurgeable = true;
        options.inInputShareable = true;
        options.inPreferredConfig = Config.ARGB_4444;

        bitmap = BitmapFactory.decodeFile(filePath, options);
    }

    return bitmap;
}

From source file:Main.java

public static Bitmap decodeScaleImage(String path, int targetWidth, int targetHeight) {
    BitmapFactory.Options bitmapOptions = getBitmapOptions(path);
    bitmapOptions.inSampleSize = calculateInSampleSize(bitmapOptions, targetWidth, targetHeight);
    bitmapOptions.inJustDecodeBounds = false;
    Bitmap noRotatingBitmap = BitmapFactory.decodeFile(path, bitmapOptions);
    int degree = readPictureDegree(path);
    Bitmap rotatingBitmap;//from   w  w  w.  j  a va2s.c om
    if (noRotatingBitmap != null && degree != 0) {
        rotatingBitmap = rotatingImageView(degree, noRotatingBitmap);
        noRotatingBitmap.recycle();
        return rotatingBitmap;
    } else {
        return noRotatingBitmap;
    }
}

From source file:Main.java

private static int getInSampleSize(String path, float width, float height) {
    BitmapFactory.Options newOpts = new BitmapFactory.Options();
    newOpts.inJustDecodeBounds = true;/*from w  w  w. j ava 2 s  .c  om*/
    BitmapFactory.decodeFile(path, newOpts);
    int outWidth = newOpts.outWidth;
    int outHeight = newOpts.outHeight;
    return (int) getScale(width, height, outWidth, outHeight);
}

From source file:Main.java

/**
 * Make a bitmap from a given Uri./* w ww. ja v a2 s  .  co m*/
 *
 * @param uri
 */
public static Bitmap makeBitmap(int minSideLength, int maxNumOfPixels, String pathName,
        BitmapFactory.Options options) {
    try {
        if (options == null)
            options = new BitmapFactory.Options();

        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(pathName, options);
        if (options.mCancel || options.outWidth == -1 || options.outHeight == -1) {
            return null;
        }

        options.inSampleSize = computeSampleSize(options, minSideLength, maxNumOfPixels);
        options.inJustDecodeBounds = false;
        //options.inDither = false;
        //options.inPreferredConfig = Bitmap.Config.ARGB_8888;
        return BitmapFactory.decodeFile(pathName, options);
    } catch (OutOfMemoryError ex) {
        Log.e(TAG, "Got oom exception ", ex);
        return null;
    }
}

From source file:Main.java

public static Bitmap decodeSampledBitmapFromAlbumId(Context context, long album_id, int reqWidth,
        int reqHeight) {

    // Get real path from the Uri
    String pathName = getRealPathFromURI(context, album_id);

    // First decode with inJustDecodeBounts = true 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(pathName, options);

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

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

From source file:Main.java

public static Bitmap getDesirableBitmap(String imgPath, int desiredSize) {
    try {/*  w w w . ja  v a2 s  . c o  m*/
        int scale = getDesirableBitmapSampleSize(imgPath, desiredSize);
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = scale;
        Bitmap originalBitmap = BitmapFactory.decodeFile(imgPath, options);

        int rotation = getExifOrientation(imgPath);
        if (rotation != 0) {
            Matrix matrix = new Matrix();
            matrix.postRotate(rotation);
            Bitmap exifBitmap = Bitmap.createBitmap(originalBitmap, 0, 0, originalBitmap.getWidth(),
                    originalBitmap.getHeight(), matrix, true);
            originalBitmap.recycle();
            return exifBitmap;
        }
        return originalBitmap;
    } catch (Exception ex) {
        ex.printStackTrace();
        //TODO: Handle
    } catch (OutOfMemoryError e) {
        e.printStackTrace();
        //Hmmm...what to do...
    }
    return null;
}

From source file:Main.java

public static Bitmap getScaleBitmap(File file, int destW, int destH) {

    BitmapFactory.Options opts = new BitmapFactory.Options();
    opts.inJustDecodeBounds = true;//w w  w. java 2s.  co  m
    BitmapFactory.decodeFile(file.getPath(), opts);

    opts.inSampleSize = getSampleSize(opts.outWidth, opts.outHeight, destW, destH);
    opts.inJustDecodeBounds = false;
    Bitmap bmp = BitmapFactory.decodeFile(file.getPath(), opts);
    return bmp;
}

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}.
 *//*from  w w w. j  ava  2s .  c o m*/
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) {
        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;
}

From source file:Main.java

/**
 * Decodes an image File to Bitmap resizing the image to be <code>inSampleSize</code> times smaller then the original.
 * @param file the image file reference//w w  w  . j ava  2s  .  c om
 * @param inSampleSize how much smaller that the image will be, for example, <code>inSampleSize</code> equals 2 will return an image 1/2 the size of the original
 * @return the resized Bitmap
 */
public static Bitmap decodeSampledBitmapFromFile(File file, int inSampleSize) {
    BitmapFactory.Options options = new BitmapFactory.Options();

    options.inSampleSize = inSampleSize;

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