Android camera2 createCaptureRequest returns all b

2019-07-21 03:43发布

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条回答
走好不送
2楼-- · 2019-07-21 04:40

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.

查看更多
登录 后发表回答