mirror of
https://github.com/serratus/quaggaJS.git
synced 2026-08-12 21:21:42 +08:00
Configuring camera-resolution
This commit is contained in:
@@ -167,7 +167,12 @@ The default `config` object is set as followed:
|
|||||||
```javascript
|
```javascript
|
||||||
{
|
{
|
||||||
inputStream: { name: "Live",
|
inputStream: { name: "Live",
|
||||||
type: "LiveStream"
|
type: "LiveStream",
|
||||||
|
constraints: {
|
||||||
|
width: 640,
|
||||||
|
height: 480,
|
||||||
|
facing: "environment"
|
||||||
|
}
|
||||||
},
|
},
|
||||||
tracking: false,
|
tracking: false,
|
||||||
debug: false,
|
debug: false,
|
||||||
|
|||||||
Vendored
+56
-31
@@ -6810,7 +6810,12 @@ define('html_utils',[], function() {
|
|||||||
define('config',[],function(){
|
define('config',[],function(){
|
||||||
var config = {
|
var config = {
|
||||||
inputStream: { name: "Live",
|
inputStream: { name: "Live",
|
||||||
type: "LiveStream"
|
type: "LiveStream",
|
||||||
|
constraints: {
|
||||||
|
width: 640,
|
||||||
|
height: 480,
|
||||||
|
facing: "environment" // or user
|
||||||
|
}
|
||||||
},
|
},
|
||||||
tracking: false,
|
tracking: false,
|
||||||
debug: false,
|
debug: false,
|
||||||
@@ -6943,7 +6948,7 @@ define('events',[],function() {
|
|||||||
/* jshint undef: true, unused: true, browser:true, devel: true */
|
/* jshint undef: true, unused: true, browser:true, devel: true */
|
||||||
/* global define, MediaStreamTrack */
|
/* global define, MediaStreamTrack */
|
||||||
|
|
||||||
define('camera_access',[],function() {
|
define('camera_access',["html_utils"], function(HtmlUtils) {
|
||||||
|
|
||||||
var streamRef;
|
var streamRef;
|
||||||
|
|
||||||
@@ -6996,43 +7001,63 @@ define('camera_access',[],function() {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function normalizeConstraints(config, cb) {
|
||||||
|
var constraints = {
|
||||||
|
audio: false,
|
||||||
|
video: true
|
||||||
|
},
|
||||||
|
videoConstraints = HtmlUtils.mergeObjects({
|
||||||
|
width: 640,
|
||||||
|
height: 480,
|
||||||
|
facing: "environment"
|
||||||
|
}, config);
|
||||||
|
|
||||||
|
if ( typeof MediaStreamTrack.getSources !== 'undefined') {
|
||||||
|
MediaStreamTrack.getSources(function(sourceInfos) {
|
||||||
|
var videoSourceId;
|
||||||
|
for (var i = 0; i != sourceInfos.length; ++i) {
|
||||||
|
var sourceInfo = sourceInfos[i];
|
||||||
|
if (sourceInfo.kind == "video" && sourceInfo.facing == videoConstraints.facing) {
|
||||||
|
videoSourceId = sourceInfo.id;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
constraints.video = {
|
||||||
|
mandatory: {
|
||||||
|
minWidth: videoConstraints.width,
|
||||||
|
minHeight: videoConstraints.height
|
||||||
|
},
|
||||||
|
optional: [{
|
||||||
|
sourceId: videoSourceId
|
||||||
|
}]
|
||||||
|
};
|
||||||
|
return cb(constraints);
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
constraints.video = {
|
||||||
|
mediaSource: "camera",
|
||||||
|
width: { min: videoConstraints.width, max: videoConstraints.width },
|
||||||
|
height: { min: videoConstraints.height, max: videoConstraints.height },
|
||||||
|
require: ["width", "height"]
|
||||||
|
};
|
||||||
|
return cb(constraints);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Requests the back-facing camera of the user. The callback is called
|
* Requests the back-facing camera of the user. The callback is called
|
||||||
* whenever the stream is ready to be consumed, or if an error occures.
|
* whenever the stream is ready to be consumed, or if an error occures.
|
||||||
* @param {Object} video
|
* @param {Object} video
|
||||||
* @param {Object} callback
|
* @param {Object} callback
|
||||||
*/
|
*/
|
||||||
function request(video, callback) {
|
function request(video, videoConstraints, callback) {
|
||||||
if ( typeof MediaStreamTrack.getSources !== 'undefined') {
|
normalizeConstraints(videoConstraints, function(constraints) {
|
||||||
MediaStreamTrack.getSources(function(sourceInfos) {
|
initCamera(constraints, video, callback);
|
||||||
var videoSourceId;
|
});
|
||||||
for (var i = 0; i != sourceInfos.length; ++i) {
|
|
||||||
var sourceInfo = sourceInfos[i];
|
|
||||||
if (sourceInfo.kind == "video" && sourceInfo.facing == "environment") {
|
|
||||||
videoSourceId = sourceInfo.id;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
var constraints = {
|
|
||||||
audio : false,
|
|
||||||
video : {
|
|
||||||
optional : [{
|
|
||||||
sourceId : videoSourceId
|
|
||||||
}]
|
|
||||||
}
|
|
||||||
};
|
|
||||||
initCamera(constraints, video, callback);
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
initCamera({
|
|
||||||
video : true,
|
|
||||||
audio : false
|
|
||||||
}, video, callback);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
request : function(video, callback) {
|
request : function(video, constraints, callback) {
|
||||||
request(video, callback);
|
request(video, constraints, callback);
|
||||||
},
|
},
|
||||||
release : function() {
|
release : function() {
|
||||||
var tracks = streamRef && streamRef.getVideoTracks();
|
var tracks = streamRef && streamRef.getVideoTracks();
|
||||||
@@ -8232,7 +8257,7 @@ function(Code128Reader, EANReader, InputStream, ImageWrapper, BarcodeLocator, Ba
|
|||||||
$viewport.appendChild(video);
|
$viewport.appendChild(video);
|
||||||
}
|
}
|
||||||
_inputStream = InputStream.createLiveStream(video);
|
_inputStream = InputStream.createLiveStream(video);
|
||||||
CameraAccess.request(video, function(err) {
|
CameraAccess.request(video, _config.inputStream.constraints, function(err) {
|
||||||
if (!err) {
|
if (!err) {
|
||||||
_inputStream.trigger("canrecord");
|
_inputStream.trigger("canrecord");
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -52,7 +52,7 @@ define(['camera_access'], function(CameraAccess){
|
|||||||
|
|
||||||
describe('request', function() {
|
describe('request', function() {
|
||||||
it('should request the camera', function(done) {
|
it('should request the camera', function(done) {
|
||||||
CameraAccess.request(video, function() {
|
CameraAccess.request(video, {}, function() {
|
||||||
expect(navigator.getUserMedia.calledOnce).to.equal(true);
|
expect(navigator.getUserMedia.calledOnce).to.equal(true);
|
||||||
expect(video.src).to.deep.equal(stream);
|
expect(video.src).to.deep.equal(stream);
|
||||||
done();
|
done();
|
||||||
@@ -62,7 +62,7 @@ define(['camera_access'], function(CameraAccess){
|
|||||||
|
|
||||||
describe('release', function() {
|
describe('release', function() {
|
||||||
it('should release the camera', function(done) {
|
it('should release the camera', function(done) {
|
||||||
CameraAccess.request(video, function() {
|
CameraAccess.request(video, {}, function() {
|
||||||
expect(video.src).to.deep.equal(stream);
|
expect(video.src).to.deep.equal(stream);
|
||||||
CameraAccess.release();
|
CameraAccess.release();
|
||||||
expect(video.src.getVideoTracks()).to.have.length(1);
|
expect(video.src.getVideoTracks()).to.have.length(1);
|
||||||
|
|||||||
+48
-22
@@ -1,7 +1,7 @@
|
|||||||
/* jshint undef: true, unused: true, browser:true, devel: true */
|
/* jshint undef: true, unused: true, browser:true, devel: true */
|
||||||
/* global define, MediaStreamTrack */
|
/* global define, MediaStreamTrack */
|
||||||
|
|
||||||
define(function() {
|
define(["html_utils"], function(HtmlUtils) {
|
||||||
"use strict";
|
"use strict";
|
||||||
var streamRef;
|
var streamRef;
|
||||||
|
|
||||||
@@ -20,7 +20,7 @@ define(function() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tries to attach the camer-stream to a given video-element
|
* Tries to attach the camera-stream to a given video-element
|
||||||
* and calls the callback function when the content is ready
|
* and calls the callback function when the content is ready
|
||||||
* @param {Object} constraints
|
* @param {Object} constraints
|
||||||
* @param {Object} video
|
* @param {Object} video
|
||||||
@@ -55,42 +55,68 @@ define(function() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Requests the back-facing camera of the user. The callback is called
|
* Normalizes the incoming constraints to satisfy the current browser
|
||||||
* whenever the stream is ready to be consumed, or if an error occures.
|
* @param config
|
||||||
* @param {Object} video
|
* @param cb Callback which is called whenever constraints are created
|
||||||
* @param {Object} callback
|
* @returns {*}
|
||||||
*/
|
*/
|
||||||
function request(video, callback) {
|
function normalizeConstraints(config, cb) {
|
||||||
|
var constraints = {
|
||||||
|
audio: false,
|
||||||
|
video: true
|
||||||
|
},
|
||||||
|
videoConstraints = HtmlUtils.mergeObjects({
|
||||||
|
width: 640,
|
||||||
|
height: 480,
|
||||||
|
facing: "environment"
|
||||||
|
}, config);
|
||||||
|
|
||||||
if ( typeof MediaStreamTrack.getSources !== 'undefined') {
|
if ( typeof MediaStreamTrack.getSources !== 'undefined') {
|
||||||
MediaStreamTrack.getSources(function(sourceInfos) {
|
MediaStreamTrack.getSources(function(sourceInfos) {
|
||||||
var videoSourceId;
|
var videoSourceId;
|
||||||
for (var i = 0; i != sourceInfos.length; ++i) {
|
for (var i = 0; i != sourceInfos.length; ++i) {
|
||||||
var sourceInfo = sourceInfos[i];
|
var sourceInfo = sourceInfos[i];
|
||||||
if (sourceInfo.kind == "video" && sourceInfo.facing == "environment") {
|
if (sourceInfo.kind == "video" && sourceInfo.facing == videoConstraints.facing) {
|
||||||
videoSourceId = sourceInfo.id;
|
videoSourceId = sourceInfo.id;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
var constraints = {
|
constraints.video = {
|
||||||
audio : false,
|
mandatory: {
|
||||||
video : {
|
minWidth: videoConstraints.width,
|
||||||
optional : [{
|
minHeight: videoConstraints.height
|
||||||
sourceId : videoSourceId
|
},
|
||||||
}]
|
optional: [{
|
||||||
}
|
sourceId: videoSourceId
|
||||||
|
}]
|
||||||
};
|
};
|
||||||
initCamera(constraints, video, callback);
|
return cb(constraints);
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
initCamera({
|
constraints.video = {
|
||||||
video : true,
|
mediaSource: "camera",
|
||||||
audio : false
|
width: { min: videoConstraints.width, max: videoConstraints.width },
|
||||||
}, video, callback);
|
height: { min: videoConstraints.height, max: videoConstraints.height },
|
||||||
|
require: ["width", "height"]
|
||||||
|
};
|
||||||
|
return cb(constraints);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Requests the back-facing camera of the user. The callback is called
|
||||||
|
* whenever the stream is ready to be consumed, or if an error occures.
|
||||||
|
* @param {Object} video
|
||||||
|
* @param {Object} callback
|
||||||
|
*/
|
||||||
|
function request(video, videoConstraints, callback) {
|
||||||
|
normalizeConstraints(videoConstraints, function(constraints) {
|
||||||
|
initCamera(constraints, video, callback);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
request : function(video, callback) {
|
request : function(video, constraints, callback) {
|
||||||
request(video, callback);
|
request(video, constraints, callback);
|
||||||
},
|
},
|
||||||
release : function() {
|
release : function() {
|
||||||
var tracks = streamRef && streamRef.getVideoTracks();
|
var tracks = streamRef && streamRef.getVideoTracks();
|
||||||
|
|||||||
+6
-1
@@ -5,7 +5,12 @@
|
|||||||
define(function(){
|
define(function(){
|
||||||
var config = {
|
var config = {
|
||||||
inputStream: { name: "Live",
|
inputStream: { name: "Live",
|
||||||
type: "LiveStream"
|
type: "LiveStream",
|
||||||
|
constraints: {
|
||||||
|
width: 640,
|
||||||
|
height: 480,
|
||||||
|
facing: "environment" // or user
|
||||||
|
}
|
||||||
},
|
},
|
||||||
tracking: false,
|
tracking: false,
|
||||||
debug: false,
|
debug: false,
|
||||||
|
|||||||
+1
-1
@@ -63,7 +63,7 @@ function(Code128Reader, EANReader, InputStream, ImageWrapper, BarcodeLocator, Ba
|
|||||||
$viewport.appendChild(video);
|
$viewport.appendChild(video);
|
||||||
}
|
}
|
||||||
_inputStream = InputStream.createLiveStream(video);
|
_inputStream = InputStream.createLiveStream(video);
|
||||||
CameraAccess.request(video, function(err) {
|
CameraAccess.request(video, _config.inputStream.constraints, function(err) {
|
||||||
if (!err) {
|
if (!err) {
|
||||||
_inputStream.trigger("canrecord");
|
_inputStream.trigger("canrecord");
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
+2
-1
@@ -21,7 +21,8 @@ require.config({
|
|||||||
'glMatrixAddon': 'src/glMatrixAddon',
|
'glMatrixAddon': 'src/glMatrixAddon',
|
||||||
'cluster': 'src/cluster',
|
'cluster': 'src/cluster',
|
||||||
'camera_access': 'src/camera_access',
|
'camera_access': 'src/camera_access',
|
||||||
'events': 'src/events'
|
'events': 'src/events',
|
||||||
|
'html_utils': 'src/html_utils'
|
||||||
},
|
},
|
||||||
deps: allTestFiles,
|
deps: allTestFiles,
|
||||||
callback: window.__karma__.start
|
callback: window.__karma__.start
|
||||||
|
|||||||
Reference in New Issue
Block a user