Example usage for android.graphics BitmapRegionDecoder decodeRegion

List of usage examples for android.graphics BitmapRegionDecoder decodeRegion

Introduction

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

Prototype

public Bitmap decodeRegion(Rect rect, BitmapFactory.Options options) 

Source Link

Document

Decodes a rectangle region in the image specified by rect.

Usage

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;/*w w  w. ja  va2 s  .  c o  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();/*  www  .  j  a v  a  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));

            //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;
}