我有一个非常大的图像文件,比如9000x9000。由于堆大小的限制,我无法将位图加载到内存中。但是,我只需要显示此位图的一小部分,例如矩形宽度= 100-200和高度= 200-400(子位图的结果大小为100x200)。
我该如何从文件中检索此位图?
注意:我不想在100x200图像中失去质量。
谢谢!
我该如何从文件中检索此位图?
注意:我不想在100x200图像中失去质量。
谢谢!
是否有可能有一个解决方案呢?例如,BitmapRegionDecoder 可以实现。它适用于API10及以上版本...
使用方法:
BitmapRegionDecoder.newInstance(...).decodeRegion(...)
使用RapidDecoder可以轻松完成。
我实际上生成了一个9000x9000大小的png文件,文件大小约为80MB,而200x400大小的区域成功加载。
import rapid.decoder.BitmapDecoder;
Bitmap bitmap = BitmapDecoder.from("big-image.png")
.region(145, 192, 145 + 200, 192 + 400)
.decode();
imageView.setImageBitmap(bitmap);
该功能适用于Android 2.2及以上版本。
public static Bitmap decodeStream (InputStream is, Rect outPadding, BitmapFactory.Options opts)
Try this code:
public static Bitmap decodeSampledBitmapFromFile(String path, int reqWidth, int reqHeight) {
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path, options);
final int height = options.outHeight;
final int width = options.outWidth;
options.inPreferredConfig = Bitmap.Config.RGB_565;
int inSampleSize = 1;
if (height > reqHeight) {
inSampleSize = Math.round((float) height / (float) reqHeight);
}
int expectedWidth = width / inSampleSize;
if (expectedWidth > reqWidth) {
inSampleSize = Math.round((float) width / (float) reqWidth);
}
options.inSampleSize = inSampleSize;
options.inJustDecodeBounds = false;
return BitmapFactory.decodeFile(path, options);
}
试试这段代码:
private Bitmap decodeFile(File f) {
Bitmap b = null;
try {
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
FileInputStream fis = new FileInputStream(f);
b=Bitmap.createBitmap(BitmapFactory.decodeStream(fis, null, o), 100, 200, 200, 400, null, null);
fis.close();
} catch (IOException e) {
}
return b;
}
我不确定,但这可能会给你一些想法
我认为你做不到。即使在PC上,我也看不出你如何在不加载整个图像的情况下完成这项任务:例如PNG等大多数图像格式都将像素数据压缩,因此您至少需要解压IDAT块才能开始进行其他操作,这基本上会解码整个图像。
如果是我,我会尝试让服务器为我完成这项任务。你从哪里获取这张图片?不是从服务器吗?那么尝试发起一个WS请求,以获取所需的图像部分。如果图像不来自服务器,您仍然可以将其发送到您的服务器,以仅获取您想要的图像部分。