Search code examples
androidzxing

How to restart the camera in ZXing after it finds the QR code


When the Zxing finds a QR code it deliver it to handleResult function and it stops the camera. I need to restart the camera if the decoded QR code was already saved in my app. How to restart the camera again?


Solution

  • If you're using ZXing's ZXingScannerView you can use stopCameraPreview() in combination with stopCamera() when you're processing the QR Code and/or showing the result to your users. When your app/user is ready to scan again you just call setResultHandler() with startCamera() and resumeCameraPreview().

    Example:

    public void startScan() { //use this when you want to resume the camera
        if (scannerView != null) {
            scannerView.setResultHandler(this);
            scannerView.startCamera();
            rescan();
        }
    }
    
    public void stopScan() { //use this when you want to stop scanning
    // it is very important to do that,
    // because the camera will keep scanning codes in background
        if (scannerView != null) {
            scannerView.stopCameraPreview();
            scannerView.stopCamera();
        }
    }
    
    public void rescan() {
        if (scannerView != null) {
            scannerView.resumeCameraPreview(this);
        }
    }
    

    Hope this helps :)