我正在研究一个使用CameraX在TextureView
上显示摄像机预览的项目。代码来自此codelab。我已经使用setOnPreviewOutputUpdateListener()
方法来更新TextureView
。
private fun startCamera() {
val previewConfig = PreviewConfig.Builder().apply {
setTargetAspectRatio( Rational( 1 , 1 ) )
setTargetResolution( Size( 640 , 640 ) )
}.build()
val preview = Preview( previewConfig )
preview.setOnPreviewOutputUpdateListener {
val parent = cameraTextureView?.parent as ViewGroup
parent.removeView( cameraTextureView )
cameraTextureView?.surfaceTexture = it.surfaceTexture
parent.addView( cameraTextureView , 0)
updateTransform()
}
preview.removePreviewOutputListener()
CameraX.bindToLifecycle( this , preview )
}
我的问题是如何使用CameraX从预览帧中获取原始byte[]
对象。我期望有些类似于onPreviewFrame(byte[] data, Camera camera)
方法。
基本上,我希望使用CameraX从预览中获取实时帧(在
byte[]
或Bitmap
中)。
答案 0 :(得分:2)
您将使用ImageAnalysis。预览帧将以YUV_420_888格式传递到分析仪。
如有必要,您可以请求非阻塞分析模式。我建议为您的分析器设置一个处理程序,以使摄像机回调不到达UI线程。