首页 > 解决方案 > 从 Camera API 函数 takePicture() 获取/转换为 YCbCr_420_SP (NV21) 图像格式

问题描述

我们从 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 格式。

标签: javaandroidopencvcamerajava-native-interface

解决方案


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

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

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

哪个更适合您,取决于设备。有些将具有用于 Java 的硬件加速 Jpeg 解码器,有些将在软件中使用 libjpeg。在后一种情况下,tjDecompressToYUV 将提供显着的改进。

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


推荐阅读