5

我正在与GeoTiff/PNG文件太大而无法在我的代码中作为一个整体进行处理。

是否有可能在 bitmapfactory 中解码文件的特定区域(例如,由两个 x,y 坐标给出)?没有发现任何看起来相似的东西http://developer.android.com/reference/android/graphics/BitmapFactory.html(Android 的开发者参考)。

谢谢!


根据 kcoppock 的提示,我设置了以下解决方案。

虽然我想知道为什么rect需要初始化Rect(left, bottom, right, top)而不是Rect(left, top, right, bottom)......

示例调用:

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

功能:

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

2 回答 2

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...

希望这可以帮助...

问候!

于 2013-10-04T23:33:24.980 回答
2

你应该调查一下BitmapRegionDecoder。它似乎准确地描述了您正在寻找的用例。

于 2013-10-05T00:10:30.733 回答