ML KIT条形码扫描仪不停止检测到

发布于 2025-02-12 16:31:37 字数 1108 浏览 1 评论 0原文

我正在尝试使用条形码扫描仪(RawValue和Format)从Camerax和ML套件中获取两个值,但是当检测到第一个时,我无法停止扫描并将数据插入房间。我的代码插入在房间中的代码插入数量是许多元素,因为在此过程中检测到许多条形码(相同的条形码很多次),

这是我在ProcessImageProxy函数中的addonSuccessListener:

   scanner.process(inputImage).addOnSuccessListener { barcodeList ->
                processBarcode(barcodeList)
            }

这是我保留数据的实际方法:

    private fun processBarcode(barcodeList: List<Barcode>) {
        if (barcodeList.isNotEmpty()) {
            with (barcodeList.first()) {

                activityCameraScannerViewModel.rawValue = this.rawValue.toString()
                activityCameraScannerViewModel.format = this.format.toString()
                activityCameraScannerViewModel.setNewCard()

                val intent = Intent(applicationContext, MainActivity::class.java)
                intent.putExtra("rawValue", this.rawValue.toString())
                intent.putExtra("format", this.format.toString())
                startActivity(intent)
            }
        }
    }

代码的实际结果很多。插入物(其中的随机数)。 感谢任何帮助。如果有人需要更多代码,我将编辑该问题。 谢谢。

I'm trying to get two values ​​from cameraX and ML Kit using the barcode scanner (rawValue and format) but I can't stop the scan and insert data into room when the first one is detected. My code inserts in room as many elements as many barcodes are detected in the process (the same barcode many times)

This is my addOnSuccessListener in the processImageProxy function:

   scanner.process(inputImage).addOnSuccessListener { barcodeList ->
                processBarcode(barcodeList)
            }

And this is my actual method to keep data:

    private fun processBarcode(barcodeList: List<Barcode>) {
        if (barcodeList.isNotEmpty()) {
            with (barcodeList.first()) {

                activityCameraScannerViewModel.rawValue = this.rawValue.toString()
                activityCameraScannerViewModel.format = this.format.toString()
                activityCameraScannerViewModel.setNewCard()

                val intent = Intent(applicationContext, MainActivity::class.java)
                intent.putExtra("rawValue", this.rawValue.toString())
                intent.putExtra("format", this.format.toString())
                startActivity(intent)
            }
        }
    }

The actual result of code is a lot of inserts (random number of them).
I would appreciate any help. If anybody need more code I will edit the question.
Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蓝眼睛不忧郁 2025-02-19 16:31:37

只需分辨率:

将扫描仪作为processbarcode函数的参数和调用

scanner.close()

完成代码:

 private fun processBarcode(barcodeList: List<Barcode>, scanner: BarcodeScanner) {
        if (barcodeList.isNotEmpty()) {
            with (barcodeList.first()) {

                val rawValue = this.rawValue.toString()
                val format = this.format.toString()
                activityCameraScannerViewModel.rawValue = rawValue
                activityCameraScannerViewModel.format = format

                activityCameraScannerViewModel.setNewCard()
                val intent = Intent(applicationContext, MainActivity::class.java)
                startActivity(intent)
                scanner.close()
            }
        }
    }

Simply resolution:

Pass scanner as parameter of processBarcode function and call

scanner.close()

Complete code:

 private fun processBarcode(barcodeList: List<Barcode>, scanner: BarcodeScanner) {
        if (barcodeList.isNotEmpty()) {
            with (barcodeList.first()) {

                val rawValue = this.rawValue.toString()
                val format = this.format.toString()
                activityCameraScannerViewModel.rawValue = rawValue
                activityCameraScannerViewModel.format = format

                activityCameraScannerViewModel.setNewCard()
                val intent = Intent(applicationContext, MainActivity::class.java)
                startActivity(intent)
                scanner.close()
            }
        }
    }
树深时见影 2025-02-19 16:31:37

如果使用相机提供商,请使用:

barcodeScanner.close();
cameraProvider.unbindAll();

If you use a camera provider, use:

barcodeScanner.close();
cameraProvider.unbindAll();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文