如何降低 Android CameraX ImageAnalysis 的帧速率?

Abh*_*bhi 7 android android-camerax

如何在图像分析中将帧速率降低到 1 fps,这样我就不会多次检测到条形码。在我的用例中,以 1 秒的间隔多次扫描相同的条形码应该会增加一个计数器。所以我希望它能够正常工作。(类似于商店收银台的产品扫描仪)

cameraX版本:1.0.0-beta02

类似问题:

目前的实施:

https://proandroiddev.com/update-android-camerax-4a44c3e4cdcc
按照此文档,限制图像分析。

override fun analyze(image: ImageProxy) {
    val currentTimestamp = System.currentTimeMillis()
    if (currentTimestamp - lastAnalyzedTimestamp >= TimeUnit.SECONDS.toMillis(1)) {
        // Image analysis code
    }
    image.close()
}
Run Code Online (Sandbox Code Playgroud)

更好的解决方案会有所帮助。

Abh*_*bhi 5

尝试了 bmdelacruz 的解决方案。关闭图像时遇到问题。
遇到与此类似的错误。
无法让它工作。

使用delay对我来说效果很好。

代码

CoroutineScope(Dispatchers.IO).launch {
    delay(1000 - (System.currentTimeMillis() - currentTimestamp))
    imageProxy.close()
}
Run Code Online (Sandbox Code Playgroud)

完整的 BarcodeAnalyser 代码

class BarcodeAnalyser(
    private val onBarcodesDetected: (barcodes: List<Barcode>) -> Unit,
) : ImageAnalysis.Analyzer {
    private val barcodeScannerOptions = BarcodeScannerOptions.Builder()
        .setBarcodeFormats(Barcode.FORMAT_ALL_FORMATS)
        .build()
    private val barcodeScanner = BarcodeScanning.getClient(barcodeScannerOptions)
    var currentTimestamp: Long = 0

    override fun analyze(
        imageProxy: ImageProxy,
    ) {
        currentTimestamp = System.currentTimeMillis()
        imageProxy.image?.let { imageToAnalyze ->
            val imageToProcess =
                InputImage.fromMediaImage(imageToAnalyze, imageProxy.imageInfo.rotationDegrees)
            barcodeScanner.process(imageToProcess)
                .addOnSuccessListener { barcodes ->
                    // Success handling
                }
                .addOnFailureListener { exception ->
                    // Failure handling
                }
                .addOnCompleteListener {
                    CoroutineScope(Dispatchers.IO).launch {
                        delay(1000 - (System.currentTimeMillis() - currentTimestamp))
                        imageProxy.close()
                    }
                }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)