Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to deactivate the camera stream #23

Open
blakemcdermott opened this issue Jan 8, 2020 · 2 comments
Open

Unable to deactivate the camera stream #23

blakemcdermott opened this issue Jan 8, 2020 · 2 comments

Comments

@blakemcdermott
Copy link

My assumptions would be that using .removeFrom( htmlElement ) or .stopScanning() would deactivate the camera stream; however, I'm not having success with this. Does anyone have any suggestions on how to deactivate the camera stream setup when using .appendTo( htmlElement )?

@froger-me
Copy link

I think you need to make sure the call to .removeFrom( htmlElement ) is done when .appendTo( htmlElement ) has finished executing, seems to happen async. Calling one right after the other and putting log calls in the browser support cache file like below shows the scanner is undefined. That was my issue.

function $stopWebcam(scanner){
  console.log(scanner);
  if (scanner.videoStream) {
    var stream = scanner.videoStream;
    stream.stop?stream.stop():stream.getTracks && stream.getTracks().forEach(function(track){
      track.stop();
    }
    );
    scanner.videoStream = null;
  }
}

@abdulwahidgul24085
Copy link

This is how I am doing it with Jquery Attached to modal shown and hide

jb_scanner = '';
        //this function will be called when JsQRScanner is ready to use
        $('#qrcode').on('shown.bs.modal', function JsQRScannerReady() {
            //create a new scanner passing to it a callback function that will be invoked when
            //the scanner succesfully scan a QR code
            var jbScanner = new JsQRScanner(onQRCodeScanned);
            //var jbScanner = new JsQRScanner(onQRCodeScanned, provideVideo);
            //reduce the size of analyzed image to increase performance on mobile devices
            jbScanner.setSnapImageMaxSize(300);
            jbScanner.setScanInterval(300);
            var scannerParentElement = document.getElementById("scanner");
            if (scannerParentElement) {
                //append the jbScanner to an existing DOM element
                jbScanner.appendTo(scannerParentElement);
            }
            jb_scanner =  jbScanner;
        });

        $('#qrcode').on('hide.bs.modal', function() {
            if($('.qrPreviewVideo').length){
                $('.qrPreviewVideo').remove();
                var scannerParentElement = document.getElementById("scanner");
                if(scannerParentElement){
                    jb_scanner.removeFrom(scannerParentElement);
                    jb_scanner.stopScanning();
                }
            }
        })

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants