android - 摄像头 2,增加 FPS

标签 android jpeg yuv frame-rate camera2

我正在使用 Camera 2 API 将 JPEG 图像保存在磁盘上。我的 Nexus 5X 目前有 3-4 fps,我想将其提高到 20-30。可能吗?

将图像格式更改为 YUV 我设法生成 30 fps。是否有可能以这个帧速率保存它们,或者我应该放弃并以我的 3-4 fps 生活?

显然,如果需要,我可以共享代码,但如果每个人都认为这是不可能的,我就会放弃。使用 NDK(例如使用 libjpeg)是一种选择(但显然我更愿意避免使用它...)。

谢谢

编辑:这是我如何将 YUV android.media.Image 转换为单个字节[]:

private byte[] toByteArray(Image image, File destination) {

    ByteBuffer buffer0 = image.getPlanes()[0].getBuffer();
    ByteBuffer buffer2 = image.getPlanes()[2].getBuffer();
    int buffer0_size = buffer0.remaining();
    int buffer2_size = buffer2.remaining();

    byte[] bytes = new byte[buffer0_size + buffer2_size];

    buffer0.get(bytes, 0, buffer0_size);
    buffer2.get(bytes, buffer0_size, buffer2_size);

    return bytes;
}

编辑 2:我发现另一种将 YUV 图像转换为 byte[] 的方法:

private byte[] toByteArray(Image image, File destination) {

    Image.Plane yPlane = image.getPlanes()[0];
    Image.Plane uPlane = image.getPlanes()[1];
    Image.Plane vPlane = image.getPlanes()[2];

    int ySize = yPlane.getBuffer().remaining();

    // be aware that this size does not include the padding at the end, if there is any
    // (e.g. if pixel stride is 2 the size is ySize / 2 - 1)
    int uSize = uPlane.getBuffer().remaining();
    int vSize = vPlane.getBuffer().remaining();

    byte[] data = new byte[ySize + (ySize/2)];

    yPlane.getBuffer().get(data, 0, ySize);

    ByteBuffer ub = uPlane.getBuffer();
    ByteBuffer vb = vPlane.getBuffer();

    int uvPixelStride = uPlane.getPixelStride(); //stride guaranteed to be the same for u and v planes

    if (uvPixelStride == 1) {

        uPlane.getBuffer().get(data, ySize, uSize);
        vPlane.getBuffer().get(data, ySize + uSize, vSize);
    }
    else {

        // if pixel stride is 2 there is padding between each pixel
        // converting it to NV21 by filling the gaps of the v plane with the u values
        vb.get(data, ySize, vSize);
        for (int i = 0; i < uSize; i += 2) {
            data[ySize + i + 1] = ub.get(i);
        }
    }

    return data;
}

最佳答案

手机上的专用 JPEG 编码器单元效率很高,但通常没有针对吞吐量进行优化。 (从历史上看,用户每两秒拍摄一张照片)。在全分辨率下,5X 的相机管道生成 JPEG 的速度不会超过几帧每秒。

如果您需要更高的速率,则需要使用未压缩的 YUV 进行捕获。正如 CommonsWare 所提到的,没有足够的磁盘带宽来将全分辨率未压缩的 YUV 流式传输到磁盘,因此您只能在内存耗尽之前保留一定数量的帧。

您可以使用 libjpeg-turbo 或其他一些高效 JPEG 编码器,看看您每秒可以自己压缩多少帧 - 这可能比硬件 JPEG 单元更高。最大化速率的最简单方法是以 30fps 捕获 YUV,并并行运行一定数量的 JPEG 编码线程。为了获得最大速度,您需要手写与 JPEG 编码器通信的代码,因为您的源数据是 YUV,而不是大多数 JPEG 编码接口(interface)倾向于接受的 RGB(即使通常编码的 JPEG 的色彩空间实际上是YUV 也是如此)。

每当编码器线程完成前一帧时,它就可以抓取来自相机的下一帧(您可以维护最新 YUV 图像的小型循环缓冲区以简化此过程)。

关于android - 摄像头 2,增加 FPS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41154048/

相关文章:

python - 在 Python (PIL) 中读取带有损坏 header 的 JPEG

image - 霍夫曼代码表

python-3.x - 如何使用 PIL 或类似的东西用 yuv420 格式数据写入图像

ffmpeg - 使用 qp 将 YUV 编码为 mP4 以获得最高质量

ios - 将 kCVPixelFormatType_420YpCbCr8BiPlanarFullRange 缓冲区转换为 RGB 缓冲区会创建蓝色图片

java - 将 MySQL 转储文件导入到 SQLite

java - 如何加载数组中的图像以更快地显示android

android - 安装两个版本的 apk(具有不同的 applicationIds)

android - 如何在 Eclipse 上清理项目后重新生成 R.java?

Android (onPreviewFrame) : Saving NV21 data as JPEG File