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:com.wots.lutmaar.CustomView.imagechooser.threads.MediaProcessorThread.java

private String compressAndSaveImage(String fileImage, int scale) throws Exception {
    try {//ww  w . j  av  a  2  s .  c o  m
        ExifInterface exif = new ExifInterface(fileImage);
        String width = exif.getAttribute(ExifInterface.TAG_IMAGE_WIDTH);
        String length = exif.getAttribute(ExifInterface.TAG_IMAGE_LENGTH);
        int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
        int rotate = 0;
        if (BuildConfig.DEBUG) {
            Log.i(TAG, "Before: " + width + "x" + length);
        }

        switch (orientation) {
        case ExifInterface.ORIENTATION_ROTATE_270:
            rotate = -90;
            break;
        case ExifInterface.ORIENTATION_ROTATE_180:
            rotate = 180;
            break;
        case ExifInterface.ORIENTATION_ROTATE_90:
            rotate = 90;
            break;
        }

        int w = Integer.parseInt(width);
        int l = Integer.parseInt(length);

        int what = w > l ? w : l;

        Options options = new Options();
        if (what > 1500) {
            options.inSampleSize = scale * 4;
        } else if (what > 1000 && what <= 1500) {
            options.inSampleSize = scale * 3;
        } else if (what > 400 && what <= 1000) {
            options.inSampleSize = scale * 2;
        } else {
            options.inSampleSize = scale;
        }
        if (BuildConfig.DEBUG) {
            Log.i(TAG, "Scale: " + (what / options.inSampleSize));
            Log.i(TAG, "Rotate: " + rotate);
        }
        Bitmap bitmap = BitmapFactory.decodeFile(fileImage, options);
        File original = new File(fileImage);
        File file = new File((original.getParent() + File.separator
                + original.getName().replace(".", "_fact_" + scale + ".")));
        FileOutputStream stream = new FileOutputStream(file);
        if (rotate != 0) {
            Matrix matrix = new Matrix();
            matrix.setRotate(rotate);
            bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, false);

        }
        /* if (scale == 1)
        bitmap = Bitmap.createScaledBitmap(bitmap, 240, 260, true);*/
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);

        if (BuildConfig.DEBUG) {
            ExifInterface exifAfter = new ExifInterface(file.getAbsolutePath());
            String widthAfter = exifAfter.getAttribute(ExifInterface.TAG_IMAGE_WIDTH);
            String lengthAfter = exifAfter.getAttribute(ExifInterface.TAG_IMAGE_LENGTH);
            if (BuildConfig.DEBUG) {
                Log.i(TAG, "After: " + widthAfter + "x" + lengthAfter);
            }
        }
        stream.flush();
        stream.close();
        return file.getAbsolutePath();

    } catch (IOException e) {
        e.printStackTrace();
        throw e;
    } catch (Exception e) {
        e.printStackTrace();
        throw new Exception("Corrupt or deleted file???");
    }
}

From source file:com.amytech.android.library.views.imagechooser.threads.MediaProcessorThread.java

private String compressAndSaveImage(String fileImage, int scale) throws Exception {
    try {//  ww w . j a v  a 2s  . c  om
        ExifInterface exif = new ExifInterface(fileImage);
        String width = exif.getAttribute(ExifInterface.TAG_IMAGE_WIDTH);
        String length = exif.getAttribute(ExifInterface.TAG_IMAGE_LENGTH);
        int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
        int rotate = 0;
        if (BuildConfig.DEBUG) {
            Log.i(TAG, "Before: " + width + "x" + length);
        }

        switch (orientation) {
        case ExifInterface.ORIENTATION_ROTATE_270:
            rotate = -90;
            break;
        case ExifInterface.ORIENTATION_ROTATE_180:
            rotate = 180;
            break;
        case ExifInterface.ORIENTATION_ROTATE_90:
            rotate = 90;
            break;
        }

        int w = Integer.parseInt(width);
        int l = Integer.parseInt(length);

        int what = w > l ? w : l;

        Options options = new Options();
        if (what > 1500) {
            options.inSampleSize = scale * 4;
        } else if (what > 1000 && what <= 1500) {
            options.inSampleSize = scale * 3;
        } else if (what > 400 && what <= 1000) {
            options.inSampleSize = scale * 2;
        } else {
            options.inSampleSize = scale;
        }
        if (BuildConfig.DEBUG) {
            Log.i(TAG, "Scale: " + (what / options.inSampleSize));
            Log.i(TAG, "Rotate: " + rotate);
        }
        Bitmap bitmap = BitmapFactory.decodeFile(fileImage, options);
        File original = new File(fileImage);
        File file = new File((original.getParent() + File.separator
                + original.getName().replace(".", "_fact_" + scale + ".")));
        FileOutputStream stream = new FileOutputStream(file);
        if (rotate != 0) {
            Matrix matrix = new Matrix();
            matrix.setRotate(rotate);
            bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, false);
        }
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);

        if (BuildConfig.DEBUG) {
            ExifInterface exifAfter = new ExifInterface(file.getAbsolutePath());
            String widthAfter = exifAfter.getAttribute(ExifInterface.TAG_IMAGE_WIDTH);
            String lengthAfter = exifAfter.getAttribute(ExifInterface.TAG_IMAGE_LENGTH);
            if (BuildConfig.DEBUG) {
                Log.i(TAG, "After: " + widthAfter + "x" + lengthAfter);
            }
        }
        stream.flush();
        stream.close();
        return file.getAbsolutePath();

    } catch (IOException e) {
        e.printStackTrace();
        throw e;
    } catch (Exception e) {
        e.printStackTrace();
        throw new Exception("Corrupt or deleted file???");
    }
}

From source file:com.krayzk9s.imgurholo.services.UploadService.java

private static Bitmap lessResolution(String filePath, int width, int height) {
    BitmapFactory.Options options = new BitmapFactory.Options();

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

    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, width, height);
    BitmapFactory.decodeFile(filePath, options);
    float factor = calculateFactor(options, width, height);

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

    return Bitmap.createScaledBitmap(BitmapFactory.decodeFile(filePath, options),
            (int) Math.floor(options.outWidth * factor), (int) Math.floor(options.outHeight * factor), false);
}

From source file:com.ruesga.rview.misc.BitmapUtils.java

@SuppressWarnings("deprecation")
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;/*from  ww w  .  j av  a  2 s  . c om*/
    options.inDither = true;
    options.inPreferQualityOverSpeed = false;
    options.inPreferredConfig = Bitmap.Config.ARGB_8888;
    // Deprecated, but still valid for KitKat and lower apis
    options.inPurgeable = true;
    options.inInputShareable = true;
    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 != null && !out.equals(bitmap)) {
        bitmap.recycle();
    }
    return out;
}

From source file:com.ibuildapp.romanblack.FanWallPlugin.data.Statics.java

/**
 * Sets the downloaded attached image.//from   ww w  .j  av a  2  s.  co  m
 *
 * @param fileName picture file path
 */
public static Bitmap publishPicture(String fileName) {
    Bitmap bitmap = null;
    try {

        if (!TextUtils.isEmpty(fileName)) {
            try {
                BitmapFactory.Options opts = new BitmapFactory.Options();
                opts.inJustDecodeBounds = true;
                BitmapFactory.decodeFile(fileName, opts);

                //Find the correct scale value. It should be the power of 2.
                int width = opts.outWidth, height = opts.outHeight;
                int scale = 1;
                while (true) {
                    if (width / 2 <= 150 || height / 2 <= 150) {
                        break;
                    }
                    width /= 2;
                    height /= 2;
                    scale *= 2;
                }
                BitmapFactory.Options opt = new BitmapFactory.Options();
                opt.inSampleSize = scale;

                bitmap = BitmapFactory.decodeFile(fileName, opt);
                int size = 0;
                if (bitmap.getHeight() > bitmap.getWidth()) {
                    size = bitmap.getWidth();
                } else {
                    size = bitmap.getHeight();
                }
                Bitmap output = Bitmap.createBitmap(size, size, Bitmap.Config.RGB_565);
                Canvas canvas = new Canvas(output);

                final int color = 0xff424242;
                final Paint paint = new Paint();
                final Rect rect = new Rect(0, 0, size, size);
                final RectF rectF = new RectF(rect);
                final float roundPx = 0;

                paint.setAntiAlias(true);
                canvas.drawARGB(0, 0, 0, 0);
                paint.setColor(color);
                canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

                paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
                canvas.drawBitmap(bitmap, rect, rect, paint);

                bitmap.recycle();

                return output;
            } catch (Exception e) {
                Log.w("", "");
            }
        }
    } catch (Exception ex) {

    }

    return null;
}

From source file:com.andrew.apollo.cache.ImageFetcher.java

/**
 * Decode and sample down a {@link Bitmap} from a file to the requested
 * width and height.//  w ww  . j  a  va 2s  . co  m
 *
 * @param filename The full path of the file to decode
 * @param reqWidth The requested width of the resulting bitmap
 * @param reqHeight The requested height of the resulting bitmap
 * @return A {@link Bitmap} sampled down from the original with the same
 *         aspect ratio and dimensions that are equal to or greater than the
 *         requested width and height
 */
public static Bitmap decodeSampledBitmapFromFile(final String filename) {

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

    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, DEFAULT_MAX_IMAGE_WIDTH, DEFAULT_MAX_IMAGE_HEIGHT);

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

From source file:com.geekandroid.sdk.pay.utils.Util.java

