使用RenderScript将相机的YUV数据转换为ARGB

14
我的问题是:我在Android中设置了一个摄像头,并使用onPreviewFrame监听器接收预览数据,该监听器通过传递一个byte []数组将图像数据以默认的Android YUV格式传递给我(设备不支持R5G6B5格式)。每个像素由12位组成,这使得事情有点棘手。现在我想做的是将YUV数据转换为ARGB数据,以便对其进行图像处理。这必须使用renderscript完成,以保持高性能。
我的想法是将两个像素传递到一个元素中(这将是24位= 3字节),然后返回两个ARGB像素。问题是,在Renderscript中,u8_3(三维8位向量)存储在32位中,这意味着最后8位未使用。但是,当将图像数据复制到分配中时,所有32位都被使用,因此最后8位会丢失。即使我使用32位输入数据,最后8位也是无用的,因为它们只有2/3的像素。当定义由3字节数组组成的元素时,它实际上具有3个字节的实际大小。但是,Allocation.copyFrom()方法不会填充in-Allocation与数据,认为它没有正确的数据类型可以用byte []填充。
Renderscript文档指出,API Level 17中应该有ScriptIntrinsicYuvToRGB可以完全实现这一点。但事实上,这个类不存在。即使我下载了API Level 17,它似乎也无法下载。有人有关于此的任何信息吗?有人曾经尝试过ScriptIntrinsic吗?
因此,总之我的问题是:如何快速将相机数据转换为ARGB数据,并进行硬件加速?
以下是在Dalvik VM中执行操作的方法(在某个地方在线上找到的代码,可行):
@SuppressWarnings("unused")
private void decodeYUV420SP(int[] rgb, byte[] yuv420sp, int width, int height) {  
    final int frameSize = width * height;  
    for (int j = 0, yp = 0; j < height; j++) {
        int uvp = frameSize + (j >> 1) * width, u = 0, v = 0;  
        for (int i = 0; i < width; i++, yp++) {  
            int y = (0xff & ((int) yuv420sp[yp])) - 16;  
            if (y < 0)
                y = 0;  
            if ((i & 1) == 0) {  
                v = (0xff & yuv420sp[uvp++]) - 128;  
                u = (0xff & yuv420sp[uvp++]) - 128;  
            }  
            int y1192 = 1192 * y;  
            int r = (y1192 + 1634 * v);  
            int g = (y1192 - 833 * v - 400 * u);  
            int b = (y1192 + 2066 * u);  
            if (r < 0)
                r = 0;
            else if (r > 262143)
                r = 262143;  
            if (g < 0)
                g = 0;
            else if (g > 262143)
                g = 262143;  
            if (b < 0)
                b = 0;
            else if (b > 262143)  
                b = 262143;  
            rgb[yp] = 0xff000000 | ((r << 6) & 0xff0000) | ((g >> 2) & 0xff00) | ((b >> 10) & 0xff);  
        }
    }
}

2
我已经自己找到了答案。诀窍是将imagedata作为全局字节数组传递(只需在根函数上方插入“uchar4 *imagedata;”即可)。然后使用与图像尺寸匹配的虚拟分配作为输入分配。在renderscript中,您只需访问全局数组,而不必关心输入数据。我不得不稍微修改转换代码,特别是yuv420sp [uvp ++] -stuff必须更改。现在我想知道如何有效地显示位图数据... - user1841833
1
嘿,你能发布你的.rs文件吗?由于某些原因,我无法让它正常工作。 - Edison
4个回答

12

我相信您会发现LivePreview测试应用程序非常有趣... 它是最新的Jelly Bean(MR1)中Android源代码的一部分。 它实现了相机预览并使用ScriptIntrinsicYuvToRgb在Renderscript中转换预览数据。 您可以在此处在线浏览源代码:

LivePreview


谢谢您的回答。由于我没有达到15点“声望值”,所以无法为您点赞。我会仔细查看的! - user1841833
这个有用吗? 我正在尝试将一个字节数组从Yuv转换为Rgb,但好像不起作用。http://stackoverflow.com/q/18435680/387099 - Utkarsh Sinha

5

我无法运行ScriptInstrinsicYuvToRgb,所以我决定自己编写RS解决方案。

以下是准备好的脚本(名为yuv.rs):

#pragma version(1) 
#pragma rs java_package_name(com.package.name)

rs_allocation gIn;

int width;
int height;
int frameSize;

