【发布时间】:2013-10-12 02:22:01
【问题描述】:
我正在处理 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);
}
【问题讨论】:
-
看起来你明白了。 :) 对 Rect 初始化很感兴趣。这是否记录在某处,或者这似乎是您刚刚通过反复试验发现的错误?
标签: java android bitmapfactory bitmapregiondecoder