Android camera2 API.设置多个ImageReader曲面可提供空白输出 [英] Android camera2 api. Setting multiple ImageReader surfaces gives blank output

查看:664
本文介绍了Android camera2 API.设置多个ImageReader曲面可提供空白输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个camera2实现.当前设置是,它使用纹理视图表面显示实际的相机视图,并使用ImageReader表面捕获图像.

I have a camera2 implementation. The current setup is, it uses a texture view surface to display the actual camera view and an ImageReader surface for capturing images.

现在,我也想捕获预览帧.因此,我尝试添加一个新的ImageReader表面来捕获帧.但是,当我将该表面添加到createCaptureSession请求时,屏幕变为空白.可能有什么问题吗?以下是我用于将表面添加到createCaptureSession

Now I want to capture preview frames as well. So I tried adding a new ImageReader surface for capturing frames. But when I add that surface to createCaptureSession request, the screen goes blank. What could possibly be wrong? Below is the code that I use to add surfaces to createCaptureSession

val surface = preview.surface
            ?: throw CameraAccessException(CameraAccessException.CAMERA_ERROR)

val previewIRSurface = previewImageReader?.surface
            ?: throw CameraAccessException(CameraAccessException.CAMERA_ERROR)

val captureSurface = captureImageReader?.surface
            ?: throw CameraAccessException(CameraAccessException.CAMERA_ERROR)

try {
    val template = if (zsl) CameraDevice.TEMPLATE_ZERO_SHUTTER_LAG else CameraDevice.TEMPLATE_PREVIEW

    previewRequestBuilder = camera?.createCaptureRequest(template)
            ?.apply { addTarget(surface) }
            ?: throw CameraAccessException(CameraAccessException.CAMERA_ERROR)

    val surfaces: ArrayList<Surface> = arrayListOf(surface, previewIRSurface, captureSurface)

    camera?.createCaptureSession(surfaces, sessionCallback, backgroundHandler)

} catch (e: CameraAccessException) {
    throw RuntimeException("Failed to start camera session")
}

ImageReaders的初始化是这样的.

The initialization of ImageReaders is like this.

private fun prepareImageReaders() {

    val largestPreview = previewSizes.sizes(aspectRatio).last()

    previewImageReader?.close()

    previewImageReader = ImageReader.newInstance(
            largestPreview.width,
            largestPreview.height,
            internalOutputFormat,
            4 // maxImages
    ).apply { setOnImageAvailableListener(onPreviewImageAvailableListener, backgroundHandler) }

    val largestPicture = pictureSizes.sizes(aspectRatio).last()

    captureImageReader?.close()

    captureImageReader = ImageReader.newInstance(
            largestPicture.width,
            largestPicture.height,
            internalOutputFormat,
            2 // maxImages
    ).apply { setOnImageAvailableListener(onCaptureImageAvailableListener, backgroundHandler) }
}

有关上面使用的参数的更多说明:

  • internalOutput格式为ImageFormat.JPEGImageFormat.YUV_420_888.
  • 图像尺寸基于最佳尺寸
  • 单独使用任何一个图像读取器都可以,但是当我将两者都加在一起时,空白屏幕就可以了!
  • 使用Android Oreo(8.0)在Samsung Galaxy S8上进行测试
  • internalOutput format is either ImageFormat.JPEG or ImageFormat.YUV_420_888.
  • Image sizes are based on best possible sizes
  • It works good with either of the image readers individually but as soon as I add both together, blank screen!
  • Testing on Samsung Galaxy S8 with Android Oreo (8.0)

原始代码在此处推荐答案

maxImages == 4 可能太多,并耗尽了RAM.另外,不清楚您使用什么 internalOutputFormat ,以及它是否与 largestPreview 大小兼容.

maxImages == 4 may be too much and exhaust your RAM. Also, it's not clear what internalOutputFormat you use, and whether it is compatible with the largestPreview size.

最重要的是,研究一长串表以获取

The bottom line is, study the long list of tables for supported surface list parameter of createCaptureSession(). Depending on your camera capabilities, the three surfaces that you use, could be too much.

从下面的评论中,一个有效的解决方案:错误本身说的并不多,但是在搜索时发现JPEG格式不支持多个表面.将其更改为YUV_420_888它可以完美运行."

From the comments below, a working solution: "The error itself doesn't say much [...] but upon searching, it is found that multiple surfaces are not supported for JPEG format. Upon changing it to YUV_420_888 it works flawlessly."

这篇关于Android camera2 API.设置多个ImageReader曲面可提供空白输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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