public static Bitmap extractThumbNail(final String path, final int height, final int width,
        final boolean crop) {
    Assert.assertTrue(path != null && !path.equals("") && height > 0 && width > 0);

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

    try {/* w  ww .j a  va 2 s.  c om*/
        options.inJustDecodeBounds = true;
        Bitmap tmp = BitmapFactory.decodeFile(path, options);
        if (tmp != null) {
            tmp.recycle();
            tmp = null;
        }

        Log.d(TAG, "extractThumbNail: round=" + width + "x" + height + ", crop=" + crop);
        final double beY = options.outHeight * 1.0 / height;
        final double beX = options.outWidth * 1.0 / width;
        Log.d(TAG, "extractThumbNail: extract beX = " + beX + ", beY = " + beY);
        options.inSampleSize = (int) (crop ? (beY > beX ? beX : beY) : (beY < beX ? beX : beY));
        if (options.inSampleSize <= 1) {
            options.inSampleSize = 1;
        }

        // NOTE: out of memory error
        while (options.outHeight * options.outWidth / options.inSampleSize > MAX_DECODE_PICTURE_SIZE) {
            options.inSampleSize++;
        }

        int newHeight = height;
        int newWidth = width;
        if (crop) {
            if (beY > beX) {
                newHeight = (int) (newWidth * 1.0 * options.outHeight / options.outWidth);
            } else {
                newWidth = (int) (newHeight * 1.0 * options.outWidth / options.outHeight);
            }
        } else {
            if (beY < beX) {
                newHeight = (int) (newWidth * 1.0 * options.outHeight / options.outWidth);
            } else {
                newWidth = (int) (newHeight * 1.0 * options.outWidth / options.outHeight);
            }
        }

        options.inJustDecodeBounds = false;

        Log.i(TAG, "bitmap required size=" + newWidth + "x" + newHeight + ", orig=" + options.outWidth + "x"
                + options.outHeight + ", sample=" + options.inSampleSize);
        Bitmap bm = BitmapFactory.decodeFile(path, options);
        if (bm == null) {
            Log.e(TAG, "bitmap decode failed");
            return null;
        }

        Log.i(TAG, "bitmap decoded size=" + bm.getWidth() + "x" + bm.getHeight());
        final Bitmap scale = Bitmap.createScaledBitmap(bm, newWidth, newHeight, true);
        if (scale != null) {
            bm.recycle();
            bm = scale;
        }

        if (crop) {
            final Bitmap cropped = Bitmap.createBitmap(bm, (bm.getWidth() - width) >> 1,
                    (bm.getHeight() - height) >> 1, width, height);
            if (cropped == null) {
                return bm;
            }

            bm.recycle();
            bm = cropped;
            Log.i(TAG, "bitmap croped size=" + bm.getWidth() + "x" + bm.getHeight());
        }
        return bm;

    } catch (final OutOfMemoryError e) {
        Log.e(TAG, "decode bitmap failed: " + e.getMessage());
        options = null;
    }

    return null;
}

From source file:com.ruesga.rview.misc.BitmapUtils.java

@SuppressWarnings("deprecation")
public static Bitmap createUnscaledBitmap(File file, int dstWidth, int dstHeight) {
    // Get the dimensions of the bitmap
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inScaled = false;//from w w  w  .  ja v a2s .c  om
    options.inDither = true;
    options.inPreferQualityOverSpeed = false;
    options.inPreferredConfig = Bitmap.Config.ARGB_8888;
    // Deprecated, but still valid for KitKat and lower apis
    options.inPurgeable = true;
    options.inInputShareable = true;
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(file.getAbsolutePath(), options);

    // Determine how much to scale down the image
    int photoWidth = options.outWidth;
    int photoHeight = options.outHeight;

    // Decode the image file into a Bitmap sized to fill the view
    options.inJustDecodeBounds = false;
    options.inSampleSize = Math.max(Math.round(photoWidth / dstWidth), Math.round(photoHeight / dstHeight));
    return decodeExifBitmap(file, BitmapFactory.decodeFile(file.getAbsolutePath(), options));
}

From source file:free.yhc.feeder.model.Utils.java

/**
 * Decode image from file path(String) or raw data (byte[]).
 * @param image//w  w  w  . j  a v a2s  .  com
 *   Two types are supported.
 *   String for file path / byte[] for raw image data.
 * @param opt
 * @return
 */
private static Bitmap decodeImageRaw(Object image, BitmapFactory.Options opt) {
    if (image instanceof String) {
        return BitmapFactory.decodeFile((String) image, opt);
    } else if (image instanceof byte[]) {
        byte[] data = (byte[]) image;
        return BitmapFactory.decodeByteArray(data, 0, data.length, opt);
    }
    eAssert(false);
    return null;
}

From source file:com.cooliris.mediayemaha.UriTexture.java

public static Bitmap createFromCache(long crc64, int maxResolution) {
    try {/*from   w w  w . j a  va2  s.  co m*/
        String file = null;
        Bitmap bitmap = null;
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inScaled = false;
        options.inPreferredConfig = Bitmap.Config.RGB_565;
        options.inDither = true;
        if (crc64 != 0) {
            file = createFilePathFromCrc64(crc64, maxResolution);
            bitmap = BitmapFactory.decodeFile(file, options);
        }
        return bitmap;
    } catch (Exception e) {
        return null;
    }
}