Pet*_* K. 5 java android bitmapfactory bitmapregiondecoder
我正在处理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);
Run Code Online (Sandbox Code Playgroud)
功能:
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);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
921 次 |
| 最近记录: |