当获取YUV_420_888图像时,Android camera2 createCaptureRequest返回所有黑色像素 [英] Android camera2 createCaptureRequest returns all black pixels when getting YUV_420_888 image

查看:316
本文介绍了当获取YUV_420_888图像时,Android camera2 createCaptureRequest返回所有黑色像素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Android camera2 API 预览,它在Kotlin中使用suspendCoroutine在所有表面设置和回调中正常运行.但是当我尝试在应用启动5秒后尝试拍照时(TEMPLATE_STILL_CAPTURE,YUV_420_888,最小分辨率),由于某种原因,它全都变成了黑色(即使在预览窗口中),并且我得到了充满0-的YUV图像lum像素.

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)
}

我错过了什么吗?我是否早些修改了设置的某些方面? setOnImageAvailableListener是否无法捕获YUV图像?

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

通过一个启用暂停的功能查看完整的设置

推荐答案

在设置相机预览时,您隐式选择了一些预览尺寸.一般而言,您的捕获可以使用不同的大小(甚至是YUV,它实际上也是预览缓冲区).

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).

但是在许多设备上,预览和捕获大小的不协调选择并不奏效.常见现象是必须选择预览大小和Jpeg捕获大小以具有相同的宽高比.我强烈建议您也对YUV遵循这种做法.

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.

您可以使用 SurfaceTexture.setDefaultBufferSize 如果要保持TextureView的大小适合常规布局.

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

这篇关于当获取YUV_420_888图像时,Android camera2 createCaptureRequest返回所有黑色像素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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