【问题标题】:Allocating big bitmap for renderscript (android)为渲染脚本分配大位图(android)
【发布时间】:2012-12-25 00:42:34
【问题描述】:

我尝试在 android renderscript 中制作简单的图像过滤器,它确实适用于小图像。但是,我得到out of memory error 的图片与使用相机拍摄的图片一样大(尽管对于小图像来说一切都很好)。我知道我的代码有点糟糕(主要是从 here 复制的),所以任何关于如何在大位图上进行渲染脚本计算的提示都值得赞赏。这是调用 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 memory image-processing bitmap renderscript


    【解决方案1】:

    如果您在运行 Android 2.3.3 或更高版本的设备上运行,则可以使用 BitmapRegionDecoder 仅读取图像文件的一部分。因此,您可以读取一个区域,对其进行处理并保存结果,然后重复进行,直到处理完整个图像。

    根据您正在执行的图像处理,您可能需要重叠区域以正确处理它们的边缘。

    在 Android 框架中,我认为没有等效的 BitmapRegionDecoder 可以分段保存大图像,因此您必须使用外部库来实现。像PNGJ 这样的东西允许逐行加载和保存PNG文件(我没有使用PNGJ,所以不能评论库本身)。

    【讨论】:

    • 谢谢,我已经知道BitmapRegionDecoder,很高兴你确认。真的没有办法在不使用外部库的情况下保存文件吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多