Example usage for android.graphics BitmapRegionDecoder newInstance

List of usage examples for android.graphics BitmapRegionDecoder newInstance

Introduction

In this page you can find the example usage for android.graphics BitmapRegionDecoder newInstance.

Prototype

public static BitmapRegionDecoder newInstance(String pathName, boolean isShareable) throws IOException 

Source Link

Document

Create a BitmapRegionDecoder from a file path.

Usage

From source file:com.jafme.mobile.activity.CropImageActivity.java

private Bitmap decodeRegionCrop(Rect rect, int outWidth, int outHeight) {
    // Release memory now
    clearImageView();//from w w w  . j a  va  2  s.  com

    InputStream is = null;
    Bitmap croppedImage = null;
    try {
        is = getContentResolver().openInputStream(sourceUri);
        BitmapRegionDecoder decoder = BitmapRegionDecoder.newInstance(is, false);
        final int width = decoder.getWidth();
        final int height = decoder.getHeight();

        if (exifRotation != 0) {
            // Adjust crop area to account for image rotation
            Matrix matrix = new Matrix();
            matrix.setRotate(-exifRotation);

            RectF adjusted = new RectF();
            matrix.mapRect(adjusted, new RectF(rect));

            // Adjust to account for origin at 0,0
            adjusted.offset(adjusted.left < 0 ? width : 0, adjusted.top < 0 ? height : 0);
            rect = new Rect((int) adjusted.left, (int) adjusted.top, (int) adjusted.right,
                    (int) adjusted.bottom);
        }

        try {
            croppedImage = decoder.decodeRegion(rect, new BitmapFactory.Options());

            //  ?  

            if (exifRotation != 0) {

                final Matrix matrix = new Matrix();
                matrix.setRotate(exifRotation);
                croppedImage = Bitmap.createBitmap(croppedImage, 0, 0, croppedImage.getWidth(),
                        croppedImage.getHeight(), matrix, true);

                exifRotation = 0;
            }

            int croppedWidth = croppedImage.getWidth();
            int croppedHeight = croppedImage.getHeight();
            if (croppedWidth > outWidth || croppedHeight > outHeight) {
                Matrix matrix = new Matrix();
                matrix.postScale((float) outWidth / croppedWidth, (float) outHeight / croppedHeight);
                croppedImage = Bitmap.createBitmap(croppedImage, 0, 0, croppedWidth, croppedHeight, matrix,
                        true);
            }

        } catch (IllegalArgumentException e) {
            // Rethrow with some extra information
            throw new IllegalArgumentException("Rectangle " + rect + " is outside of the image (" + width + ","
                    + height + "," + exifRotation + ")", e);
        }

    } catch (IOException e) {
        Log.e(LOG_TAG, "Error cropping image: " + e.getMessage(), e);
        finish();
    } catch (OutOfMemoryError e) {
        Log.e(LOG_TAG, "OOM cropping image: " + e.getMessage(), e);
        setResultException(e);
    } finally {
        CropUtil.closeSilently(is);
    }
    return croppedImage;
}

From source file:com.android.contacts.DynamicShortcuts.java

