Example usage for android.graphics BitmapRegionDecoder getHeight

List of usage examples for android.graphics BitmapRegionDecoder getHeight

Introduction

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

Prototype

public int getHeight() 

Source Link

Document

Returns the original image's height

Usage

From source file:com.aimfire.gallery.service.PhotoProcessor.java

private void saveThumbnail(String sbsPath, String thumbPath) {
    try {//from  ww w.j  ava 2  s. c  o m
        BitmapRegionDecoder decoder = null;
        Bitmap bmp = null;

        decoder = BitmapRegionDecoder.newInstance(sbsPath, false);
        bmp = decoder.decodeRegion(new Rect(0, 0, decoder.getWidth() / 2, decoder.getHeight()), null);

        Bitmap thumb = ThumbnailUtils.extractThumbnail(bmp, MainConsts.THUMBNAIL_SIZE,
                MainConsts.THUMBNAIL_SIZE);

        FileOutputStream fos;
        fos = new FileOutputStream(thumbPath);
        thumb.compress(CompressFormat.JPEG, 50, fos);
        fos.close();

        if (bmp != null) {
            bmp.recycle();
        }

        if (thumb != null) {
            thumb.recycle();
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:org.mariotaku.gallery3d.GLImageLoader.java

protected Result decodeImage(final File file) {
    final String path = file.getAbsolutePath();
    try {/* www.j ava 2  s.  c om*/
        final BitmapRegionDecoder decoder = BitmapRegionDecoder.newInstance(path, false);
        final int width = decoder.getWidth();
        final int height = decoder.getHeight();
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = BitmapUtils.computeSampleSize(mFallbackSize / Math.max(width, height));
        options.inPreferredConfig = Bitmap.Config.RGB_565;
        final Bitmap bitmap = decoder.decodeRegion(new Rect(0, 0, width, height), options);
        return Result.getInstance(decoder, bitmap, Exif.getOrientation(file), file);
    } catch (final IOException e) {
        return decodeBitmapOnly(file);
    }
}

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;//  ww  w.  j  a  va  2 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.jafme.mobile.activity.CropImageActivity.java

private Bitmap decodeRegionCrop(Rect rect, int outWidth, int outHeight) {
    // Release memory now
    clearImageView();/*from  w  w  w . jav a  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));

            // 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.futurologeek.smartcrossing.crop.CropImageActivity.java

private Bitmap decodeRegionCrop(Rect rect, int outWidth, int outHeight) {
    // Release memory now
    clearImageView();/* w  w w.j a va2 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;
}