mirror of
https://github.com/serratus/quaggaJS.git
synced 2026-08-12 21:21:42 +08:00
Added tests for selecting camera; related to #128
This commit is contained in:
@@ -0,0 +1,17 @@
|
||||
|
||||
export function enumerateDevices() {
|
||||
if (navigator.mediaDevices
|
||||
&& typeof navigator.mediaDevices.enumerateDevices === 'function') {
|
||||
return navigator.mediaDevices.enumerateDevices();
|
||||
}
|
||||
return Promise.reject(new Error('enumerateDevices is not defined'));
|
||||
};
|
||||
|
||||
export function getUserMedia(constraints) {
|
||||
if (navigator.mediaDevices
|
||||
&& typeof navigator.mediaDevices.getUserMedia === 'function') {
|
||||
return navigator.mediaDevices
|
||||
.getUserMedia(constraints);
|
||||
}
|
||||
return Promise.reject(new Error('getUserMedia is not defined'));
|
||||
}
|
||||
+16
-20
@@ -1,4 +1,5 @@
|
||||
import {omit, pick} from 'lodash';
|
||||
import {getUserMedia, enumerateDevices} from 'mediaDevices';
|
||||
|
||||
const facingMatching = {
|
||||
"user": /front/i,
|
||||
@@ -37,24 +38,19 @@ function waitForVideo(video) {
|
||||
* @param {Object} video
|
||||
*/
|
||||
function initCamera(video, constraints) {
|
||||
if (navigator.mediaDevices
|
||||
&& typeof navigator.mediaDevices.getUserMedia === 'function') {
|
||||
return navigator.mediaDevices
|
||||
.getUserMedia(constraints)
|
||||
.then((stream) => {
|
||||
return new Promise((resolve) => {
|
||||
streamRef = stream;
|
||||
video.setAttribute("autoplay", 'true');
|
||||
video.srcObject = stream;
|
||||
video.addEventListener('loadedmetadata', () => {
|
||||
video.play();
|
||||
resolve();
|
||||
});
|
||||
});
|
||||
})
|
||||
.then(waitForVideo.bind(null, video));
|
||||
}
|
||||
return Promise.reject(new Error('getUserMedia is not defined'));
|
||||
return getUserMedia(constraints)
|
||||
.then((stream) => {
|
||||
return new Promise((resolve) => {
|
||||
streamRef = stream;
|
||||
video.setAttribute("autoplay", 'true');
|
||||
video.srcObject = stream;
|
||||
video.addEventListener('loadedmetadata', () => {
|
||||
video.play();
|
||||
resolve();
|
||||
});
|
||||
});
|
||||
})
|
||||
.then(waitForVideo.bind(null, video));
|
||||
}
|
||||
|
||||
function deprecatedConstraints(videoConstraints) {
|
||||
@@ -80,7 +76,7 @@ function pickDevice(constraints) {
|
||||
if (!facingMatch) {
|
||||
return Promise.resolve(constraints);
|
||||
}
|
||||
return navigator.mediaDevices.enumerateDevices()
|
||||
return enumerateDevices()
|
||||
.then(devices => {
|
||||
const selectedDeviceId = devices
|
||||
.filter(device => device.kind === 'videoinput' && facingMatch.test(device.label))
|
||||
@@ -98,7 +94,7 @@ function pickDevice(constraints) {
|
||||
});
|
||||
}
|
||||
|
||||
function pickConstraints(videoConstraints) {
|
||||
export function pickConstraints(videoConstraints) {
|
||||
const normalizedConstraints = {
|
||||
audio: false,
|
||||
video: deprecatedConstraints(videoConstraints)
|
||||
|
||||
Reference in New Issue
Block a user