Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stop mobile vision api after first detection

I'm trying to detect a QR code with the google mobile vision api.

The problem is that after detecting a QR code, the api calls continiously the "receiveDetections" function as long as the QR code is visible to the camera. I need to stop after the first detection and send the result to my server to validate this code. How can I stop the process after the first detection?

    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()
    }
like image 949
KayD Avatar asked Nov 07 '25 13:11

KayD


1 Answers

you can create function to stop camera,ex

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

        })

edit: create variable for flag detection at first like

//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
                }
            }

            })
        }
}

hope this help....

like image 194
adi purnama Avatar answered Nov 09 '25 06:11

adi purnama



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!