[英]How can I freeze picture of camera with CameraX API in Android Studio?
我正在学习CameraX API, CameraXBasic是office 示例代码。
CameraXBasic中的CameraFragment.kt显示了真实的相机预览,我希望添加一个切换按钮来冻结当前预览,这样即使移动手机相机镜头,图片也不会改变。
如何使用 CameraX API? 谢谢!
相机片段.kt
private lateinit var viewFinder: TextureView
private fun bindCameraUseCases() {
// Get screen metrics used to setup camera for full screen resolution
val metrics = DisplayMetrics().also { viewFinder.display.getRealMetrics(it) }
val screenAspectRatio = Rational(metrics.widthPixels, metrics.heightPixels)
Log.d(TAG, "Screen metrics: ${metrics.widthPixels} x ${metrics.heightPixels}")
// Set up the view finder use case to display camera preview
val viewFinderConfig = PreviewConfig.Builder().apply {
setLensFacing(lensFacing)
// We request aspect ratio but no resolution to let CameraX optimize our use cases
setTargetAspectRatio(screenAspectRatio)
// Set initial target rotation, we will have to call this again if rotation changes
// during the lifecycle of this use case
setTargetRotation(viewFinder.display.rotation)
}.build()
// Use the auto-fit preview builder to automatically handle size and orientation changes
preview = AutoFitPreviewBuilder.build(viewFinderConfig, viewFinder)
....
CameraX.bindToLifecycle(
viewLifecycleOwner, preview, imageCapture, imageAnalyzer)
}
将PreviewOutputUpdateListener
添加到Preview
,然后您可以决定是否更新TextureView
:
爪哇:
preview.setOnPreviewOutputUpdateListener(
previewOutput -> {
if(!frozen){
textureView.setSurfaceTexture(previewOutput.getSurfaceTexture());
}
});
科特林:
preview.setOnPreviewOutputUpdateListener {
previewOutput: Preview.PreviewOutput? ->
if(!frozen)
textureView.setSurfaceTexture(previewOutput.getSurfaceTexture());
}
预览用例会生成一个 SurfaceTexture,它会流式传输相机输入。 它还为 View 提供了额外的信息来裁剪、缩放或旋转以正确显示。
当相机处于活动状态时,图像预览将流式传输到此 SurfaceTexture。 SurfaceTexture 可以连接到 TextureView 或 GLSurfaceView。
所以Preview
本身不渲染任何东西,只提供一个要渲染的Texture
,由你决定如何处理previewOutput.getSurfaceTexture()
返回的这个Texture
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.