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.

BrowserQRCodeReader doesn't focus when I use rear camera

See original GitHub issue

Hi! 😋

Firstly, thanks for developing this great library. My problem is the following, I’m developing a web app that allows user to scan QR codes.

===================================================================================

async function scanCode() {
    try {
      const codeReader = new BrowserQRCodeReader();

      const previewElem = document.querySelector(
        "#test-area-qr-code-webcam > video"
      );

      const controls2 = await codeReader.decodeOnceFromConstraints(
        {
          video: {
            aspectRatio: 1.7777777778,
            facingMode: 'environment',
            focusMode: 'continuous',
            frameRate: { ideal: 30, max: 60 },
            width: { min: 640, ideal: 640, max: 640 },
          },
          audio: false
        },
        previewElem
      )

      setSerialNumber(controls2.text)
    } catch (error) {
      console.log(error);
    }
 }

The result is this: photo_2021-06-04_12-51-51

And if I just set the following config, app uses front camera and autofocus works perfectly fine but, scan codes using front camera is quite uncomfortable.

const controls2 = await codeReader.decodeOnceFromConstraints(
    {
        video: true,
        audio: false,
    },
    previewElem
)

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
fydeliocommented, Jun 8, 2021

I have been playing around with library for quite some time and here is what I’ve learnt.

In the past it was easy. Smartphone had just two cameras. A front-facing and a back-facing. But now with mobiles becoming more sophisticated, suddenly they have multiple lenses, wide lenses and zoom lenses. Unfortunately the browser / smartphone / library isnt always correct when choosing a default lense (I’ve experienced problems mostly with Samsung phones), with some lenses being unusable for close up Scanning, as they are unfocused. Basically the only solution I found is providing a dropdown to the user, where he can select his lenses (and potentially store the selection in a cookie).

Curious: Did someone else come up with a different solution?

0reactions
fydeliocommented, Jun 9, 2021

Yes, of course

How to display a select box with the various lenses

var videoSourceSelect = document.getElementById('videoSourceSelect')

BrowserCodeReader.listVideoInputDevices()
.then((videoInputDevices) => {

    if (videoInputDevices.length >= 1) {
        videoInputDevices.forEach((element) => {
            const sourceOption = document.createElement('option')
            sourceOption.text = element.label
            sourceOption.value = element.deviceId
            videoSourceSelect.appendChild(sourceOption)
        })
    }
})

When loading the codeReader, do as follows:

// get the selected id of the camera
let device_id = var videoSourceSelect = document.getElementById('videoSourceSelect').value
// load the codeReader with the according device_id
const controls = await codeReader.decodeFromVideoDevice(device_id, previewElem, (result, error, controls) => {
});

If it helps (and not published yet): I’m trying to help improve the documentation. Here is a branch with a more enriched documentation. Have a look at it. You might get some more ideas (and maybe some feedback): https://github.com/fydelio/browser/tree/improved_documentation

Read more comments on GitHub >

github_iconTop Results From Across the Web

SOLVED: Rear facing camera won't auto focus - iPhone 8 - iFixit
The motor is damaged on the rear camera so it can't focus. This happens usually after the phone is dropped numerous times.
Read more >
Camera doesn't focus anymore | XDA Forums
I changed the rear cameras,but now sometimes it takes good picture and sometimes not. I reset it and no change.What should I do?...
Read more >
Lenovo-Duet-rear-camera-doesn-t-focus - English Community
Lenovo Duet rear camera doesn't focus. When taking photos the rear camera does not focus / autofocus, everything is blurred. Front camera ...
Read more >
[Solved] Why iPhone Camera Won't Focus and How to Fix It
How to Fix iPhone Camera Won't Focus Issue. 1. Tap the iPhone. Use a piece of napkin, or just your palm to tap...
Read more >
Surface book Rear camera not focusing - Microsoft Community
I am facing a problem with the rear camera not being able to take any ... You can follow the question or vote...
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