Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Decode specific areas of image in Bitmapfactory?

I'm working with GeoTiff/PNG files too large for handling as a whole in my code.

Is there any possibility to decode specific areas (e.g. given by two x,y coordinates) of a file in bitmapfactory? Haven't found anything looking similar at http://developer.android.com/reference/android/graphics/BitmapFactory.html(Android's developer reference).

Thanks!


With kcoppock's hint I've set up the following solution.

Though I'm wondering why rect needs to be initialized by Rect(left, bottom, right, top) instead of Rect(left, top, right, bottom)...

Example call:

Bitmap myBitmap = loadBitmapRegion(context, R.drawable.heightmap,
    0.08f, 0.32f, 0.13f, 0.27f);

Function:

public static Bitmap loadBitmapRegion(
    Context context, int resourceID,
    float regionLeft, float regionTop,
    float regionRight, float regionBottom) {

    // Get input stream for resource
    InputStream is = context.getResources().openRawResource(resourceID);

    // Set options
    BitmapFactory.Options opt = new BitmapFactory.Options();
    //opt.inPreferredConfig = Bitmap.Config.ARGB_8888; //standard

    // Create decoder
    BitmapRegionDecoder decoder = null;
    try {
        decoder = BitmapRegionDecoder.newInstance(is, false);
    } catch (IOException e) {
        e.printStackTrace();
    }

    // Get resource dimensions
    int h = decoder.getHeight();
    int w = decoder.getWidth();

    // Set region to decode
    Rect region = new Rect(
            Math.round(regionLeft*w), Math.round(regionBottom*h),
            Math.round(regionRight*w), Math.round(regionTop*h));

    // Return bitmap
    return decoder.decodeRegion(region, opt);

}
like image 241
Peter K. Avatar asked Oct 02 '22 18:10

Peter K.


1 Answers

You should look into BitmapRegionDecoder. It seems to describe exactly the use case that you are looking for.

like image 131
Kevin Coppock Avatar answered Oct 11 '22 13:10

Kevin Coppock