因此,我从使用传统相机 api 迁移到 CameraX,尽管设置非常简单,但我注意到了一个问题。现在,相机开始显示预览的时间几乎是以前的两倍,如果不是更长的话。我正在 Galaxy s7 上进行测试。我的代码如下所示:
val previewConfig = PreviewConfig.Builder().apply {
setTargetAspectRatio(Rational(1, 1))
setTargetResolution(Size(binding.codeScannerView.width, binding.codeScannerView.height))
}.build()
val preview = Preview(previewConfig)
preview.setOnPreviewOutputUpdateListener { preview ->
val parent = binding.codeScannerView.parent as ViewGroup
parent.removeView(binding.codeScannerView)
parent.addView(binding.codeScannerView, 0)
binding.codeScannerView.surfaceTexture = preview.surfaceTexture
}
val analyzerConfig = ImageAnalysisConfig.Builder().apply {
val analyzerThread = HandlerThread(
"QrCodeReader").apply { start() }
setCallbackHandler(Handler(analyzerThread.looper))
setImageReaderMode(
ImageAnalysis.ImageReaderMode.ACQUIRE_LATEST_IMAGE)
}.build()
val analyzerUseCase = ImageAnalysis(analyzerConfig).apply {
analyzer = QrCodeAnalyzer(requireContext(), Handler(), { qrCode ->
if (activity == null) {
return@QrCodeAnalyzer
}
presenter.disableCameraPreview()
presenter.updateTable(qrCode.toLowerCase().parseTableId(), isFromOrder, Screens.MENU_SCREEN)
})
}
CameraX.bindToLifecycle(this, preview, analyzerUseCase)
关于如何让它看起来更快的任何想法?
PS我偶尔也会在预览中看到撕裂