android.graphics.BitmapRegionDecoder

Here are the examples of the java api class android.graphics.BitmapRegionDecoder taken from open source projects.

1. Utils#loadMaskedBitmap()

Project: SpotifyTray-Android
File: Utils.java
/**
	 * Takes an inputstream, loads a bitmap optimized of space and then crops it for the view.
	 * @param iStream Inputstream to the image.
	 * @param containerHeight Height of the image holder container. 
	 * @param containerWidth Width of the image holder container.
	 * @return Cropped/masked bitmap.
	 */
public static Bitmap loadMaskedBitmap(InputStream iStream, int containerHeight, int containerWidth) {
    // Load image data before loading the image itself.
    BitmapFactory.Options bmOptions = new BitmapFactory.Options();
    bmOptions.inJustDecodeBounds = true;
    BitmapFactory.decodeStream(iStream, null, bmOptions);
    int photoH = bmOptions.outHeight;
    int photoW = bmOptions.outWidth;
    // Find a suitable scalefactor to load the smaller bitmap, and then set the options.
    int scaleFactor = Math.min(photoH / containerHeight, photoW / containerHeight);
    bmOptions.inJustDecodeBounds = false;
    bmOptions.inSampleSize = scaleFactor;
    bmOptions.inPurgeable = true;
    // Load the square region out of the bitmap.
    Bitmap bmap = null;
    BitmapRegionDecoder decoder;
    try {
        iStream.reset();
        decoder = BitmapRegionDecoder.newInstance(iStream, false);
        bmap = decoder.decodeRegion(new Rect(0, 0, Math.min(photoH, photoW), Math.min(photoH, photoW)), bmOptions);
    } catch (IOException e) {
        e.printStackTrace();
    }
    // Calculate new width of the bitmap based on the width of the container
    int bitmapNewWidth = (bmap.getWidth() * containerWidth) / containerHeight;
    // Produce clipping mask on the canvas and draw the bitmap 
    Bitmap targetBitmap = Bitmap.createBitmap(bitmapNewWidth, bmap.getHeight(), bmap.getConfig());
    Canvas canvas = new Canvas(targetBitmap);
    Path path = new Path();
    path.addCircle(bmap.getWidth() / 2, bmap.getHeight() / 2, bmap.getWidth() / 2, Path.Direction.CCW);
    canvas.clipPath(path);
    canvas.drawBitmap(bmap, 0, 0, null);
    // Retrieve the clipped bitmap and return
    return targetBitmap;
}

2. AsyncBitmapTexture#fractionalDecode()

Project: GearVRf
File: AsyncBitmapTexture.java
/**
     * Use {@link BitmapRegionDecoder} to read the bitmap in smallish slices,
     * and resize each slice so that the target bitmap matches requestedWidth or
     * requestedHeight in at least one dimension.
     */
private static Bitmap fractionalDecode(FractionalDecodeShim shim, Options options, int requestedWidth, int requestedHeight) {
    final int rawWidth = options.outWidth;
    final int rawHeight = options.outHeight;
    final float sampledWidth = (float) rawWidth / options.inSampleSize;
    final float sampledHeight = (float) rawHeight / options.inSampleSize;
    // Use the simple path, if we can/must
    if (requestedWidth == 0 || //
    requestedHeight == 0 || sampledWidth <= Math.abs(requestedWidth) || sampledHeight <= Math.abs(requestedHeight)) {
        if (VERBOSE_DECODE) {
            Log.d(TAG, "Can't use slice decoder: sampledWidth = %.0f, requestedWidth = %d; sampledHeight = %.0f, requestedHeight = %d", sampledWidth, requestedWidth, sampledHeight, requestedHeight);
        }
        return shim.decode(options);
    }
    // We must use a BitmapRegionDecoder to read and resize 'slices' of the
    // input
    BitmapRegionDecoder decoder = shim.newRegionDecoder();
    try {
        float scale = Math.max(scale(requestedWidth, sampledWidth), scale(requestedHeight, sampledHeight));
        int scaledSliceWidth = roundUp(sampledWidth * scale);
        int slices = (decoder == null) ? 1 : roundUp(sampledHeight / (SLICE_SIZE / sampledWidth));
        int sliceRows = (int) (rawHeight / slices);
        float scaledSliceRows = sliceRows / options.inSampleSize * scale;
        Bitmap result = Bitmap.createBitmap((int) (sampledWidth * scale), (int) (sampledHeight * scale), Config.ARGB_8888);
        Canvas canvas = new Canvas(result);
        Paint paint = new Paint(Paint.FILTER_BITMAP_FLAG);
        // Rect decode, uses 'raw' coordinates
        Rect decode = new Rect(0, 0, rawWidth, sliceRows);
        // RectF target, uses scaled coordinates
        RectF target = new RectF(0, 0, scaledSliceWidth, scaledSliceRows);
        Bitmap slice = options.inBitmap = null;
        boolean hasAlpha = false;
        for (int index = 0; index < slices; ++index) {
            slice = //
            options.inBitmap = //
            (decoder == null) ? //
            shim.decode(options) : decoder.decodeRegion(decode, options);
            hasAlpha |= slice.hasAlpha();
            canvas.drawBitmap(slice, null, target, paint);
            decode.offset(0, sliceRows);
            target.offset(0, scaledSliceRows);
        }
        slice.recycle();
        // log(TAG,
        // "fractionalDecode: result.hasAlpha() == %b, slices' hasAlpha == %b",
        // result.hasAlpha(), hasAlpha);
        result.setHasAlpha(hasAlpha);
        return result;
    } finally {
        if (decoder != null) {
            decoder.recycle();
        }
    }
}

3. BitmapUtils#decodeSampledBitmapRegion()

Project: Android-Image-Cropper
File: BitmapUtils.java
/**
     * Decode specific rectangle bitmap from stream using sampling to get bitmap with the requested limit.
     */
private static Bitmap decodeSampledBitmapRegion(Context context, Uri uri, Rect rect, int reqWidth, int reqHeight) {
    InputStream stream = null;
    BitmapRegionDecoder decoder = null;
    try {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = calculateInSampleSizeByReqestedSize(rect.width(), rect.height(), reqWidth, reqHeight);
        stream = context.getContentResolver().openInputStream(uri);
        decoder = BitmapRegionDecoder.newInstance(stream, false);
        do {
            try {
                return decoder.decodeRegion(rect, options);
            } catch (OutOfMemoryError e) {
                options.inSampleSize *= 2;
            }
        } while (options.inSampleSize <= 512);
    } catch (Exception e) {
        throw new RuntimeException("Failed to load sampled bitmap: " + uri + "\r\n" + e.getMessage(), e);
    } finally {
        closeSafe(stream);
        if (decoder != null) {
            decoder.recycle();
        }
    }
    return null;
}