[英]Allocating big bitmap for renderscript (android)
我尝试在android renderscript中制作简单的图像过滤器,它确实适用于小图像。 但是,我得到out of memory error
与例如用相机拍摄的照片一样大的照片out of memory error
(尽管对于小图像来说,一切都很好)。 我知道我的代码有点糟糕(通常是从此处复制),因此,有关如何在大位图上进行渲染脚本计算的任何技巧都值得赞赏。.这是调用rs的代码:
RenderScript rs = RenderScript.create(this);
Allocation allocIn = Allocation.createFromBitmap(rs, bmp);
Allocation allocOut = Allocation.createTyped(rs, allocIn.getType());
ScriptC_sample sc = new ScriptC_sample(rs, getResources(), R.raw.sample);
sc.set_in(allocIn);
sc.set_out(allocOut);
sc.forEach_root(allocIn, allocOut);
Bitmap bmpOut = Bitmap.createBitmap(bmp.getWidth(), bmp.getHeight(), bmp.getConfig());
allocOut.copyTo(bmpOut);
imgView.setImageBitmap(bmpOut);
我认为这与rendescript文件本身有关:
rs_allocation in;
void root(const uchar4* v_in, uchar4* v_out, const void* usrData,
uint32_t x, uint32_t y) {
float4 curPixel = rsUnpackColor8888(*v_in);
// ... computations
*v_out = rsPackColorTo8888(curPixel.r, curPixel.g, curPixel.b, curPixel.a);
}
我确实知道我无法真正将这么大的位图加载到内存中(虽然我可以将文件加载到imageView中吗?),而且我以前使用过inJustDecodeBounds来处理它。使用它,并且我不想调整位图的大小(我想处理原始文件并保存相同大小的修改后的文件)
如果您在装有Android 2.3.3或更高版本的设备上运行,则可以使用BitmapRegionDecoder仅读取图像文件的一部分。 因此,您可以读取区域,进行处理并保存结果,然后重复进行直到处理完整个图像为止。
根据您正在执行的图像处理,可能需要重叠区域以正确处理其边缘。
在Android框架中,我认为不存在与BitmapRegionDecoder等效的功能,可以将大图像保存在各个部分中,因此您将不得不使用外部库来实现这一点。 像PNGJ这样的东西允许逐行加载和保存PNG文件(我没有使用PNGJ,因此无法对库本身进行注释)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.