簡體   English   中英

使用 renderscript 將相機 YUV 數據轉換為 ARGB

[英]Converting camera YUV-data to ARGB with renderscript

我的問題是:我在 Android 中設置了一個攝像頭,並使用 onPreviewFrame-listener 接收預覽數據,它向我傳遞一個字節 [] 數組,其中包含默認 android YUV 格式的圖像數據(設備不支持 R5G6B5-格式)。 每個像素由 12 位組成,這讓事情變得有點棘手。 現在我要做的是將 YUV 數據轉換為 ARGB 數據,以便用它進行圖像處理。 這必須通過 renderscript 來完成,以保持高性能。

我的想法是在一個元素中傳遞兩個像素(即 24 位 = 3 個字節),然后返回兩個 ARGB 像素。 問題是,在 Renderscript 中,u8_3(一個 3 維 8 位向量)以 32 位存儲,這意味着最后 8 位未使用。 但是當將圖像數據復制到分配中時,所有 32 位都被使用,所以最后 8 位丟失了。 即使我使用 32 位輸入數據,最后 8 位也沒有用,因為它們只有 2/3 個像素。 當定義一個包含 3 字節數組的元素時,它實際上具有 3 個字節的實際大小。 但是 Allocation.copyFrom() 方法沒有用數據填充 in-Allocation,認為它沒有正確的數據類型來填充 byte[]。

renderscript 文檔指出,有一個 ScriptIntrinsicYuvToRGB 應該在 API 級別 17 中執行此操作。但實際上 class 不存在。 我已經下載了 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);  
        }
    }
}

我相信你會發現LivePreview測試應用程序很有趣......它是最新Jelly Bean(MR1)中Android源代碼的一部分。 它實現了攝像頭預覽,並使用ScriptIntrinsicYuvToRgb通過Renderscript轉換預覽數據。 您可以在此瀏覽在線源:

LivePreview

我無法運行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 
    // .....
}

我們現在有了新的renderscript-intrinsics-replacement-toolkit來完成它。 首先,構建 renderscript 模塊並將其導入到您的項目中,並將其作為依賴項添加到您的應用程序模塊中。 然后,go 到 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

現在,go 到 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;
}

Go 到 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];
        }
    }
}

最后,go到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 Camera Image object 示例(將第一行替換為提供相機圖像的任何代碼):

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
)}}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM