簡體   English   中英

條碼和二維碼掃描不適用於 CameraX / ZXing

[英]Barcode & Qr code scanning not working with CameraX / ZXing

我正在嘗試在我的應用程序中實現 QR 碼 + 條碼掃描 function,但我只能掃描 QR 碼。

我使用 ZXing 作為掃描庫和 CameraX 的相機功能。 為了分析 CameraX 捕獲的圖像,我使用自定義 ImageAnalysis.Analyzer class 來檢測和解碼 QR 碼 + 條形碼(不起作用)。

這是我的分析器:

class MyCodeImageAnalyzer(
    private val onCodeDetected: (code: Result) -> Unit
) : ImageAnalysis.Analyzer {

    private val yuvFormats = mutableListOf(YUV_420_888)

    init {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            yuvFormats.addAll(listOf(YUV_422_888, YUV_444_888))
        }
    }

    private val reader = MultiFormatReader()

    override fun analyze(image: ImageProxy) {
        // We are using YUV format because, ImageProxy internally uses ImageReader to get the image
        // by default ImageReader uses YUV format unless changed.

        if (image.format !in yuvFormats) {
            Timber.e("QRCodeAnalyzer Expected YUV, now = ${image.format}")
            return
        }

        val data = image.planes[0].buffer.toByteArray()

        val source = PlanarYUVLuminanceSource(
            data,
            image.width,
            image.height,
            0,
            0,
            image.width,
            image.height,
            false
        )

        val binaryBitmap = BinaryBitmap(HybridBinarizer(source))
        try {
            // Whenever reader fails to detect a QR code in image
            // it throws NotFoundException
            val result = reader.decode(binaryBitmap)
            onCodeDetected(result)
        } catch (e: NotFoundException) {
            e.printStackTrace()
        }
        image.close()
    }
}

我使用 MultiFormatReader,它考慮了多種格式,包括 QR 碼和條形碼。 我認為問題出在 yuvFormats 列表中,但我不知道要在其中添加什么。

您可以使用 Google ML Kit 掃描 QR/條形碼。

請在這里查看我的文章。 您可以在其中了解如何使用 Google ML Kit 和 Jetpack CameraX 創建條碼掃描儀。

這是處理它的最新方法,而且太容易了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM