Android camera2 createCaptureRequest returns all black pixels when getting YUV_420_888 image

倾然丶 夕夏残阳落幕 提交于 2019-12-11 03:15:56

问题


I have a Android camera2 API preview running ok in Kotlin using suspendCoroutine for all the surface setup and callbacks. But when I try to take a picture 5 seconds after the app starts (TEMPLATE_STILL_CAPTURE, YUV_420_888, smallest res) for some reason it all goes completely black for a moment (even in the preview window) and I get a YUV image full of 0-lum pixels.

private suspend fun captureStill(): Image = suspendCoroutine { cont ->
    val captureRequestStill = cameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE)
    captureRequestStill.addTarget(imageReaderYUV.surface)
    imageReaderYUV.setOnImageAvailableListener({ cont.resume(imageReaderYUV.acquireLatestImage()) }, backgroundHandler)
    cameraCaptureSession.capture(captureRequestStill.build(), null, backgroundHandler)
}

Am I missing something? Did I mangle some aspect of the setup earlier? Is setOnImageAvailableListener not ok for capturing a YUV image?

See the full setup dance in one suspend-enabled function


回答1:


While setting up your Camera preview, you implicitly chose some preview size. Generally speaking, your capture can use different size (even to YUV, which is essentially preview buffer, too).

But on many devices, uncoordinated choice of preview and capture sizes does not work well. The common phenomenon is that you must select preview size and Jpeg capture size to have same aspect ratio. I strongly recommend you to follow this practice for YUV, too.

You can use SurfaceTexture.setDefaultBufferSize if you want to keep the TextureView size fit the general layout.



来源:https://stackoverflow.com/questions/52490242/android-camera2-createcapturerequest-returns-all-black-pixels-when-getting-yuv-4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!