FileDocCategorySizeDatePackage
BitmapRegionDecoder.javaAPI DocAndroid 5.1 API11961Thu Mar 12 22:22:30 GMT 2015android.graphics

BitmapRegionDecoder

public final class BitmapRegionDecoder extends Object
BitmapRegionDecoder can be used to decode a rectangle region from an image. BitmapRegionDecoder is particularly useful when an original image is large and you only need parts of the image.

To create a BitmapRegionDecoder, call newInstance(...). Given a BitmapRegionDecoder, users can call decodeRegion() repeatedly to get a decoded Bitmap of the specified region.

Fields Summary
private long
mNativeBitmapRegionDecoder
private boolean
mRecycled
private final Object
mNativeLock
Constructors Summary
private BitmapRegionDecoder(long decoder)

        mNativeBitmapRegionDecoder = decoder;
        mRecycled = false;
    
Methods Summary
private voidcheckRecycled(java.lang.String errorMessage)
Called by methods that want to throw an exception if the region decoder has already been recycled.

        if (mRecycled) {
            throw new IllegalStateException(errorMessage);
        }
    
public BitmapdecodeRegion(Rect rect, BitmapFactory.Options options)
Decodes a rectangle region in the image specified by rect.

param
rect The rectangle that specified the region to be decode.
param
options null-ok; Options that control downsampling. inPurgeable is not supported.
return
The decoded bitmap, or null if the image data could not be decoded.

        synchronized (mNativeLock) {
            checkRecycled("decodeRegion called on recycled region decoder");
            if (rect.right <= 0 || rect.bottom <= 0 || rect.left >= getWidth()
                    || rect.top >= getHeight())
                throw new IllegalArgumentException("rectangle is outside the image");
            return nativeDecodeRegion(mNativeBitmapRegionDecoder, rect.left, rect.top,
                    rect.right - rect.left, rect.bottom - rect.top, options);
        }
    
protected voidfinalize()

        try {
            recycle();
        } finally {
            super.finalize();
        }
    
public intgetHeight()
Returns the original image's height

        synchronized (mNativeLock) {
            checkRecycled("getHeight called on recycled region decoder");
            return nativeGetHeight(mNativeBitmapRegionDecoder);
        }
    
public intgetWidth()
Returns the original image's width

        synchronized (mNativeLock) {
            checkRecycled("getWidth called on recycled region decoder");
            return nativeGetWidth(mNativeBitmapRegionDecoder);
        }
    
public final booleanisRecycled()
Returns true if this region decoder has been recycled. If so, then it is an error to try use its method.

return
true if the region decoder has been recycled

        return mRecycled;
    
private static native voidnativeClean(long lbm)

private static native BitmapnativeDecodeRegion(long lbm, int start_x, int start_y, int width, int height, BitmapFactory.Options options)

private static native intnativeGetHeight(long lbm)

private static native intnativeGetWidth(long lbm)

private static native android.graphics.BitmapRegionDecodernativeNewInstance(byte[] data, int offset, int length, boolean isShareable)

private static native android.graphics.BitmapRegionDecodernativeNewInstance(java.io.FileDescriptor fd, boolean isShareable)

private static native android.graphics.BitmapRegionDecodernativeNewInstance(java.io.InputStream is, byte[] storage, boolean isShareable)

private static native android.graphics.BitmapRegionDecodernativeNewInstance(long asset, boolean isShareable)

public static android.graphics.BitmapRegionDecodernewInstance(byte[] data, int offset, int length, boolean isShareable)
Create a BitmapRegionDecoder from the specified byte array. Currently only the JPEG and PNG formats are supported.

param
data byte array of compressed image data.
param
offset offset into data for where the decoder should begin parsing.
param
length the number of bytes, beginning at offset, to parse
param
isShareable If this is true, then the BitmapRegionDecoder may keep a shallow reference to the input. If this is false, then the BitmapRegionDecoder will explicitly make a copy of the input data, and keep that. Even if sharing is allowed, the implementation may still decide to make a deep copy of the input data. If an image is progressively encoded, allowing sharing may degrade the decoding speed.
return
BitmapRegionDecoder, or null if the image data could not be decoded.
throws
IOException if the image format is not supported or can not be decoded.


                                                                                                                                                                                                                                                                                       
        
                    
        if ((offset | length) < 0 || data.length < offset + length) {
            throw new ArrayIndexOutOfBoundsException();
        }
        return nativeNewInstance(data, offset, length, isShareable);
    
