ngx-scanner icon indicating copy to clipboard operation
ngx-scanner copied to clipboard

How to stop scanner after scan complete

Open azurehari opened this issue 5 years ago • 25 comments

Please help me to resolve this issue. After scan complete i redirect to new page. but still site is taking the video input hence my device is getting so hot. i am using angular 8. Please explain me how to stop the video input once the scan is complete.

azurehari avatar Dec 06 '19 10:12 azurehari

I have the exact same issue

unitrix0 avatar Dec 12 '19 15:12 unitrix0

I have the same problem, and when he reads it, I hide the camera, but when I return the camera view, it doesn't come back.

Can you help us?

Arthurferrera avatar Dec 18 '19 19:12 Arthurferrera

Why is this not getting any attention?!

unitrix0 avatar Jan 11 '20 14:01 unitrix0

Hello! So you can stop the scanner element using it's API or by setting the scannerEnabled property to false.

To enable the scanner back you must set scannerEnabled to true and set the device again. "Why I need to set the device again?" - That's because the scanner component doesn't keep in memory the used device after disabling it, so it does not have a device to start scanning after disabled.

odahcam avatar Jan 11 '20 18:01 odahcam

@unitrix0 my personal life is not concern of anyone, but I'd like to say I was in vacation and before that I was dealing with matters of my own interest since this project does not helps me with any financial support sometimes I just can't give attention to it, sorry 😕.

odahcam avatar Jan 11 '20 18:01 odahcam

Hello! So you can stop the scanner element using it's API or by setting the scannerEnabled property to false.

To enable the scanner back you must set scannerEnabled to true and set the device again. "Why I need to set the device again?" - That's because the scanner component doesn't keep in memory the used device after disabling it, so it does not have a device to start scanning after disabled.

While this does stop the code detection, it is not what we're looking for. The problem is that the camera stream is still running. And there seems to be no way of ending that stream.

unitrix0 avatar Jan 12 '20 14:01 unitrix0

Stopping the scanner should stop the stream, if it doesn't we have a bug. Could you set up a demo for us to inspect?

odahcam avatar Jan 12 '20 18:01 odahcam

After trying around with various things I've found out that it is related with the "tryHarder" flag. If it is set the stream doesn't seem to end. More precisely I've initiated the scan like this:

export class CodeScanner2Component implements OnInit {
  @ViewChild(ZXingScannerComponent) scanner: ZXingScannerComponent;

  constructor() {
  }
  startScan() {
    this.scanner.updateVideoInputDevices().then(devices => {
      this.scanner.device = devices[0]; 
    });

    this.scanner.askForPermission().then(permission => {
      console.log('Permissions response: ' + permission);
      this.scanner.tryHarder = true;
    });
  }
}

However it does end if I do it this way:

export class CodeScanner2Component implements OnInit {
  @ViewChild(ZXingScannerComponent) scanner: ZXingScannerComponent;

  constructor() {
  }
  startScan() {
    this.scanner.updateVideoInputDevices().then(devices => {
      this.scanner.device = devices[0];
    });
    this.scanner.tryHarder = true;  // ************* Line moved
    this.scanner.askForPermission().then(permission => {
      console.log('Permissions response: ' + permission);      
    });
  }
}

unitrix0 avatar Jan 12 '20 18:01 unitrix0

That seems like fun. That probably happens due to the way Angular checks changes in the components.

odahcam avatar Jan 12 '20 19:01 odahcam

well I wouldn't call it fun... I tried to follow the try harder flag through the code but I lost track... Maybe an issue in the way it is processed

unitrix0 avatar Jan 17 '20 09:01 unitrix0

Hey Guys did you find any solution to stop video stream after scan complete , I am also facing same problem can't hide camera after scan completed

aniketpatil0315 avatar Mar 13 '20 11:03 aniketpatil0315

yes, as I wrote in my last post, setting the "try harder" Option to false solved the issue for me.

unitrix0 avatar Mar 13 '20 12:03 unitrix0

<zxing-scanner *ngIf="!qrResultString"... Before scan complete this property is null, as you initialized it.

sandrumirceaioan avatar Aug 26 '20 19:08 sandrumirceaioan

Okay, Gotcha. Thanks for reply.

On 27/08/2020, Sandru Mircea Ioan [email protected] wrote:

<zxing-scanner *ngIf="!qrResultString"... Before scan complete this property is null, as you initialized it.

-- You are receiving this because you commented. Reply to this email directly or view it on GitHub: https://github.com/zxing-js/ngx-scanner/issues/267#issuecomment-681091654

aniketpatil0315 avatar Aug 27 '20 09:08 aniketpatil0315

I have this problem , i don't stop the stream camera.can anyone help me?

Bomber91 avatar Sep 17 '20 15:09 Bomber91

