Decode specific image areas in Bitmapfactory?

I work with files GeoTiff/PNGtoo large to handle in general in my code.

Is it possible to decode certain areas (for example, given by two x, y coordinates) of a file in bitmapfactory? We did not find anything similar to http://developer.android.com/reference/android/graphics/BitmapFactory.html(link for Android developers).

Thanks!


With the hint of kcoppock, I installed the following solution.

Although I wonder why rectyou need to initialize with Rect(left, bottom, right, top)instead Rect(left, top, right, bottom)...

Call example:

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

Functions:

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);

}
+4
source share
2 answers
+2

, " ", , "" , , , :

        Bitmap bmpWithArea = Bitmap.createBitmap(widthDesired, heightDesired, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bmpWithArea);
        Rect area = new Rect(arealeft, areatop, arearight, areabottom);
        Rect actualSize = new Rect(0, 0, widthDesired, heightDesired);
        canvas.drawBitmap(bitmapWithAreaYouWantToGet, area, actual, paintIfAny);

        //And done, starting from this line "bmpWithArea" has the bmp that you wanted, you can assign it to ImageView and use it as regular bmp...

, ...

!

+2

All Articles