首次检测后停止移动视觉api

问题描述 投票:0回答:1

我正在尝试使用Google移动视觉api检测QR码。

问题是,只要检测到QR码,api就会连续调用“ receiveDetections”功能,只要QR码对相机可见即可。第一次检测后,我需要停止并将结果发送到我的服务器以验证此代码。首次检测后如何停止该过程?

    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()
    }
android kotlin qr-code barcode-scanner
1个回答
0
投票
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
                }
            }

            })
        }
}

希望获得帮助。...

© www.soinside.com 2019 - 2024. All rights reserved.