void yuvToRgb(const uchar *v_in, uchar4 *v_out, const void *usrData, uint32_t x, uint32_t y) {

    uchar yp = rsGetElementAtYuv_uchar_Y(gIn, x, y) & 0xFF;

    int index = frameSize + (x & (~1)) + (( y>>1) * width );
    int v = (int)( rsGetElementAt_uchar(gIn, index) & 0xFF ) -128;
    int u = (int)( rsGetElementAt_uchar(gIn, index+1) & 0xFF ) -128;

    int r = (int) (1.164f * yp  + 1.596f * v );
    int g = (int) (1.164f * yp  - 0.813f * v  - 0.391f * u);
    int b = (int) (1.164f * yp  + 2.018f * u );

    r = r>255? 255 : r<0 ? 0 : r;
    g = g>255? 255 : g<0 ? 0 : g;
    b = b>255? 255 : b<0 ? 0 : b;

    uchar4 res4;
    res4.r = (uchar)r;
    res4.g = (uchar)g;
    res4.b = (uchar)b;
    res4.a = 0xFF;

    *v_out = res4;
}

不要忘记将相机预览格式设置为NV21:

Parameters cameraParameters = camera.getParameters();
cameraParameters.setPreviewFormat(ImageFormat.NV21);
// Other camera init stuff: preview size, framerate, etc.
camera.setParameters(cameraParameters);

分配初始化和脚本使用:

// Somewhere in initialization section 
// w and h are variables for selected camera preview size
rs = RenderScript.create(this); 

Type.Builder tbIn = new Type.Builder(rs, Element.U8(rs));
tbIn.setX(w);
tbIn.setY(h);
tbIn.setYuvFormat(ImageFormat.NV21);

Type.Builder tbOut = new Type.Builder(rs, Element.RGBA_8888(rs));
tbOut.setX(w); 
tbOut.setY(h);

inData = Allocation.createTyped(rs, tbIn.create(), Allocation.MipmapControl.MIPMAP_NONE,  Allocation.USAGE_SCRIPT & Allocation.USAGE_SHARED);
outData = Allocation.createTyped(rs, tbOut.create(), Allocation.MipmapControl.MIPMAP_NONE,  Allocation.USAGE_SCRIPT & Allocation.USAGE_SHARED);

outputBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);


yuvScript = new ScriptC_yuv(rs); 
yuvScript.set_gIn(inData);
yuvScript.set_width(w);
yuvScript.set_height(h);
yuvScript.set_frameSize(previewSize);
//.....

相机回调方法:

public void onPreviewFrame(byte[] data, Camera camera) {
    // In your camera callback, data 
    inData.copyFrom(data);
    yuvScript.forEach_yuvToRgb(inData, outData);
    outData.copyTo(outputBitmap);
    // draw your bitmap where you want to 
    // .....
}

为什么我在使用你的脚本时会出现这个错误:错误:计算内核yuy2ToRgb()不能具有指针类型“const uchar4 *”的参数“v_in”。 - Christopher Schneider
1
原型需要像这样:void root( const void *v_in, uchar4 *v_out, const void *usr, uint32_t x, uint32_t y) - Christopher Schneider

2

是的,没错。至于Android Studio支持,只需复制jar和so文件即可! - Edison

0

现在我们有了新的renderscript-intrinsics-replacement-toolkit来完成它。首先,构建并导入renderscript模块到您的项目,并将其作为依赖项添加到您的应用程序模块中。然后,转到Toolkit.kt并添加以下内容:

fun toNv21(image: Image): ByteArray? {
        val nv21 = ByteArray((image.width * image.height * 1.5f).toInt())
        return if (!nativeYuv420toNv21(
                nativeHandle,
                image.width,
                image.height,
                image.planes[0].buffer,  // Y buffer
                image.planes[1].buffer,  // U buffer
                image.planes[2].buffer,  // V buffer
                image.planes[0].pixelStride,  // Y pixel stride
                image.planes[1].pixelStride,  // U/V pixel stride
                image.planes[0].rowStride,  // Y row stride
                image.planes[1].rowStride,  // U/V row stride
                nv21
            )
        ) {
            null
        } else nv21
    }

private external fun nativeYuv420toNv21(
        nativeHandle: Long,
        imageWidth: Int,
        imageHeight: Int,
        yByteBuffer: ByteBuffer,
        uByteBuffer: ByteBuffer,
        vByteBuffer: ByteBuffer,
        yPixelStride: Int,
        uvPixelStride: Int,
        yRowStride: Int,
        uvRowStride: Int,
        nv21Output: ByteArray
    ): Boolean

