相机分析仅在ImageAnalysis.Analyzer中进入分析方法一次,在小米米a20 Lite中

Camera analysis only entering to analyze method in ImageAnalysis.Analyzer only once, in Xiaomi Mi a20 Lite

分析在所有其他 Android phone 我已经尝试过(华为 Mate 20 Pro、三星 S8、三星 J7 和三星 S10+),但在小米 Mi a20 Lite 中没有.它只进入一次 analyze 函数,然后在 Logcat 中抛出:

E/ImageAnalysisAnalyzer: Failed to acquire image.
java.lang.IllegalStateException: maxImages (1) has already been acquired, call #close before acquiring more.

我是这样构建分析器的,因为我们希望它仅在处理完队列中的帧后才带来新帧:

val imageAnalysis = builder
                    .setBackpressureStrategy(ImageAnalysis.STRATEGY_BLOCK_PRODUCER)
                    .setImageQueueDepth(1)
                    .build()

analyze 函数中,我们确保关闭 image,但在这个特定的小米设备中仍然出现错误:

@SuppressLint("UnsafeOptInUsageError")
    override fun analyze(imageProxy: ImageProxy) {
        val mediaImage = imageProxy.image
        if (mediaImage != null) {
            val image = InputImage.fromMediaImage(mediaImage, imageProxy.imageInfo.rotationDegrees)
            val scanner = BarcodeScanning.getClient(options)
            scanner.process(image)
                    .addOnSuccessListener { barcodes: List<Barcode?> ->
                        if (barcodes.isNotEmpty()) {
                            EventBus.getDefault().post(OnRetrievedSuccess(barcodes))
                        }
                        image.mediaImage?.close()
                        imageProxy.close()
                    }
                    .addOnFailureListener {
                        EventBus.getDefault().post(OnRetrievedFailure())
                        image.mediaImage?.close()
                        imageProxy.close()
                    }
        }
    }

构建时更改图像策略已解决问题:

val imageAnalysis = builder
                    .setBackpressureStrategy(ImageAnalysis.STRATEGY_KEEP_ONLY_LATEST)
                    .setImageQueueDepth(1)
                    .build()

处理一帧然后询问另一帧似乎更合理,但使用此策略它在每个 phone.

中都运行良好

image.mediaImage?.close()无法使用。只需 imageProxy?.close() 就足够了:它将释放包装的 MediaImage。参考 developer.android.com/training/camerax/analyze – 格里 11 月 19 日 1:01