也许您可以尝试解除预览用例的绑定:
版本1.0.0-alpha06:CameraX.unbind(preview);
版本 > 1.0.0-alpha07:cameraProvider.unbind(preview);
在您的情况下,您需要将预览用例保存到变量中,然后取消绑定:
// Declare the preview use case variable (as in the CameraXBasic example)
private var preview: Preview? = null
然后实例化变量(就像你所做的那样):
private fun buildPreviewUseCase(): Preview {
preview = Preview(
UseCaseConfigBuilder.buildPreviewConfig(
viewFinder.display
)
)
preview.setOnPreviewOutputUpdateListener { previewOutput ->
updateViewFinderWithPreview(previewOutput)
correctPreviewOutputForDisplay(previewOutput.textureSize)
}
return preview
}
然后,当您想要冻结预览时,只需取消绑定用例即可:
CameraX.unbind(preview);
EDIT正如@Billda 在这篇文章中所说: :
要冻结预览,您不应取消预览用例的绑定。也许有
将来会使用 API,但目前推荐的方法是
存储来自 ImageAnalysis 的最新帧并将其放入 ImageView
重叠预览。
因此,我决定更新我的答案,提供另一个使用 ImageAnalysis 实现分析器的解决方案(1.0.0-beta02)。
1- 创建 FreezeAnalyzer 类:
class FreezeAnalyzer(private val callback: FreezeCallback) : ImageAnalysis.Analyzer {
private var flag = false
override fun analyze(image: ImageProxy) {
if(flag){
flag = false
val bitmap = toBitmap(image)
callback.onLastFrameCaptured(bitmap)
}
image.close()
}
fun freeze(){
flag = true
}
private fun toBitmap(image: ImageProxy): Bitmap {
// Convert the imageProxy to Bitmap
// ref https://stackoverflow.com/questions/56772967/converting-imageproxy-to-bitmap
// ISSUE, on my android 7 when converting the imageProxy to Bitmap I have a problem with the colors...
var bitmap = ...
// Rotate the bitmap
val rotationDegrees = image.imageInfo.rotationDegrees.toFloat()
if (rotationDegrees != 0f) {
val matrix = Matrix()
matrix.postRotate(rotationDegrees)
bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.width, bitmap.height, matrix, true)
}
return bitmap
}
}
2- XML
<androidx.camera.view.PreviewView
android:id="@+id/preview_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="@+id/image_view"
android:visibility="invisible"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
3-初始化图像分析器
val resolutionSize = Size(preview_view.width, preview_view.height)
// Set up analyser
imageAnalysis = ImageAnalysis.Builder().apply {
setTargetResolution(resolutionSize)
setBackpressureStrategy(ImageAnalysis.STRATEGY_KEEP_ONLY_LATEST)
}.build()
val analyzer = FreezeAnalyzer(object : FreezeCallback {
override fun onLastFrameCaptured(bitmap: Bitmap) {
runOnUiThread {
preview_view.visibility = View.INVISIBLE
image_view.visibility = View.VISIBLE
image_view.setImageBitmap(bitmap)
}
}
})
imageAnalysis.setAnalyzer(executor, analyzer)
4- 绑定 imageAnalysis 用例
try {
val camera = cameraProvider.bindToLifecycle(
this,
cameraSelector,
preview,
imageAnalysis,
imageCapture
)
preview.setSurfaceProvider(preview_view.createSurfaceProvider(camera.cameraInfo))
}
5-拍摄照片
btn_capture.setOnClickListener {
file = File(externalMediaDirs.first(), "${System.currentTimeMillis()}.jpg")
val outputFileOptions: ImageCapture.OutputFileOptions =
ImageCapture.OutputFileOptions.Builder(file!!).build()
analyzer.freeze()
imageCapture.takePicture(outputFileOptions, executor, onImageSavedCallback)
}
6-释放
btn_release.setOnClickListener {
preview_view.visibility = View.VISIBLE
image_view.visibility = View.INVISIBLE
}
我希望它有所帮助,我不是专家,所以如果您有一些改进,欢迎您!