现在,请转到JniEntryPoints.cpp并添加以下内容:
extern "C" JNIEXPORT jboolean JNICALL Java_com_google_android_renderscript_Toolkit_nativeYuv420toNv21(
        JNIEnv *env, jobject/*thiz*/, jlong native_handle,
        jint image_width, jint image_height, jobject y_byte_buffer,
        jobject u_byte_buffer, jobject v_byte_buffer, jint y_pixel_stride,
        jint uv_pixel_stride, jint y_row_stride, jint uv_row_stride,
        jbyteArray nv21_array) {

    auto y_buffer = static_cast<jbyte*>(env->GetDirectBufferAddress(y_byte_buffer));
    auto u_buffer = static_cast<jbyte*>(env->GetDirectBufferAddress(u_byte_buffer));
    auto v_buffer = static_cast<jbyte*>(env->GetDirectBufferAddress(v_byte_buffer));

    jbyte* nv21 = env->GetByteArrayElements(nv21_array, nullptr);
    if (nv21 == nullptr || y_buffer == nullptr || u_buffer == nullptr
        || v_buffer == nullptr) {
        // Log this.
        return false;
    }

    RenderScriptToolkit* toolkit = reinterpret_cast<RenderScriptToolkit*>(native_handle);
    toolkit->yuv420toNv21(image_width, image_height, y_buffer, u_buffer, v_buffer,
                 y_pixel_stride, uv_pixel_stride, y_row_stride, uv_row_stride,
                 nv21);

    env->ReleaseByteArrayElements(nv21_array, nv21, 0);
    return true;
}

前往YuvToRgb.cpp并添加以下内容:

void RenderScriptToolkit::yuv420toNv21(int image_width, int image_height, const int8_t* y_buffer,
                  const int8_t* u_buffer, const int8_t* v_buffer, int y_pixel_stride,
                  int uv_pixel_stride, int y_row_stride, int uv_row_stride,
                  int8_t *nv21) {
    // Copy Y channel.
    for(int y = 0; y < image_height; ++y) {
        int destOffset = image_width * y;
        int yOffset = y * y_row_stride;
        memcpy(nv21 + destOffset, y_buffer + yOffset, image_width);
    }

    if (v_buffer - u_buffer == sizeof(int8_t)) {
        // format = nv21
        // TODO: If the format is VUVUVU & pixel stride == 1 we can simply the copy
        // with memcpy. In Android Camera2 I have mostly come across UVUVUV packaging
        // though.
    }

    // Copy UV Channel.
    int idUV = image_width * image_height;
    int uv_width = image_width / 2;
    int uv_height = image_height / 2;
    for(int y = 0; y < uv_height; ++y) {
        int uvOffset = y * uv_row_stride;
        for (int x = 0; x < uv_width; ++x) {
            int bufferIndex = uvOffset + (x * uv_pixel_stride);
            // V channel.
            nv21[idUV++] = v_buffer[bufferIndex];
            // U channel.
            nv21[idUV++] = u_buffer[bufferIndex];
        }
    }
}

最后,转到RenderscriptToolkit.h并添加以下内容:
/**
     * https://blog.minhazav.dev/how-to-use-renderscript-to-convert-YUV_420_888-yuv-image-to-bitmap/#tobitmapimage-image-method
     * @param image_width width of the image you want to convert to byte array
     * @param image_height height of the image you want to convert to byte array
     * @param y_buffer Y buffer
     * @param u_buffer U buffer
     * @param v_buffer V buffer
     * @param y_pixel_stride Y pixel stride
     * @param uv_pixel_stride UV pixel stride
     * @param y_row_stride Y row stride
     * @param uv_row_stride UV row stride
     * @param nv21 the output byte array
     */
    void yuv420toNv21(int image_width, int image_height, const int8_t* y_buffer,
                 const int8_t* u_buffer, const int8_t* v_buffer, int y_pixel_stride,
                 int uv_pixel_stride, int y_row_stride, int uv_row_stride,
                 int8_t *nv21);

你现在已经准备好充分利用renderscript的强大功能了。以下,我提供一个与ARCore相机图像对象相关的示例(将第一行替换为任何给出相机图像的代码):
val cameraImage = arFrame.frame.acquireCameraImage()
val width = cameraImage.width
val height = cameraImage.height
val byteArray = Toolkit.toNv21(cameraImage)
byteArray?.let {
Toolkit.yuvToRgbBitmap(
        byteArray,
        width,
        height,
        YuvFormat.NV21
).let { bitmap ->
        saveBitmapToDevice(
            name,
            session,
            bitmap,
            context
)}}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接