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.

Use MediaDevices.getUserMedia() with facingMode: environment as default

See original GitHub issue

Hello,

I’m trying to use only rear camera, but the following RegEx pattern is not working for alle devices supporting different languages.

Why is facingMode: environment not used as a default? You just hand over deviceId = null to getStreamForDevice. But right now it’s only possible to hand over a MediaDevice

const matcher = ({ label }) => /back|trás|rear|traseira|environment|ambiente/gi.test(label);

Is the following method supported? I can’t find it in class BrowserMultiFormatContinuousReader, but it is used in ZXingScannerComponent. const devices = await this.getCodeReader().listVideoInputDevices() || [];

  /**
   * Gets the media stream for certain device.
   * Falls back to any available device if no `deviceId` is defined.
   */
  public async getStreamForDevice({ deviceId }: Partial<MediaDeviceInfo>): Promise<MediaStream> {
    const constraints = this.getUserMediaConstraints(deviceId);
    const stream = await navigator.mediaDevices.getUserMedia(constraints);
    return stream;
  }

  /**
   * Creates media steram constraints for certain `deviceId`.
   * Falls back to any environment available device if no `deviceId` is defined.
   */
  public getUserMediaConstraints(deviceId: string): MediaStreamConstraints {

    const video = typeof deviceId === 'undefined'
      ? { facingMode: { exact: 'environment' } }
      : { deviceId: { exact: deviceId } };

    const constraints: MediaStreamConstraints = { video };

    return constraints;
  }

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:2
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
marteacommented, Oct 1, 2020

Also confirm that this would be great, or anycase define media contraints as input for the component, so one can control the search preferences, this is way better to look for “back cameras”

apply MediaStreamConstraints as @input parameter and apply it to when calling for the device. this is for example what i want to achive.


var mediaConstraint = {
      video: {
width: {
           min: 1280,
         ideal: 1920,
           max: 2560,
         },
         height: {
           min: 720,
           ideal: 1080,
           max: 1440
         },
        facingMode: 'environment'
      }

Read more comments on GitHub >

github_iconTop Results From Across the Web

MediaTrackConstraints.facingMode - Web APIs | MDN
The video source is facing toward the user; this includes, for example, the front-facing camera on a smartphone. "environment". The video source ...
Read more >
javascript - GetUserMedia - facingmode - Stack Overflow
mediaDevices. ... facingMode == "environment" ||tempDevice.label. ... I use the following for webcam.js, call LoadWebcam ()
Read more >
How to choose between front and back camera stream
Selecting facing mode of the video stream. When we were acquiring the stream, we passed an object as a getUserMedia parameter. This object...
Read more >
How To Access Front and Rear Cameras with JavaScript's ...
The getUserMedia API makes use of the media input devices to produce a ... you can alter the facingMode property to environment ....
Read more >
Choosing cameras in JavaScript with the mediaDevices API
To use the facingMode constraint we need to change the constraints we are using in our call to getUserMedia . Rather than just...
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