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

Camera cannot focus properly #308

Closed
amerfathullah opened this issue Oct 11, 2021 · 8 comments
Closed

Camera cannot focus properly #308

amerfathullah opened this issue Oct 11, 2021 · 8 comments

Comments

@amerfathullah
Copy link

Describe the bug
Camera cannot focus properly on Samsung device on Firefox browser. It's working fine on Chrome and other devices. For my test, since the size of QR code is small on the object, I set the qrbox small too qrbox=250.

Expected behavior
Camera should properly focus on the QR code.

Screenshots
zoom

Smartphone:

  • Device: Samsung Galaxy Note5
  • OS: Android 7.0
  • Browser Firefox
  • Version 92.1.1
@amerfathullah
Copy link
Author

amerfathullah commented Oct 12, 2021

fixed by setting focusMode: "continuous" in video constraints

@ROBERT-MCDOWELL
Copy link

could you provide the piece of code where you integrated this extra code?

@amerfathullah
Copy link
Author

I just do it like this

this.html5QrCode.applyVideoConstraints({ focusMode: "continuous", advanced: [ {zoom: isAndroid ? 1.5 : 2.0 } ]})

can refer below on how to use it
https://developer.mozilla.org/en-US/docs/Web/API/MediaStreamTrack/applyConstraints
https://developer.mozilla.org/en-US/docs/Web/API/MediaDevices/getUserMedia
https://developer.mozilla.org/en-US/docs/Web/API/MediaTrackConstraints

@ROBERT-MCDOWELL
Copy link

ok thanks, but be aware that call videoconstraints manually override all other settings when you create the qrcode object like framerate, aspect ratio etc... (according to the html5-qrcode help) on the README page)

@antonyf
Copy link

antonyf commented Apr 18, 2022

I just do it like this

this.html5QrCode.applyVideoConstraints({ focusMode: "continuous", advanced: [ {zoom: isAndroid ? 1.5 : 2.0 } ]})

can refer below on how to use it https://developer.mozilla.org/en-US/docs/Web/API/MediaStreamTrack/applyConstraints https://developer.mozilla.org/en-US/docs/Web/API/MediaDevices/getUserMedia https://developer.mozilla.org/en-US/docs/Web/API/MediaTrackConstraints

where did you add this code?

@nicocirio
Copy link

nicocirio commented May 9, 2022

where did you add this code?

Hi @antonyf . I applied it 2 sec after the code reader started, as it seems the camera must be running to be able to apply the video constraints (focusMode and zoom). I am not sure if this is the best way to make it work, but ir worked for my usecase (mobile device running Android)...

 html5QrCode.start({ facingMode: "environment" }, config, onScanSuccess);

      // wait 2 seconds to guarantee the camera has already started to apply the focus mode and zoom...
      setTimeout(function () {
        html5QrCode.applyVideoConstraints({
          focusMode: "continuous",
          advanced: [{ zoom: 2.0 }],
        });
      }, 2000);

I share the whole code for you to get an idea of the context were I applied it (a Phoenix web app). I have many barcode readers rendered, that is why my code has a dinamic_id...

app.js

Hooks.scanner = {
  mounted() {
    const view = this;

    var head_dinamic_id = "qr-reader";
    var tail_dinamic_id = this.el.id.substring(8);
    var dinamic_id = head_dinamic_id.concat(tail_dinamic_id);
    var html5QrCode = new Html5Qrcode(dinamic_id);

    this.el.addEventListener("click", startScanner);

    function startScanner() {
      const config = {
        fps: 100,
        qrbox: 200,
        aspectRatio: 1,
        formatsToSupport: [Html5QrcodeSupportedFormats.EAN_13],
      };
     
      html5QrCode.start({ facingMode: "environment" }, config, onScanSuccess);

      // wait 2 seconds to guarantee the camera has already started to apply the focus mode and zoom...
      setTimeout(function () {
        html5QrCode.applyVideoConstraints({
          focusMode: "continuous",
          advanced: [{ zoom: 2.0 }],
        });
      }, 2000);
    }

    function onScanSuccess(decodedText, _decodedResult) {
      html5QrCode.stop();
      html5QrCode.clear();
      view.pushEvent("scan_result", { result: decodedText });
    }
  },
};

@antonyf
Copy link

antonyf commented May 9, 2022 via email

@clzola
Copy link

clzola commented Oct 23, 2022

@nicocirio do you have maybe an iphone around you test does it work for you on android. I have copied your code, set 3s delay just to be sure. Also I have put alert box before applying constraints just to see if function is executed, however I still cannot get camera to focus properly. Also since I have put zoom to be 2.0, I guess I should notice zoom after video constraints are applied, however nothing happens...

I guess best solution would be just to have zoom applied, that way picture would be bigger and phone would not need to be so closed to the code...

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

5 participants