mirror of
https://github.com/serratus/quaggaJS.git
synced 2026-08-12 21:21:42 +08:00
#47: Passing camera related errors to the init callback
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
quaggaJS
|
quaggaJS
|
||||||
========
|
========
|
||||||
|
|
||||||
- [Changelog](#changelog) (2015-06-21)
|
- [Changelog](#changelog) (2015-07-06)
|
||||||
|
|
||||||
## What is QuaggaJS?
|
## What is QuaggaJS?
|
||||||
|
|
||||||
@@ -80,12 +80,16 @@ version `quagga.min.js` and places both files in the `dist` folder.
|
|||||||
You can check out the [examples][github_examples] to get an idea of how to
|
You can check out the [examples][github_examples] to get an idea of how to
|
||||||
use QuaggaJS. Basically the library exposes the following API:
|
use QuaggaJS. Basically the library exposes the following API:
|
||||||
|
|
||||||
### Quagga.init(config, callback)
|
### <a name="quaggainit">Quagga.init(config, callback)</a>
|
||||||
|
|
||||||
This method initializes the library for a given configuration `config` (see
|
This method initializes the library for a given configuration `config` (see
|
||||||
below) and invokes the `callback` when Quagga is ready to start. The
|
below) and invokes the `callback(err)` when Quagga has finished its
|
||||||
initialization process also requests for camera access if real-time detection is
|
bootstrapping phase. The initialization process also requests for camera
|
||||||
configured.
|
access if real-time detection is configured. In case of an error, the `err`
|
||||||
|
parameter is set and contains information about the cause. A potential cause
|
||||||
|
may be the `inputStream.type` is set to `LiveStream`, but the browser does
|
||||||
|
not support this API, or simply if the user denies the permission to use the
|
||||||
|
camera.
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
Quagga.init({
|
Quagga.init({
|
||||||
@@ -96,7 +100,11 @@ Quagga.init({
|
|||||||
decoder : {
|
decoder : {
|
||||||
readers : ["code_128_reader"]
|
readers : ["code_128_reader"]
|
||||||
}
|
}
|
||||||
}, function() {
|
}, function(err) {
|
||||||
|
if (err) {
|
||||||
|
console.log(err);
|
||||||
|
return
|
||||||
|
}
|
||||||
console.log("Initialization finished. Ready to start");
|
console.log("Initialization finished. Ready to start");
|
||||||
Quagga.start();
|
Quagga.start();
|
||||||
});
|
});
|
||||||
@@ -357,6 +365,11 @@ on the ``singleChannel`` flag in the configuration when using ``decodeSingle``.
|
|||||||
|
|
||||||
## <a name="changelog">Changelog</a>
|
## <a name="changelog">Changelog</a>
|
||||||
|
|
||||||
|
### 2015-07-06
|
||||||
|
- Improvements
|
||||||
|
- Added `err` parameter to [Quagga.init()](#quaggainit) callback
|
||||||
|
function
|
||||||
|
|
||||||
### 2015-06-21
|
### 2015-06-21
|
||||||
- Features
|
- Features
|
||||||
- Added ``singleChannel`` configuration to ``inputStream`` (in [config]
|
- Added ``singleChannel`` configuration to ``inputStream`` (in [config]
|
||||||
|
|||||||
Vendored
+12
-8
@@ -8187,11 +8187,15 @@ define('camera_access',["html_utils"], function(HtmlUtils) {
|
|||||||
* @param {Object} failure Callback
|
* @param {Object} failure Callback
|
||||||
*/
|
*/
|
||||||
function getUserMedia(constraints, success, failure) {
|
function getUserMedia(constraints, success, failure) {
|
||||||
navigator.getUserMedia(constraints, function(stream) {
|
if (typeof navigator.getUserMedia !== 'undefined') {
|
||||||
streamRef = stream;
|
navigator.getUserMedia(constraints, function (stream) {
|
||||||
var videoSrc = (window.URL && window.URL.createObjectURL(stream)) || stream;
|
streamRef = stream;
|
||||||
success.apply(null, [videoSrc]);
|
var videoSrc = (window.URL && window.URL.createObjectURL(stream)) || stream;
|
||||||
}, failure);
|
success.apply(null, [videoSrc]);
|
||||||
|
}, failure);
|
||||||
|
} else {
|
||||||
|
failure(new TypeError("getUserMedia not available"));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function loadedData(video, callback) {
|
function loadedData(video, callback) {
|
||||||
@@ -8230,7 +8234,7 @@ define('camera_access',["html_utils"], function(HtmlUtils) {
|
|||||||
video.addEventListener('loadeddata', loadedDataHandler, false);
|
video.addEventListener('loadeddata', loadedDataHandler, false);
|
||||||
video.play();
|
video.play();
|
||||||
}, function(e) {
|
}, function(e) {
|
||||||
console.log(e);
|
callback(e);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -8253,7 +8257,7 @@ define('camera_access',["html_utils"], function(HtmlUtils) {
|
|||||||
facing: "environment"
|
facing: "environment"
|
||||||
}, config);
|
}, config);
|
||||||
|
|
||||||
if ( typeof MediaStreamTrack.getSources !== 'undefined') {
|
if ( typeof MediaStreamTrack !== 'undefined' && 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) {
|
||||||
@@ -8473,7 +8477,7 @@ function(Code128Reader,
|
|||||||
if (!err) {
|
if (!err) {
|
||||||
_inputStream.trigger("canrecord");
|
_inputStream.trigger("canrecord");
|
||||||
} else {
|
} else {
|
||||||
console.log(err);
|
return cb(err);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
Vendored
+2
-2
File diff suppressed because one or more lines are too long
@@ -11,12 +11,20 @@ $(function() {
|
|||||||
});
|
});
|
||||||
var App = {
|
var App = {
|
||||||
init : function() {
|
init : function() {
|
||||||
Quagga.init(this.state, function() {
|
var self = this;
|
||||||
|
|
||||||
|
Quagga.init(this.state, function(err) {
|
||||||
|
if (err) {
|
||||||
|
return self.handleError(err);
|
||||||
|
}
|
||||||
Quagga.registerResultCollector(resultCollector);
|
Quagga.registerResultCollector(resultCollector);
|
||||||
App.attachListeners();
|
App.attachListeners();
|
||||||
Quagga.start();
|
Quagga.start();
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
handleError: function(err) {
|
||||||
|
console.log(err);
|
||||||
|
},
|
||||||
attachListeners: function() {
|
attachListeners: function() {
|
||||||
var self = this;
|
var self = this;
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "quagga",
|
"name": "quagga",
|
||||||
"version": "0.6.11",
|
"version": "0.6.12",
|
||||||
"description": "An advanced barcode-scanner written in JavaScript",
|
"description": "An advanced barcode-scanner written in JavaScript",
|
||||||
"main": "dist/quagga.js",
|
"main": "dist/quagga.js",
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
|||||||
+67
-21
@@ -1,6 +1,5 @@
|
|||||||
define(['camera_access'], function(CameraAccess){
|
define(['camera_access'], function(CameraAccess){
|
||||||
var originalURL,
|
var originalURL,
|
||||||
originalUserMedia,
|
|
||||||
originalMediaStreamTrack,
|
originalMediaStreamTrack,
|
||||||
video,
|
video,
|
||||||
stream;
|
stream;
|
||||||
@@ -11,7 +10,6 @@ define(['camera_access'], function(CameraAccess){
|
|||||||
}];
|
}];
|
||||||
|
|
||||||
originalURL = window.URL;
|
originalURL = window.URL;
|
||||||
originalUserMedia = window.getUserMedia;
|
|
||||||
originalMediaStreamTrack = window.MediaStreamTrack;
|
originalMediaStreamTrack = window.MediaStreamTrack;
|
||||||
window.MediaStreamTrack = {};
|
window.MediaStreamTrack = {};
|
||||||
window.URL = null;
|
window.URL = null;
|
||||||
@@ -23,10 +21,7 @@ define(['camera_access'], function(CameraAccess){
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
sinon.spy(tracks[0], "stop");
|
sinon.spy(tracks[0], "stop");
|
||||||
navigator.getUserMedia = function(constraints, cb) {
|
|
||||||
cb(stream);
|
|
||||||
};
|
|
||||||
sinon.spy(navigator, "getUserMedia");
|
|
||||||
video = {
|
video = {
|
||||||
src: null,
|
src: null,
|
||||||
addEventListener: function() {
|
addEventListener: function() {
|
||||||
@@ -48,29 +43,80 @@ define(['camera_access'], function(CameraAccess){
|
|||||||
});
|
});
|
||||||
|
|
||||||
afterEach(function() {
|
afterEach(function() {
|
||||||
navigator.getUserMedia = originalUserMedia;
|
|
||||||
window.URL = originalURL;
|
window.URL = originalURL;
|
||||||
window.MediaStreamTrack = originalMediaStreamTrack;
|
window.MediaStreamTrack = originalMediaStreamTrack;
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('request', function() {
|
describe('success', function() {
|
||||||
it('should request the camera', function(done) {
|
beforeEach(function() {
|
||||||
CameraAccess.request(video, {}, function() {
|
sinon.stub(navigator, "getUserMedia", function(constraints, success) {
|
||||||
expect(navigator.getUserMedia.calledOnce).to.equal(true);
|
success(stream);
|
||||||
expect(video.src).to.deep.equal(stream);
|
});
|
||||||
done();
|
});
|
||||||
|
|
||||||
|
afterEach(function() {
|
||||||
|
navigator.getUserMedia.restore();
|
||||||
|
});
|
||||||
|
describe('request', function () {
|
||||||
|
it('should request the camera', function (done) {
|
||||||
|
CameraAccess.request(video, {}, function () {
|
||||||
|
expect(navigator.getUserMedia.calledOnce).to.equal(true);
|
||||||
|
expect(video.src).to.deep.equal(stream);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('release', function () {
|
||||||
|
it('should release the camera', function (done) {
|
||||||
|
CameraAccess.request(video, {}, function () {
|
||||||
|
expect(video.src).to.deep.equal(stream);
|
||||||
|
CameraAccess.release();
|
||||||
|
expect(video.src.getVideoTracks()).to.have.length(1);
|
||||||
|
expect(video.src.getVideoTracks()[0].stop.calledOnce).to.equal(true);
|
||||||
|
done();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('release', function() {
|
describe('failure', function() {
|
||||||
it('should release the camera', function(done) {
|
describe("permission denied", function(){
|
||||||
CameraAccess.request(video, {}, function() {
|
before(function() {
|
||||||
expect(video.src).to.deep.equal(stream);
|
sinon.stub(navigator, "getUserMedia", function(constraints, success, failure) {
|
||||||
CameraAccess.release();
|
failure(new Error());
|
||||||
expect(video.src.getVideoTracks()).to.have.length(1);
|
});
|
||||||
expect(video.src.getVideoTracks()[0].stop.calledOnce).to.equal(true);
|
});
|
||||||
done();
|
|
||||||
|
after(function() {
|
||||||
|
navigator.getUserMedia.restore();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should throw if getUserMedia not available', function(done) {
|
||||||
|
CameraAccess.request(video, {}, function(err) {
|
||||||
|
expect(err).to.be.defined;
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("not available", function(){
|
||||||
|
var originalGetUserMedia;
|
||||||
|
|
||||||
|
before(function() {
|
||||||
|
originalGetUserMedia = navigator.getUserMedia;
|
||||||
|
navigator.getUserMedia = undefined;
|
||||||
|
});
|
||||||
|
|
||||||
|
after(function() {
|
||||||
|
navigator.getUserMedia = originalGetUserMedia;
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should throw if getUserMedia not available', function(done) {
|
||||||
|
CameraAccess.request(video, {}, function(err) {
|
||||||
|
expect(err).to.be.defined;
|
||||||
|
done();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
+11
-7
@@ -13,11 +13,15 @@ define(["html_utils"], function(HtmlUtils) {
|
|||||||
* @param {Object} failure Callback
|
* @param {Object} failure Callback
|
||||||
*/
|
*/
|
||||||
function getUserMedia(constraints, success, failure) {
|
function getUserMedia(constraints, success, failure) {
|
||||||
navigator.getUserMedia(constraints, function(stream) {
|
if (typeof navigator.getUserMedia !== 'undefined') {
|
||||||
streamRef = stream;
|
navigator.getUserMedia(constraints, function (stream) {
|
||||||
var videoSrc = (window.URL && window.URL.createObjectURL(stream)) || stream;
|
streamRef = stream;
|
||||||
success.apply(null, [videoSrc]);
|
var videoSrc = (window.URL && window.URL.createObjectURL(stream)) || stream;
|
||||||
}, failure);
|
success.apply(null, [videoSrc]);
|
||||||
|
}, failure);
|
||||||
|
} else {
|
||||||
|
failure(new TypeError("getUserMedia not available"));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function loadedData(video, callback) {
|
function loadedData(video, callback) {
|
||||||
@@ -56,7 +60,7 @@ define(["html_utils"], function(HtmlUtils) {
|
|||||||
video.addEventListener('loadeddata', loadedDataHandler, false);
|
video.addEventListener('loadeddata', loadedDataHandler, false);
|
||||||
video.play();
|
video.play();
|
||||||
}, function(e) {
|
}, function(e) {
|
||||||
console.log(e);
|
callback(e);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -79,7 +83,7 @@ define(["html_utils"], function(HtmlUtils) {
|
|||||||
facing: "environment"
|
facing: "environment"
|
||||||
}, config);
|
}, config);
|
||||||
|
|
||||||
if ( typeof MediaStreamTrack.getSources !== 'undefined') {
|
if ( typeof MediaStreamTrack !== 'undefined' && 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) {
|
||||||
|
|||||||
+1
-1
@@ -99,7 +99,7 @@ function(Code128Reader,
|
|||||||
if (!err) {
|
if (!err) {
|
||||||
_inputStream.trigger("canrecord");
|
_inputStream.trigger("canrecord");
|
||||||
} else {
|
} else {
|
||||||
console.log(err);
|
return cb(err);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user