如何在 Android Studio 中使用 CameraX API 冻结相机的照片?
How can I freeze picture of camera with CameraX API in Android Studio?
正在学习CameraXAPI,CameraXBasic是office示例代码。
CameraFragment.kt在CameraXBasic中显示真实的相机预览,希望添加一个Switch按钮冻结当前预览,即使我移动phone相机镜头,图片也不会改变。
如何使用 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)
}
将PreviewOutputUpdateListener
添加到Preview
,然后您可以决定是否更新TextureView
:
Java:
preview.setOnPreviewOutputUpdateListener(
previewOutput -> {
if(!frozen){
textureView.setSurfaceTexture(previewOutput.getSurfaceTexture());
}
});
科特林:
preview.setOnPreviewOutputUpdateListener {
previewOutput: Preview.PreviewOutput? ->
if(!frozen)
textureView.setSurfaceTexture(previewOutput.getSurfaceTexture());
}
The preview use case produces a SurfaceTexture which streams the camera input. It also provides additional information for a View to crop, scale, or rotate for proper display.
The image preview is streamed to this SurfaceTexture when the camera becomes active. The SurfaceTexture can be connected to a TextureView or a GLSurfaceView.
所以 Preview
本身不渲染任何东西,只是提供了一个要渲染的 Texture
,由你决定如何处理这个返回的 Texture
previewOutput.getSurfaceTexture()
.
正在学习CameraXAPI,CameraXBasic是office示例代码。
CameraFragment.kt在CameraXBasic中显示真实的相机预览,希望添加一个Switch按钮冻结当前预览,即使我移动phone相机镜头,图片也不会改变。
如何使用 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)
}
将PreviewOutputUpdateListener
添加到Preview
,然后您可以决定是否更新TextureView
:
Java:
preview.setOnPreviewOutputUpdateListener(
previewOutput -> {
if(!frozen){
textureView.setSurfaceTexture(previewOutput.getSurfaceTexture());
}
});
科特林:
preview.setOnPreviewOutputUpdateListener {
previewOutput: Preview.PreviewOutput? ->
if(!frozen)
textureView.setSurfaceTexture(previewOutput.getSurfaceTexture());
}
The preview use case produces a SurfaceTexture which streams the camera input. It also provides additional information for a View to crop, scale, or rotate for proper display.
The image preview is streamed to this SurfaceTexture when the camera becomes active. The SurfaceTexture can be connected to a TextureView or a GLSurfaceView.
所以 Preview
本身不渲染任何东西,只是提供了一个要渲染的 Texture
,由你决定如何处理这个返回的 Texture
previewOutput.getSurfaceTexture()
.