public static android.graphics.BitmapRegionDecodernewInstance(java.io.FileDescriptor fd, boolean isShareable)
Create a BitmapRegionDecoder from the file descriptor. The position within the descriptor will not be changed when this returns, so the descriptor can be used again as is. Currently only the JPEG and PNG formats are supported.

param
fd The file descriptor containing the data to decode
param
isShareable If this is true, then the BitmapRegionDecoder may keep a shallow reference to the input. If this is false, then the BitmapRegionDecoder will explicitly make a copy of the input data, and keep that. Even if sharing is allowed, the implementation may still decide to make a deep copy of the input data. If an image is progressively encoded, allowing sharing may degrade the decoding speed.
return
BitmapRegionDecoder, or null if the image data could not be decoded.
throws
IOException if the image format is not supported or can not be decoded.

        return nativeNewInstance(fd, isShareable);
    
public static android.graphics.BitmapRegionDecodernewInstance(java.io.InputStream is, boolean isShareable)
Create a BitmapRegionDecoder from an input stream. The stream's position will be where ever it was after the encoded data was read. Currently only the JPEG and PNG formats are supported.

param
is The input stream that holds the raw data to be decoded into a BitmapRegionDecoder.
param
isShareable If this is true, then the BitmapRegionDecoder may keep a shallow reference to the input. If this is false, then the BitmapRegionDecoder will explicitly make a copy of the input data, and keep that. Even if sharing is allowed, the implementation may still decide to make a deep copy of the input data. If an image is progressively encoded, allowing sharing may degrade the decoding speed.
return
BitmapRegionDecoder, or null if the image data could not be decoded.
throws
IOException if the image format is not supported or can not be decoded.

Prior to {@link android.os.Build.VERSION_CODES#KITKAT}, if {@link InputStream#markSupported is.markSupported()} returns true, is.mark(1024) would be called. As of {@link android.os.Build.VERSION_CODES#KITKAT}, this is no longer the case.

        if (is instanceof AssetManager.AssetInputStream) {
            return nativeNewInstance(
                    ((AssetManager.AssetInputStream) is).getNativeAsset(),
                    isShareable);
        } else {
            // pass some temp storage down to the native code. 1024 is made up,
            // but should be large enough to avoid too many small calls back
            // into is.read(...).
            byte [] tempStorage = new byte[16 * 1024];
            return nativeNewInstance(is, tempStorage, isShareable);
        }
    
public static android.graphics.BitmapRegionDecodernewInstance(java.lang.String pathName, boolean isShareable)
Create a BitmapRegionDecoder from a file path. Currently only the JPEG and PNG formats are supported.

param
pathName complete path name for the file to be decoded.
param
isShareable If this is true, then the BitmapRegionDecoder may keep a shallow reference to the input. If this is false, then the BitmapRegionDecoder will explicitly make a copy of the input data, and keep that. Even if sharing is allowed, the implementation may still decide to make a deep copy of the input data. If an image is progressively encoded, allowing sharing may degrade the decoding speed.
return
BitmapRegionDecoder, or null if the image data could not be decoded.
throws
IOException if the image format is not supported or can not be decoded.

        BitmapRegionDecoder decoder = null;
        InputStream stream = null;

        try {
            stream = new FileInputStream(pathName);
            decoder = newInstance(stream, isShareable);
        } finally {
            if (stream != null) {
                try {
                    stream.close();
                } catch (IOException e) {
                    // do nothing here
                }
            }
        }
        return decoder;
    
public voidrecycle()
Frees up the memory associated with this region decoder, and mark the region decoder as "dead", meaning it will throw an exception if decodeRegion(), getWidth() or getHeight() is called.

This operation cannot be reversed, so it should only be called if you are sure there are no further uses for the region decoder. This is an advanced call, and normally need not be called, since the normal GC process will free up this memory when there are no more references to this region decoder.

        synchronized (mNativeLock) {
            if (!mRecycled) {
                nativeClean(mNativeBitmapRegionDecoder);
                mRecycled = true;
            }
        }