第一次检测后停止移动视觉 api

Stop mobile vision api after first detection

我正在尝试使用 google 移动视觉 api 检测二维码。

问题是检测到二维码后,只要二维码对摄像头可见,api就会不断调用"receiveDetections"函数。 我需要在第一次检测后停止并将结果发送到我的服务器以验证此代码。 如何在第一次检测后停止进程?

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.qrcode_scanner)

detector = BarcodeDetector.Builder(this).setBarcodeFormats(Barcode.ALL_FORMATS).build()
        detector.setProcessor(object: Detector.Processor<Barcode> {
            override fun release() {
            override fun receiveDetections(detections: Detector.Detections<Barcode>?) {

                val barcodes = detections?.detectedItems
                if(barcodes!!.size()>0) {
                  Log.e("qrcode",barcodes.valueAt(0).displayValue)
                  sendQRCodeToServer(url,barcodes.valueAt(0).displayValue)

                }
            }

        })


        cameraSource = CameraSource.Builder(this,detector).setRequestedPreviewSize(1920,1080).setRequestedFps(25f).setAutoFocusEnabled(true).build()

        svBarcode.holder.addCallback(object: SurfaceHolder.Callback2 {
            override fun surfaceRedrawNeeded(holder: SurfaceHolder?) {
            }
            override fun surfaceChanged(holder: SurfaceHolder?, format: Int, width: Int, height: Int) {
            }

            override fun surfaceDestroyed(holder: SurfaceHolder?) {
              cameraSource.stop()
            }



            override fun surfaceCreated(holder: SurfaceHolder?) {

                if(ContextCompat.checkSelfPermission(this@Scanner,
                                Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) {
                    cameraSource.start(holder)
                    startAnimation()

                } else ActivityCompat.requestPermissions(this@Scanner, arrayOf(Manifest.permission.CAMERA),123)

            }

        })
    }

         }

    override fun onDestroy() {
        super.onDestroy()
        detector.release()
        cameraSource.stop()
        cameraSource.release()
    }

您可以创建停止相机的功能,例如

private fun stopCamera(){
        cameraSource.stop()
    }

detector = BarcodeDetector.Builder(this).setBarcodeFormats(Barcode.ALL_FORMATS).build()
        detector.setProcessor(object: Detector.Processor<Barcode> {
            override fun release() {
            override fun receiveDetections(detections: Detector.Detections<Barcode>?) {

                val barcodes = detections?.detectedItems
                if(barcodes!!.size()>0) {
                  Log.e("qrcode",barcodes.valueAt(0).displayValue)
                  sendQRCodeToServer(url,barcodes.valueAt(0).displayValue)
                  //add this to stop camera 
                  stopCamera()
                }
            }

        })

编辑: 首先为标志检测创建变量,如

//to flag first detection
private var firstDetection=true

override fun onCreate(savedInstanceState: Bundle?) {
    ///.......
    detector = BarcodeDetector.Builder(this).setBarcodeFormats(Barcode.ALL_FORMATS).build()
        detector.setProcessor(object: Detector.Processor<Barcode> {
            override fun release() {

            }
            override fun receiveDetections(detections: Detector.Detections<Barcode>?) {

                val barcodes = detections?.detectedItems
                //check firstDetection
                if(barcodes!!.size()>0 && firstDetection) {
                    sendQRCodeToServer(url,barcodes.valueAt(0).displayValue)
                    //set firstDetection 
                    firstDetection=false
                }
            }

            })
        }
}

希望对您有所帮助....