question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Angular 8 - camera loading error

See original GitHub issue

Unable to find the scanner either on init or AfterviewInit after upgrade to Angular 8, Zxing - 1.7

typescript :34.5

It works perfect with Angular 7.

HTML

   <zxing-scanner #scanner [autostart]="true"
           [device]="currentDevice" (scanFailure)="handleScanFailure($event)"  (scanError)="handleScanError($event)" (scanComplete)="handleScanComplete($event)" (scanSuccess)="handleScanSuccess($event)" [formats]="allowedFormats"
         ></zxing-scanner>

TS (with static: true)

 @ViewChild('scanner',{static:false}) scanner: ZXingScannerComponent;

 private initCamera(): void {
    this.scanner.camerasFound.subscribe((devices: MediaDeviceInfo[]) => {
      this.hasDevices = true;
      this.availableDevices = devices;
      if (this.availableDevices.length > 1) {
        const defaultCamera = this.availableDevices.filter(e => e.label.toLocaleLowerCase().indexOf('back') > -1);
        if (defaultCamera !== null && defaultCamera !== undefined) {
          this.currentDevice = defaultCamera[0];
        } else {
           this.currentDevice = this.availableDevices[0];
        }
      } else {
        this.currentDevice = this.availableDevices[0];
      }
    });
  }

Error: The device must be a valid MediaDeviceInfo or null.


Edit: added code formatting

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
ymallikreddy521commented, Jun 11, 2019

Sure.

HTML:

     <zxing-scanner  #scanner  [autofocusEnabled]="true" [autostart]="false" [enable]="true"  [device]="currentDevice"
          (scanComplete)="handleScanComplete($event)" [formats]="allowedFormats"
          [tryHarder]="true"></zxing-scanner>

TS:

 @ViewChild('scanner',{static:true, read: false}) scanner: ZXingScannerComponent;

This is change and must to set to NULL
 currentDevice: MediaDeviceInfo = null;

ngOnInit() {
    this.onPageInit();
  }

  private onPageInit(){
    this.initCamera();
    this.scanner.permissionResponse.subscribe(
      (perm: boolean) =>{
       this.hasPermission = perm;
      });

    this.allowedFormats = [
      BarcodeFormat.AZTEC,
      BarcodeFormat.CODABAR,
      BarcodeFormat.CODE_39,
      BarcodeFormat.CODE_93,
      BarcodeFormat.CODE_128,
      BarcodeFormat.DATA_MATRIX,
      BarcodeFormat.EAN_8,
      BarcodeFormat.EAN_13,
      BarcodeFormat.ITF,
      BarcodeFormat.MAXICODE,
      BarcodeFormat.PDF_417,
      BarcodeFormat.QR_CODE,
      BarcodeFormat.RSS_14,
      BarcodeFormat.RSS_EXPANDED,
      BarcodeFormat.UPC_A,
      BarcodeFormat.UPC_E,
      BarcodeFormat.UPC_EAN_EXTENSION
    ];
    this._loggerService.log('inside the init camera');
  }

  private initCamera(): void {
    this.scanner.camerasFound.subscribe((devices: MediaDeviceInfo[]) => {
      this.hasDevices = true;
      this.availableDevices = devices;
      this.currentDevice = null;
      if (this.availableDevices.length > 1) {
        const defaultCamera = this.availableDevices.filter(e => e.label.toLocaleLowerCase().indexOf('back') > -1);
        if (defaultCamera !== null && defaultCamera !== undefined) {
          this.currentDevice = defaultCamera[0];
        } else {
           this.currentDevice = this.availableDevices[0];
        }
      } else {
        this.currentDevice = this.availableDevices[0];
      }
    });
  }

Edit: added propper syntax highlighting.

0reactions
odahcamcommented, Jun 11, 2019

Thank you @ymallikreddy521 . I’m locking this now for clarity, please anyone reading this feel free to open new issues.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Problem running an Angular 4 project and in using web-cam ...
it's causing problems using the webcam and showing this error: [Deprecation] getUserMedia() no longer works on insecure origins. To use this ...
Read more >
NG0100: Expression has changed after it was checked - Angular
This error commonly occurs when you've added template expressions or have begun to implement lifecycle hooks like ngAfterViewInit or ngOnChanges . It is...
Read more >
Ngx Webcam with angular | Capturing image the live image
In this video you will learn how to access webcam in angular. How to capture image and preview an image using ngx-webcam in...
Read more >
ngx-webcam - npm
A simple Angular webcam component. Pure & minimal, no Flash-fallback. See the Demo! Plug-and-play. This library contains a single module ...
Read more >
Angular Debugging "Expression has changed": Explanation ...
And here is what the component looks like when the data is loading: Material Data Table. To find out why the "Expression has...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found