从Camera API函数takePicture()获取/转换为YCbCr_420_SP(NV21)图像格式

时间:2018-09-27 15:51:20

标签: java android opencv camera java-native-interface

我们正在从takePicture()函数获取图像-在这里我们使用 jpeg回调(第三个参数),因为我们无法获取原始图像-即使在设置了回调缓冲区达到最大大小。因此,图像被压缩为JPEG格式-另一方面,我们需要将图像与预览帧的格式相同: YCbCr_420_SP(NV21) (此格式是我们使用的第三方库所期望的,并且我们没有重新实现的资源)

在尝试使用 setPictureFormat()初始化相机时,我们尝试在参数中设置图片格式,这很无济于事。我猜这个功能只适用于原始回调。

我们可以访问JNI端的OpenCV C库,但是不知道如何使用IplImage实现转换。

因此,当前我们正在使用以下Java实现进行转换,该实现的性能确实很差(尺寸为3840x2160的图片大约需要2秒):

byte [] getNV21(int inputWidth, int inputHeight, Bitmap scaled) {
    int [] argb = new int[inputWidth * inputHeight];
    scaled.getPixels(argb, 0, inputWidth, 0, 0, inputWidth, inputHeight);
    byte [] yuv = new byte[inputWidth*inputHeight*3/2];
    encodeYUV420SP(yuv, argb, inputWidth, inputHeight);
    scaled.recycle();

    return yuv;
}

void encodeYUV420SP(byte[] yuv420sp, int[] argb, int width, int height) {
    final int frameSize = width * height;

    int yIndex = 0;
    int uvIndex = frameSize;
    int R, G, B, Y, U, V;
    int index = 0;
    for (int j = 0; j < height; j++) {
        for (int i = 0; i < width; i++) {
            R = (argb[index] & 0xff0000) >> 16;
            G = (argb[index] & 0xff00) >> 8;
            B = (argb[index] & 0xff) >> 0;

            // well known RGB to YUV algorithm
            Y = ( (  66 * R + 129 * G +  25 * B + 128) >> 8) +  16;
            U = ( ( -38 * R -  74 * G + 112 * B + 128) >> 8) + 128;
            V = ( ( 112 * R -  94 * G -  18 * B + 128) >> 8) + 128;

            // NV21 has a plane of Y and interleaved planes of VU each sampled by a factor of 2
            //    meaning for every 4 Y pixels there are 1 V and 1 U.  Note the sampling is every other
            //    pixel AND every other scanline.
            yuv420sp[yIndex++] = (byte) ((Y < 0) ? 0 : ((Y > 255) ? 255 : Y));
            if (j % 2 == 0 && index % 2 == 0) {
                yuv420sp[uvIndex++] = (byte)((V<0) ? 0 : ((V > 255) ? 255 : V));
                yuv420sp[uvIndex++] = (byte)((U<0) ? 0 : ((U > 255) ? 255 : U));
            }

            index ++;
        }
    }
}

有人知道在OpenCV C的帮助下转换是什么样子吗?或者可以提供更有效的Java实现?

更新:重新实现相机类以使用camera2 API后,我们将收到格式为YUV_420_888的Image对象。然后,我们使用以下函数转换为NV21:

private static byte[] YUV_420_888toNV21(Image image) {
    byte[] nv21;
    ByteBuffer yBuffer = image.getPlanes()[0].getBuffer();
    ByteBuffer uBuffer = image.getPlanes()[1].getBuffer();
    ByteBuffer vBuffer = image.getPlanes()[2].getBuffer();

    int ySize = yBuffer.remaining();
    int uSize = uBuffer.remaining();
    int vSize = vBuffer.remaining();

    nv21 = new byte[ySize + uSize + vSize];

    //U and V are swapped
    yBuffer.get(nv21, 0, ySize);
    vBuffer.get(nv21, ySize, vSize);
    uBuffer.get(nv21, ySize + vSize, uSize);

    return nv21;
}

虽然此功能在cameraCaptureSessions.setRepeatingRequest上可以正常使用,但在调用cameraCaptureSessions.capture时会遇到分段错误。两者都通过ImageReader请求YUV_420_888格式。

1 个答案:

答案 0 :(得分:1)

有两种方法可以改善您的结果。

  • 您可以使用jpeg-turbo库直接从Jpeg获取YUV,该函数是tjDecompressToYUV。

  • 您可以使用渲染脚本将位图转换为YUV。

哪种设备对您更好,取决于设备。一些将使用Java的硬件加速Jpeg解码器,一些将在软件中使用libjpeg。在后一种情况下,tjDecompressToYUV将带来显着改善。

如果您的设备运行的是Android-5或更高版本,请考虑切换到 camera2 API,ImageReader可能能够提供所需分辨率和质量的YUV或RAW图像。