mirror of
https://github.com/serratus/quaggaJS.git
synced 2026-08-13 13:41:54 +08:00
Merged master to 1.0
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'));
|
||||
}
|
||||
@@ -24,3 +24,28 @@ Math.imul = Math.imul || function(a, b) {
|
||||
// the final |0 converts the unsigned value into a signed value
|
||||
return ((al * bl) + (((ah * bl + al * bh) << 16) >>> 0) | 0);
|
||||
};
|
||||
|
||||
if (typeof Object.assign !== 'function') {
|
||||
Object.assign = function(target) { // .length of function is 2
|
||||
'use strict';
|
||||
if (target === null) { // TypeError if undefined or null
|
||||
throw new TypeError('Cannot convert undefined or null to object');
|
||||
}
|
||||
|
||||
var to = Object(target);
|
||||
|
||||
for (var index = 1; index < arguments.length; index++) {
|
||||
var nextSource = arguments[index];
|
||||
|
||||
if (nextSource !== null) { // Skip over if undefined or null
|
||||
for (var nextKey in nextSource) {
|
||||
// Avoid bugs when hasOwnProperty is shadowed
|
||||
if (Object.prototype.hasOwnProperty.call(nextSource, nextKey)) {
|
||||
to[nextKey] = nextSource[nextKey];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return to;
|
||||
};
|
||||
}
|
||||
|
||||
+44
-22
@@ -1,4 +1,10 @@
|
||||
import {pick} from 'lodash';
|
||||
import {omit, pick} from 'lodash';
|
||||
import {getUserMedia, enumerateDevices} from 'mediaDevices';
|
||||
|
||||
const facingMatching = {
|
||||
"user": /front/i,
|
||||
"environment": /back/i
|
||||
};
|
||||
|
||||
var streamRef;
|
||||
|
||||
@@ -32,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) {
|
||||
@@ -68,16 +69,28 @@ function deprecatedConstraints(videoConstraints) {
|
||||
return normalized;
|
||||
}
|
||||
|
||||
function pickConstraints(videoConstraints) {
|
||||
return {
|
||||
export function pickConstraints(videoConstraints) {
|
||||
const normalizedConstraints = {
|
||||
audio: false,
|
||||
video: deprecatedConstraints(videoConstraints)
|
||||
};
|
||||
|
||||
if (normalizedConstraints.video.deviceId
|
||||
&& normalizedConstraints.video.facingMode) {
|
||||
delete normalizedConstraints.video.facingMode;
|
||||
}
|
||||
return Promise.resolve(normalizedConstraints);
|
||||
}
|
||||
|
||||
function enumerateVideoDevices() {
|
||||
return enumerateDevices()
|
||||
.then(devices => devices.filter(device => device.kind === 'videoinput'));
|
||||
}
|
||||
|
||||
export default {
|
||||
request: function(video, videoConstraints) {
|
||||
return initCamera(video, pickConstraints(videoConstraints));
|
||||
return pickConstraints(videoConstraints)
|
||||
.then(initCamera.bind(null, video));
|
||||
},
|
||||
release: function() {
|
||||
var tracks = streamRef && streamRef.getVideoTracks();
|
||||
@@ -85,5 +98,14 @@ export default {
|
||||
tracks[0].stop();
|
||||
}
|
||||
streamRef = null;
|
||||
},
|
||||
enumerateVideoDevices,
|
||||
getActiveStreamLabel: function() {
|
||||
if (streamRef) {
|
||||
const tracks = streamRef.getVideoTracks();
|
||||
if (tracks && tracks.length) {
|
||||
return tracks[0].label;
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
+1
-1
@@ -1,5 +1,4 @@
|
||||
import './common/typedefs';
|
||||
import 'webrtc-adapter';
|
||||
import createScanner from './scanner';
|
||||
import ImageWrapper from './common/image_wrapper';
|
||||
import ImageDebug from './common/image_debug';
|
||||
@@ -134,4 +133,5 @@ function createApi(configuration = Config) {
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
export default createApi();
|
||||
|
||||
Reference in New Issue
Block a user