android - 如何降低 Android CameraX ImageAnalysis 的帧率?

标签 android android-camerax

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

cameraX 版本:1.0.0-beta02

类似问题:

  • How to increase frame rate with Android CameraX ImageAnalysis?
    在此,答案基于 cameraX alpha 版本中的预览配置和分析配置 Builders。但它在 beta 版本中找不到。
  • How to pause barcode scanning in android ML-kit when using ByteBuffer from SurfaceView
    在这种情况下,图像分析受到限制,但我希望对图像分析器的调用以较低的帧速率进行。即从图像分析用例到图像分析器的调用应该每秒一次。

  • 当前实现:

    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()
    }
    

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

    最佳答案

    尝试了 bmdelacruz 的解决方案。关闭图像时遇到问题。
    收到类似于 this 的错误.
    无法让它工作。
    使用 delay对我来说效果很好。
    代码

    CoroutineScope(Dispatchers.IO).launch {
        delay(1000 - (System.currentTimeMillis() - currentTimestamp))
        imageProxy.close()
    }
    
    完整的条码分析器代码
    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()
                        }
                    }
            }
        }
    }
    

    关于android - 如何降低 Android CameraX ImageAnalysis 的帧率?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61252975/

    相关文章:

    android 图片保存到 res/drawable 文件夹

    android - 即使在提供 "app:layout_scrollFlags"标志后,工具栏也不会滚动

    android - 如何拍摄手动设置了包括闪光灯在内的所有设置而又不丢失包含全闪光灯的图像的照片?

    android - CameraX Image 拍照慢

    android - 如何设置工具栏文本和后退箭头颜色

    android - 在前台或后台运行时在整个应用程序中保持 Socket.IO 连接

    android - 检查 Android 中的用户是否已授予权限

    android - 如何在 CameraX 中镜像 PreviewView?

    android-studio - 当我使用 camerax "1.0.0-alpha06"时,为什么 zoomRatio 会显示错误 Unresolved reference ?

    uri - OnImageSavedCallback返回的OutputFileResults具有无效的Uri