我正在学习CameraX API,CameraXBasic是办公室示例代码。
CameraXBasic 中的CameraFragment.kt 显示了真实的摄像机预览,我希望添加一个 Switch 按钮冻结当前预览,通过即使移动手机相机镜头,图片也不会改变。
如何使用CameraX API?谢谢!
CameraFragment.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)
}
答案 0 :(得分:1)
在PreviewOutputUpdateListener
上添加Preview
,然后可以决定是否更新TextureView
:
Java:
preview.setOnPreviewOutputUpdateListener(
previewOutput -> {
if(!frozen){
textureView.setSurfaceTexture(previewOutput.getSurfaceTexture());
}
});
科特琳:
preview.setOnPreviewOutputUpdateListener {
previewOutput: Preview.PreviewOutput? ->
if(!frozen)
textureView.setSurfaceTexture(previewOutput.getSurfaceTexture());
}
预览用例产生一个SurfaceTexture,用于传输相机输入。它还提供了有关视图进行裁剪,缩放或旋转以正确显示的其他信息。
当相机处于活动状态时,图像预览将流式传输到此SurfaceTexture。 SurfaceTexture可以连接到TextureView或GLSurfaceView。
因此Preview
本身不会呈现任何内容,仅提供要渲染的Texture
,由您决定如何处理由{
Texture
。