mirror of
https://github.com/serratus/quaggaJS.git
synced 2026-08-12 21:21:42 +08:00
Cleaning up after stopping quagga
This commit is contained in:
+26
-20
@@ -3,7 +3,8 @@
|
||||
|
||||
define(["html_utils"], function(HtmlUtils) {
|
||||
"use strict";
|
||||
var streamRef;
|
||||
var streamRef,
|
||||
loadedDataHandler;
|
||||
|
||||
/**
|
||||
* Wraps browser-specific getUserMedia
|
||||
@@ -19,6 +20,25 @@ define(["html_utils"], function(HtmlUtils) {
|
||||
}, failure);
|
||||
}
|
||||
|
||||
function loadedData(video, callback) {
|
||||
var attempts = 10;
|
||||
|
||||
function checkVideo() {
|
||||
if (attempts > 0) {
|
||||
if (video.videoWidth > 0 && video.videoHeight > 0) {
|
||||
console.log(video.videoWidth + "px x " + video.videoHeight + "px");
|
||||
callback();
|
||||
} else {
|
||||
window.setTimeout(checkVideo, 500);
|
||||
}
|
||||
} else {
|
||||
callback('Unable to play video stream. Is webcam working?');
|
||||
}
|
||||
attempts--;
|
||||
}
|
||||
checkVideo();
|
||||
}
|
||||
|
||||
/**
|
||||
* Tries to attach the camera-stream to a given video-element
|
||||
* and calls the callback function when the content is ready
|
||||
@@ -29,25 +49,11 @@ define(["html_utils"], function(HtmlUtils) {
|
||||
function initCamera(constraints, video, callback) {
|
||||
getUserMedia(constraints, function(src) {
|
||||
video.src = src;
|
||||
video.addEventListener('loadeddata', function() {
|
||||
var attempts = 10;
|
||||
|
||||
function checkVideo() {
|
||||
if (attempts > 0) {
|
||||
if (video.videoWidth > 0 && video.videoHeight > 0) {
|
||||
console.log(video.videoWidth + "px x " + video.videoHeight + "px");
|
||||
callback();
|
||||
} else {
|
||||
window.setTimeout(checkVideo, 500);
|
||||
}
|
||||
} else {
|
||||
callback('Unable to play video stream. Is webcam working?');
|
||||
}
|
||||
attempts--;
|
||||
}
|
||||
|
||||
checkVideo();
|
||||
}, false);
|
||||
if (loadedDataHandler) {
|
||||
video.removeEventListener("loadeddata", loadedDataHandler, false);
|
||||
}
|
||||
loadedDataHandler = loadedData.bind(null, video, callback);
|
||||
video.addEventListener('loadeddata', loadedDataHandler, false);
|
||||
video.play();
|
||||
}, function(e) {
|
||||
console.log(e);
|
||||
|
||||
+2
-1
@@ -196,6 +196,7 @@ define(
|
||||
return endInfo;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
};
|
||||
|
||||
EANReader.prototype._findEnd = function(offset, isWhite) {
|
||||
@@ -261,7 +262,7 @@ define(
|
||||
decodedCodes.push(code);
|
||||
code = self._decodePayload(code, result, decodedCodes);
|
||||
code = self._findEnd(code.end, false);
|
||||
if (code === null){
|
||||
if (!code){
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
@@ -68,6 +68,17 @@ define(["image_loader"], function(ImageLoader) {
|
||||
}
|
||||
};
|
||||
|
||||
that.clearEventHandlers = function() {
|
||||
_eventNames.forEach(function(eventName) {
|
||||
var handlers = _eventHandlers[eventName];
|
||||
if (handlers && handlers.length > 0) {
|
||||
handlers.forEach(function(handler) {
|
||||
video.removeEventListener(eventName, handler);
|
||||
});
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
that.trigger = function(eventName, args) {
|
||||
var j,
|
||||
handlers = _eventHandlers[eventName];
|
||||
|
||||
@@ -407,8 +407,14 @@ function(Code128Reader,
|
||||
},
|
||||
stop : function() {
|
||||
_stopped = true;
|
||||
_workerPool.forEach(function(workerThread) {
|
||||
workerThread.worker.terminate();
|
||||
console.log("Worker terminated!");
|
||||
});
|
||||
_workerPool.length = 0;
|
||||
if (_config.inputStream.type === "LiveStream") {
|
||||
CameraAccess.release();
|
||||
_inputStream.clearEventHandlers();
|
||||
}
|
||||
},
|
||||
pause: function() {
|
||||
|
||||
Reference in New Issue
Block a user