Android Renderscript在nexus 7上创建分配会导致错误

时间:2014-06-20 20:33:41

标签: android renderscript

我尝试使用以下代码将相机预览帧从YUV格式转换为位图:

    mYuvToRgbScript = ScriptIntrinsicYuvToRGB.create(mRs, Element.RGBA_8888(mRs));
    final Allocation allocationIn = Allocation.createSized(mRs, Element.U8(mRs),
            (mPreviewWidth * mPreviewHeight) + ((mPreviewHeight / 2) * (mPreviewWidth / 2) * 2));
    allocationIn.copyFrom(mPreviewFrame);

    final Bitmap previewBitmap = Bitmap.createBitmap(mPreviewWidth, mPreviewHeight, Bitmap.Config.ARGB_8888);
    final Allocation allocationOut = Allocation.createFromBitmap(mRs, previewBitmap);

    mYuvToRgbScript.setInput(allocationIn);
    mYuvToRgbScript.forEach(allocationOut);

    allocationIn.destroy();
    allocationOut.destroy();

    if (previewBitmap != null) {
        return previewBitmap;
    } else
        return null;

此代码适用于2台安装了Android 4.1的手机,但是在nexus 7(2013)上安装了Android 4.4.3后续行显示在每个预览框架上:

W/Adreno-RS﹕ <rsdVendorAllocationSetupTexture:647>: ERROR: Runtime texture creation failed err: -10 image: 0x0 alloc: 0xad720000
W/Adreno-RS﹕ <rsdVendorAllocationSetupTexture:649>: ERROR: Runtime texture creation failed type: 8 kind: 0 eleSize: 1

没有转换发生。通过一些调试,我找到导致此日志消息的行:

    final Allocation allocationIn = Allocation.createSized(mRs, Element.U8(mRs),
            (mPreviewWidth * mPreviewHeight) + ((mPreviewHeight / 2) * (mPreviewWidth / 2) * 2));

我也尝试了另一个allocateIn创建:

    final Type.Builder tb = new Type.Builder(mRs, Element.U8(mRs));
    tb.setX(mPreviewFrame.length);
    final Allocation allocationIn = Allocation.createTyped(mRs, tb.create(), Allocation.USAGE_SCRIPT);

但它会导致相同的错误日志行,并且不会发生转换。有任何想法如何解决它?

1 个答案:

答案 0 :(得分:3)

您忘记将分配复制回预览位图。在调用forEach()之后,你应该这样做:

allocationOut.copyTo(previewBitmap);