Use MediaDevices.getUserMedia() with facingMode: environment as default
See original GitHub issueHello,
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:
- Created 3 years ago
- Reactions:2
- Comments:5 (3 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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.
You can play with this now: https://github.com/zxing-js/ngx-scanner/blob/42e6ad4f58fd235bd15d688d36050a4ea25fdc01/projects/zxing-scanner/src/lib/zxing-scanner.component.ts#L287-L302