在图像读取器中将帧从YUV_420_888格式错误转换为NV21 [英] Incorrect transformation of frames from YUV_420_888 format to NV21 within an image reader

查看:142
本文介绍了在图像读取器中将帧从YUV_420_888格式错误转换为NV21的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I configured my code in order to get a stream of YUV_420_888 frames from my device's camera using an imageReader object and the rest of the well known camera2 API. Now I need to transform these frames to NV21 pixel format and call a native function which expect a frame in this format to perform certain computations. This is the code I am using inside the imagereader callback to rearrange the bytes of the frame:

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

但是我不知道为什么,结果帧在水平方向上翻转".换句话说,当我将相机向右移动时,我描述的包装步骤之后的帧正在向左移动,就像传感器放置在反自然的位置一样.

But I dont know why, the resulting frame is "flipped" in the horizontal direction. In other word, when I move the camera to the right, the frame after the packing procedure I have described is moving to the left, like if the sensor is placed in an antinatural position.

希望您能理解我的意思

谢谢

JM

推荐答案

相机可以生成镜像图像.如果您不希望对其进行镜像,则需要执行水平镜像,在每一行中交换像素.

It's ok for camera to produce mirrored image. If you don't want it to be mirrored - you need to perform horizontal mirroring, swapping pixels in each row.

这篇关于在图像读取器中将帧从YUV_420_888格式错误转换为NV21的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