private Bitmap decodeStreamForShortcut(InputStream stream) throws IOException {
    final BitmapRegionDecoder bitmapDecoder = BitmapRegionDecoder.newInstance(stream, false);

    final int sourceWidth = bitmapDecoder.getWidth();
    final int sourceHeight = bitmapDecoder.getHeight();

    final int iconMaxWidth = mShortcutManager.getIconMaxWidth();
    final int iconMaxHeight = mShortcutManager.getIconMaxHeight();

    final int sampleSize = Math.min(BitmapUtil.findOptimalSampleSize(sourceWidth, mIconSize),
            BitmapUtil.findOptimalSampleSize(sourceHeight, mIconSize));
    final BitmapFactory.Options opts = new BitmapFactory.Options();
    opts.inSampleSize = sampleSize;/*from   www  . ja  v  a  2  s .co  m*/

    final int scaledWidth = sourceWidth / opts.inSampleSize;
    final int scaledHeight = sourceHeight / opts.inSampleSize;

    final int targetWidth = Math.min(scaledWidth, iconMaxWidth);
    final int targetHeight = Math.min(scaledHeight, iconMaxHeight);

    // Make it square.
    final int targetSize = Math.min(targetWidth, targetHeight);

    // The region is defined in the coordinates of the source image then the sampling is
    // done on the extracted region.
    final int prescaledXOffset = ((scaledWidth - targetSize) * opts.inSampleSize) / 2;
    final int prescaledYOffset = ((scaledHeight - targetSize) * opts.inSampleSize) / 2;

    final Bitmap bitmap = bitmapDecoder.decodeRegion(new Rect(prescaledXOffset, prescaledYOffset,
            sourceWidth - prescaledXOffset, sourceHeight - prescaledYOffset), opts);
    bitmapDecoder.recycle();

    if (!BuildCompat.isAtLeastO()) {
        return BitmapUtil.getRoundedBitmap(bitmap, targetSize, targetSize);
    }

    return bitmap;
}

From source file:com.futurologeek.smartcrossing.crop.CropImageActivity.java

private Bitmap decodeRegionCrop(Rect rect, int outWidth, int outHeight) {
    // Release memory now
    clearImageView();//from   w  w  w  .  ja va 2  s.  c  om

    InputStream is = null;
    Bitmap croppedImage = null;
    try {
        is = getContentResolver().openInputStream(sourceUri);
        BitmapRegionDecoder decoder = BitmapRegionDecoder.newInstance(is, false);
        final int width = decoder.getWidth();
        final int height = decoder.getHeight();

        if (exifRotation != 0) {
            // Adjust crop area to account for image rotation
            Matrix matrix = new Matrix();
            matrix.setRotate(-exifRotation);

            RectF adjusted = new RectF();
            matrix.mapRect(adjusted, new RectF(rect));

            //if the cutting box are rectangle( outWidth != outHeight ),and the exifRotation is 90 or 270,
            //the outWidth and outHeight showld be interchanged
            if (exifRotation == 90 || exifRotation == 270) {
                int temp = outWidth;
                outWidth = outHeight;
                outHeight = temp;
            }

            // Adjust to account for origin at 0,0
            adjusted.offset(adjusted.left < 0 ? width : 0, adjusted.top < 0 ? height : 0);
            rect = new Rect((int) adjusted.left, (int) adjusted.top, (int) adjusted.right,
                    (int) adjusted.bottom);
        }

        try {
            croppedImage = decoder.decodeRegion(rect, new BitmapFactory.Options());
            if (rect.width() > outWidth || rect.height() > outHeight) {
                Matrix matrix = new Matrix();
                matrix.postScale((float) outWidth / rect.width(), (float) outHeight / rect.height());

                //if the picture's exifRotation !=0 ,they should be rotate to 0 degrees
                matrix.postRotate(exifRotation);
                croppedImage = Bitmap.createBitmap(croppedImage, 0, 0, croppedImage.getWidth(),
                        croppedImage.getHeight(), matrix, true);
            } else {
                //if the picture need not to be scale, they also neet to be rotate to 0 degrees
                Matrix matrix = new Matrix();
                matrix.postRotate(exifRotation);
                croppedImage = Bitmap.createBitmap(croppedImage, 0, 0, croppedImage.getWidth(),
                        croppedImage.getHeight(), matrix, true);
            }
        } catch (IllegalArgumentException e) {
            // Rethrow with some extra information
            throw new IllegalArgumentException("Rectangle " + rect + " is outside of the image (" + width + ","
                    + height + "," + exifRotation + ")", e);
        }

    } catch (IOException e) {
        Log.e("Error cropping image: " + e.getMessage(), e);
        finish();
    } catch (OutOfMemoryError e) {
        Log.e("OOM cropping image: " + e.getMessage(), e);
        setResultException(e);
    } finally {
        CropUtil.closeSilently(is);
    }
    return croppedImage;
}