Skip to content
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
5 changes: 3 additions & 2 deletions bower.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
{
"name": "angular-qr-scanner",
"version": "0.1.0",
"version": "0.2.0",
"homepage": "https://github.com/sembrestels/angular-qr-scanner",
"authors": [
"Sem <[email protected]>"
"Sem <[email protected]>",
"Maxaille <[email protected]>"
],
"description": "QR scanner angular directive",
"main": "./qr-scanner.js",
Expand Down
2 changes: 1 addition & 1 deletion package.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.2.0",
"devDependencies": {
"grunt": "~0.4.5",
"grunt-contrib-uglify": "~0.9.1",
Expand Down
174 changes: 102 additions & 72 deletions qr-scanner.js
Original file line number Diff line number Diff line change
@@ -1,84 +1,114 @@
if (require){
if (!angular) var angular = require('angular');
if (!qrcode) var qrcode = require('jsqrcode');
}

(function() {
'use strict';

angular.module('qrScanner', ["ng"]).directive('qrScanner', ['$interval', '$window', function($interval, $window) {
return {
restrict: 'E',
scope: {
ngSuccess: '&ngSuccess',
ngError: '&ngError',
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);
video.setAttribute('style', '-moz-transform:rotateY(-180deg);-webkit-transform:rotateY(-180deg);transform:rotateY(-180deg);');
var canvas = $window.document.createElement('canvas');
canvas.setAttribute('id', 'qr-canvas');
canvas.setAttribute('width', width);
canvas.setAttribute('height', height);
canvas.setAttribute('style', 'display:none;');

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

var scan = function() {
if ($window.localMediaStream) {
context.drawImage(video, 0, 0, 307,250);
try {
qrcode.decode();
} catch(e) {
scope.ngError({error: e});
angular.module('qrScanner', [])
.directive('qrScanner', ['$interval', '$window', function($interval, $window) {
return {
restrict: 'E',
scope: {
ngSuccess: '&ngSuccess',
ngError: '&ngError',
ngVideoError: '&ngVideoError',
ngVideoSource: '=ngVideoSource'
},
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('autoplay', true);
video.setAttribute('width', width);
video.setAttribute('height', height);
var canvas = $window.document.createElement('canvas');
canvas.setAttribute('id', 'qr-canvas');
canvas.setAttribute('style', 'display:none;');

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

var scan = function() {
if ($window.localMediaStream) {
canvas.setAttribute('width', angular.element(video)[0].videoWidth);
canvas.setAttribute('height', angular.element(video)[0].videoHeight);
context.drawImage(video, 0, 0, angular.element(video)[0].videoWidth, angular.element(video)[0].videoHeight);
try {
qrcode.decode();
} catch(e) {
scope.ngError({error: e});
}
}
}
}
};

var successCallback = function(stream) {
video.src = (window.URL && window.URL.createObjectURL(stream)) || stream;
$window.localMediaStream = stream;
var successCallback = function(stream) {
video.src = (window.URL && window.URL.createObjectURL(stream)) || stream;
$window.localMediaStream = stream;

scope.video = video;
video.play();
stopScan = $interval(scan, 500);
}
scope.video = video;
video.play();
stopScan = $interval(scan, 500);
};

// Call the getUserMedia method with our callback functions
if (navigator.getUserMedia) {
navigator.getUserMedia({video: true}, successCallback, function(e) {
scope.ngVideoError({error: e});
});
} else {
scope.ngVideoError({error: 'Native web camera streaming (getUserMedia) not supported in this browser.'});
}
var startVideo = function(source) {
var option;
if (!source) option = true;
else option = {
optional: [{
sourceId: source
}]
};

qrcode.callback = function(data) {
scope.ngSuccess({data: data});
};
// Call the getUserMedia method with our callback functions
if (navigator.getUserMedia) {
navigator.getUserMedia({video: option}, successCallback, function(e) {
scope.ngVideoError({error: e});
});
} else {
scope.ngVideoError({error: 'Native web camera streaming (getUserMedia) not supported in this browser.'});
}

element.bind('$destroy', function() {
if ($window.localMediaStream) {
$window.localMediaStream.stop();
}
if (stopScan) {
$interval.cancel(stopScan);
qrcode.callback = function(data) {
scope.ngSuccess({data: data});
};
};
if(typeof scope.ngVideoSource == 'undefined') {
startVideo(null);
}
});

scope.$watch('ngVideoSource', function(newValue, oldValue) {
if (typeof MediaStreamTrack === 'undefined' ||
typeof MediaStreamTrack.getSources === 'undefined') {
console.log('This browser does not support MediaStreamTrack')
} else {
MediaStreamTrack.getSources(function(sources) {
var videoSources = [];
for (var i = 0, len = sources.length; i<len; i++)
if (sources[i].id == newValue) {
video.pause();
$interval.cancel(stopScan);
startVideo(newValue);
break;
}
});
}
});

element.bind('$destroy', function() {
if ($window.localMediaStream) {
$window.localMediaStream.stop();
}
if (stopScan) {
$interval.cancel(stopScan);
}
});
}
}

}
}]);
})();
]);
})();