在图像 reader 中将帧从 YUV_420_888 格式错误转换为 NV21

Incorrect transformation of frames from YUV_420_888 format to NV21 within an image reader

我配置了我的代码,以便使用 imageReader 对象和其他众所周知的 camera2 [=28= 从我的设备的相机获取 YUV_420_888 帧流].现在我需要将这些帧转换为 NV21 像素格式并调用一个本机函数,该函数期望此格式的帧执行某些计算。这是我在 imagereader 回调中使用的代码,用于重新排列帧的字节:

        ImageReader.OnImageAvailableListener readerListener = new ImageReader.OnImageAvailableListener() {
            @Override
            public void onImageAvailable(ImageReader mReader) {
                Image image = null;
                image = mReader.acquireLatestImage();
                if (image == null) {
                    return;
                }                           

                byte[] bytes = convertYUV420ToNV21(image);

                nativeVideoFrame(bytes);
                image.close();   
            }
        };

private byte[] convertYUV420ToNV21(Image imgYUV420) {

    byte[] rez;

    ByteBuffer buffer0 = imgYUV420.getPlanes()[0].getBuffer();
    ByteBuffer buffer1 = imgYUV420.getPlanes()[1].getBuffer();
    ByteBuffer buffer2 = imgYUV420.getPlanes()[2].getBuffer();

    int buffer0_size = buffer0.remaining();
    int buffer1_size = buffer1.remaining();
    int buffer2_size = buffer2.remaining();

    byte[] buffer0_byte = new byte[buffer0_size];
    byte[] buffer1_byte = new byte[buffer1_size];
    byte[] buffer2_byte = new byte[buffer2_size];
    buffer0.get(buffer0_byte, 0, buffer0_size);
    buffer1.get(buffer1_byte, 0, buffer1_size);
    buffer2.get(buffer2_byte, 0, buffer2_size);


    ByteArrayOutputStream outputStream = new ByteArrayOutputStream( );
    try {
        outputStream.write( buffer0_byte );
        outputStream.write( buffer1_byte );
        outputStream.write( buffer2_byte );
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    rez = outputStream.toByteArray( );   

    return rez;
}

但不知道为什么,结果帧在水平方向上是"flipped"。换句话说,当我将相机向右移动时,我所描述的打包程序后的帧向左移动,就像传感器放置在反自然位置一样。

希望你能明白我的意思

谢谢,

JM

相机镜像正常。如果您不希望它被镜像 - 您需要执行水平镜像,交换每行中的像素。