Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#4 - Fix issue with stop on changing state #11

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "angular-qr-scanner",
"version": "0.1.0",
"version": "0.1.1",
"homepage": "https://github.com/sembrestels/angular-qr-scanner",
"authors": [
"Sem <[email protected]>"
Expand Down
28 changes: 15 additions & 13 deletions qr-scanner.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
if (require){
if (!angular) var angular = require('angular');
if (!qrcode) var qrcode = require('jsqrcode');
}

(function() {
'use strict';

Expand All @@ -15,13 +10,13 @@ angular.module('qrScanner', ["ng"]).directive('qrScanner', ['$interval', '$windo
ngVideoError: '&ngVideoError'
},
link: function(scope, element, attrs) {

window.URL = window.URL || window.webkitURL || window.mozURL || window.msURL;
navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia;

var height = attrs.height || 300;
var width = attrs.width || 250;

var video = $window.document.createElement('video');
video.setAttribute('width', width);
video.setAttribute('height', height);
Expand All @@ -30,13 +25,13 @@ angular.module('qrScanner', ["ng"]).directive('qrScanner', ['$interval', '$windo
canvas.setAttribute('id', 'qr-canvas');
canvas.setAttribute('width', width);
canvas.setAttribute('height', height);
canvas.setAttribute('style', 'display:none;');
canvas.setAttribute('style', 'display:none;');

angular.element(element).append(video);
angular.element(element).append(canvas);
var context = canvas.getContext('2d');
var context = canvas.getContext('2d');
var stopScan;

var scan = function() {
if ($window.localMediaStream) {
context.drawImage(video, 0, 0, 307,250);
Expand Down Expand Up @@ -72,7 +67,14 @@ angular.module('qrScanner', ["ng"]).directive('qrScanner', ['$interval', '$windo

element.bind('$destroy', function() {
if ($window.localMediaStream) {
$window.localMediaStream.stop();
if ($window.localMediaStream.stop) {
$window.localMediaStream.stop();
} else if ($window.localMediaStream.getVideoTracks) {
var videoTracks = $window.localMediaStream.getVideoTracks();
if (videoTracks && videoTracks.length > 0) {
videoTracks[0].stop();
}
}
}
if (stopScan) {
$interval.cancel(stopScan);
Expand Down