我如何 lock/freeze CameraX 预览 TextureView?
How do I lock/freeze a CameraX preview TextureView?
我在 Android Studio 中关注 CameraX code lab and also looked at their sample app 和源代码,但似乎没有办法冻结或锁定正在显示的 TextureView
预览帧。
在 Camera2 API 中,我们可以调用 cameraCaptureSession?.stopRepeating()
之类的东西,TextureView
将停止从相机获取输入。
我冻结预览的用例是向用户显示当前正在保存的图像,因为我在 TextureView
上添加了其他动画。
// You can unbind from any UseCase
CameraX.unbind(previewUseCase)
// In this way TextureView will hold the last frame
// for version 1.0.0-alpha07
cameraProvider.unbind(preview);
您可以使用PreviewView.getBitmap()
来捕获出现在预览屏幕上的最新图像。速度非常快,您甚至不必旋转或翻转图像。
我的XML代码:
<androidx.camera.view.PreviewView
android:id="@+id/viewFinder"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintDimensionRatio="3:3"
app:layout_constraintTop_toTopOf="parent"/>
<ImageView
android:visibility="gone"
android:id="@+id/ivPreview"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintDimensionRatio="3:3"
app:layout_constraintTop_toTopOf="parent"/>
我以前的代码,处理来自OnImageCapturedCallback
或OnImageSavedCallback
的图像:
private fun showCapturedImage(bitmap: Bitmap, rotationDegrees: Float) {
viewFinder.visibility = View.GONE
ivPreview.visibility = View.VISIBLE
var rotatedBitmap = ImageUtils.rotateImage(bitmap, rotationDegrees).toSquare()
if (isTakeFacePhoto){
rotatedBitmap = rotatedBitmap.flipBitmap()
}
ivPreview.setImageBitmap(rotatedBitmap)
}
当前代码,使用后PreviewView.getBitmap()
:
val bitmap = viewFinder.getBitmap()
showCapturedImage(bitmap)
private fun showCapturedImage(bitmap: Bitmap) {
viewFinder.visibility = View.GONE
ivPreview.visibility = View.VISIBLE
ivPreview.setImageBitmap(bitmap)
}
如果您打算使用 OnImageSavedCallback
保存图像,请记住 PreviewView.getBitmap()
和 OnImageSavedCallback
保存的文件可能会有轻微的延迟,因此这两张图像可以真正不同取决于延迟。
我在 Android Studio 中关注 CameraX code lab and also looked at their sample app 和源代码,但似乎没有办法冻结或锁定正在显示的 TextureView
预览帧。
在 Camera2 API 中,我们可以调用 cameraCaptureSession?.stopRepeating()
之类的东西,TextureView
将停止从相机获取输入。
我冻结预览的用例是向用户显示当前正在保存的图像,因为我在 TextureView
上添加了其他动画。
// You can unbind from any UseCase
CameraX.unbind(previewUseCase)
// In this way TextureView will hold the last frame
// for version 1.0.0-alpha07
cameraProvider.unbind(preview);
您可以使用PreviewView.getBitmap()
来捕获出现在预览屏幕上的最新图像。速度非常快,您甚至不必旋转或翻转图像。
我的XML代码:
<androidx.camera.view.PreviewView
android:id="@+id/viewFinder"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintDimensionRatio="3:3"
app:layout_constraintTop_toTopOf="parent"/>
<ImageView
android:visibility="gone"
android:id="@+id/ivPreview"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintDimensionRatio="3:3"
app:layout_constraintTop_toTopOf="parent"/>
我以前的代码,处理来自OnImageCapturedCallback
或OnImageSavedCallback
的图像:
private fun showCapturedImage(bitmap: Bitmap, rotationDegrees: Float) {
viewFinder.visibility = View.GONE
ivPreview.visibility = View.VISIBLE
var rotatedBitmap = ImageUtils.rotateImage(bitmap, rotationDegrees).toSquare()
if (isTakeFacePhoto){
rotatedBitmap = rotatedBitmap.flipBitmap()
}
ivPreview.setImageBitmap(rotatedBitmap)
}
当前代码,使用后PreviewView.getBitmap()
:
val bitmap = viewFinder.getBitmap()
showCapturedImage(bitmap)
private fun showCapturedImage(bitmap: Bitmap) {
viewFinder.visibility = View.GONE
ivPreview.visibility = View.VISIBLE
ivPreview.setImageBitmap(bitmap)
}
如果您打算使用 OnImageSavedCallback
保存图像,请记住 PreviewView.getBitmap()
和 OnImageSavedCallback
保存的文件可能会有轻微的延迟,因此这两张图像可以真正不同取决于延迟。