This works form me. As soon as you fetch a result you can stop the camera using .reset() method: async handleQrCodeResult(resultString: string) { this.qrResultString = resultString; this.scanner.reset(); }

sandrumirceaioan avatar Sep 21 '20 14:09 sandrumirceaioan

@sandrumirceaioan, I tried your solution but getting TypeError: Cannot read property 'length' of undefined

sanjayhanumegowda avatar Oct 29 '20 15:10 sanjayhanumegowda

Referring to @odahcam suggestion of setting scannerEnabled I am somewhat taken aback. Using the code for multi-camera that variable is not set in the embedded javascript. it is quite clear that codeReader.decodeFromVideoDevice(selectedDeviceId, 'video', (result, err) => { is intended to scan continuously consistent with the logging console.log('Started continous decode from camera with id ${selectedDeviceId}'). The string scannerEnabled is not found in this repository. So I fail to see how the suggestion would allow to toggle the scanner off (based on a result), then back on on user command.

@sandrumirceaioan 's suggestion sounds interesting but it is not obvious to me where to invoke it in the linked example and whether this would conflict with the document.getElementById('resetButton') block.

Some other suggestions refer to another verb barcodeView.decodeContinuous(callback); leading to a thought that library dependencies are at play. Is there a list of commands, based on the context that one can refer to ?

dvodvo avatar Feb 08 '21 08:02 dvodvo

@dvodvo

The string scannerEnabled is not found in this repository.

It just got renamed: https://github.com/zxing-js/ngx-scanner/blob/42e6ad4f58fd235bd15d688d36050a4ea25fdc01/projects/zxing-scanner/src/lib/zxing-scanner.component.ts#L338

odahcam avatar Feb 09 '21 03:02 odahcam

I kinda found a solution.

<zxing-scanner *ngIf="showScanner" #scanner (scanSuccess)="scanSuccessHandler($event)"> </zxing-scanner>

scanSuccessHandler(event){ this.showScanner = false; this.showScanBtn = true; console.log(event); //Other logics }

The above approach closes the scanner after a single successful scan. Not sure if it is exactly the solution you all are looking for but I hope it helps.

bratinkundu avatar Apr 01 '21 13:04 bratinkundu

I used the solution of @bratinkundu.

I just wrapped the zxing-scanner html like :

<div class="qr-scan-area" *ngIf="showScanner">
  <zxing-scanner
    class="area"
    #scanner
    [(device)]="currentDevice"
    (scanSuccess)="onCodeResult($event)"
    (permissionResponse)="onHasPermission($event)"
    [formats]="allowedFormats">
  </zxing-scanner>
  <div class="qr-area" *ngIf="hasPermission">
    <div class="area"></div>
  </div>
</div>

I settled a variable showScanner to true in the file.ts When the scan is successful onCodeResult( ) then toggle showScanner to false. My camera's macbook turned off.

I also got a button which trigger reset of QRresult and this function toggle showScanner to true then my camera turned on.

Paul-D-Dev avatar Jun 18 '21 14:06 Paul-D-Dev

@sandrumirceaioan's solution worked for me. Once I set this.scanner.reset(); in the handleQrCodeResult method, the camera loaded faster on each iteration and I observed that scanning was quicker.

shreya-sawardekar avatar Oct 25 '21 07:10 shreya-sawardekar

For me, what solved this issue was to manually set this.scanner.enable = false; within my ts file.

None of the other approaches mentioned here worked so far.

celsomtrindade avatar Oct 26 '21 10:10 celsomtrindade

I used stopScan() which stopped the scan plus I added a timeout followed by a startScan() just in case I there was a problem with what was scanned...

onCodeResult(result: string) {
    this.operator_details.emit(result); 
    this.scanner.scanStop()
    setTimeout(() => {
      this.scanner.scanStart()
    },3000)
  }

Ken-mbira avatar Jun 02 '22 09:06 Ken-mbira

@Ken-mbira

@ViewChild(ZXingScannerComponent) scanner: ZXingScannerComponent;
onCodeResult(result: string) {
 this.operator_details.emit(result); 
   this.scanner.scanStop()
   setTimeout(() => {
     this.scanner.scanStart()
   },3000)
 }

This is worked for me. Thanks.

sanathpathiraja avatar Aug 25 '22 21:08 sanathpathiraja

Even if i use this.scanner.scanStop() or this.scanner.reset(); or *ngIf, still multiple API's are getting trigger (means multiple scans are happening)

NagendraMNK avatar Nov 22 '22 06:11 NagendraMNK

In my case i was using angular and this helped to close the camera in background

await navigator.mediaDevices.getUserMedia({ audio:false, video: true }).then((stream: any) => { const track = stream.getTracks()[0]; track.stop(); });

suryakantarora avatar Feb 12 '24 09:02 suryakantarora