0

I've got this QR scanner on my component and it works, but has a problem: in some devices (mostly devices with 3 or more cameras like iPhone Pro) it doesn't get the camera for short distances (the one used by the default camera to scan QR codes) and it takes a lot to scan them. You can't be too close or it'll be blurry, and when on focus you need to angle it perfectly or else it doesn't get scanned. I've tried to make it take the right camera with onCamerasFound, but it doesn't seem to work. What am I doing wrong? If you need more info on the code or in general feel free to ask.

HTML:

<zxing-scanner 
  [device]="selectedDevice" 
  (camerasFound)="onCamerasFound($event)"
  (scanSuccess)="onQrCodeScanComplete($event)"
  style="width: 100%; max-height: 50vh;">
</zxing-scanner>

TS:

 onCamerasFound(devices: MediaDeviceInfo[]) {
    if (devices.length > 0) {
      const backCameras = devices.filter(device => 
        device.label.toLowerCase().includes('back') || 
        device.label.toLowerCase().includes('rear')
      );
      this.selectedDevice = backCameras.length > 0 ? backCameras[0] : devices[0];
    }
  }
4
  • Similar: stackoverflow.com/questions/59636464/… Commented Feb 5 at 17:00
  • Seems like picking the first camera out of the list (backCameras[0]/devices[0]) isn't the way to go. Could you add a way for the user to switch between the available rear cameras to find one that works for them? Commented Feb 5 at 17:02
  • This answer suggests that manufacturers usually report the "base" camera as the last camera in the list, though the iPhone 15 Pro Max apparently breaks this trend. If a switcher isn't desirable, maybe the last camera (backCameras[backCameras.length - 1]/devices[devices.length - ]) would be a better default to try Commented Feb 5 at 17:05
  • @DM sadly I wanted to add a switch but the client doesn't want it, preferring it to automatically choose the camera. I'll try the length - 1 method and'll let you know, and thanks for now! Commented Feb 6 at 9:50

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.