bitcoin-atm

bitcoin atm for pyc inc.

git clone https://9o.is/git/bitcoin-atm.git

angular-qr-scanner.js

(2864B)


      1 angular.module('angular-qr-scanner', [])
      2 .directive('qrScanner', ['$timeout', '$rootScope', function($timeout, $rootScope) {
      3   return {
      4     restrict: 'E',
      5     scope: {
      6       ngSuccess: '&ngSuccess',
      7       ngError: '&ngError',
      8       ngVideoError: '&ngVideoError'
      9     },
     10     link: function(scope, element, attrs) {
     11     
     12       window.URL = window.URL || window.webkitURL || window.mozURL || window.msURL;
     13       navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia;
     14           
     15       var height = attrs.height || 300;
     16       var width = attrs.width || 250;
     17       var localMediaStream;
     18     
     19       var video = document.createElement('video');
     20       video.setAttribute('width', width);
     21       video.setAttribute('height', height);
     22       var canvas = document.createElement('canvas');
     23       canvas.setAttribute('id', 'qr-canvas');
     24       canvas.setAttribute('width', width);
     25       canvas.setAttribute('height', height);
     26       canvas.setAttribute('style', 'display:none;'); 
     27     
     28       angular.element(element).append(video);
     29       angular.element(element).append(canvas);
     30       var context = canvas.getContext('2d');
     31       
     32       var scanned = false;
     33       
     34       var setScanned = function() {
     35   		scanned = true;
     36   		$timeout(function() {
     37   			scanned = false;
     38   		}, 2000);
     39   	  };
     40     
     41       var scan = function() {
     42         if (localMediaStream) {
     43           try {
     44         	  context.drawImage(video, 0, 0, width, height);
     45               qrcode.decode();
     46           } catch(e) {
     47         	if (e.name !== "NS_ERROR_NOT_AVAILABLE") {} else {
     48               scope.ngError({error: e});
     49         	}
     50           }
     51         }
     52         
     53         if($rootScope.scanning) {
     54         	$timeout(scan, 10);
     55         }
     56       };
     57 
     58       var successCallback = function(stream) {
     59         video.src = (window.URL && window.URL.createObjectURL(stream)) || stream;
     60         localMediaStream = stream;
     61         video.play();
     62       };
     63 
     64       // Call the getUserMedia method with our callback functions
     65       if (navigator.getUserMedia) {
     66         navigator.getUserMedia({video: true}, successCallback, function(e) {
     67           scope.ngVideoError({error: e});
     68         });
     69       } else {
     70         scope.ngVideoError({error: 'Native web camera streaming (getUserMedia) not supported in this browser.'});
     71       }
     72 
     73       var scan_sound = new Audio("/sound/scanner-beep.wav");
     74       
     75       qrcode.callback = function(data) {
     76     	  setScanned();
     77     	  window.overlord.send({qrcode: data});
     78   		  scan_sound.play();
     79     	  scope.ngSuccess({data: data});
     80       };
     81       
     82       
     83       $rootScope.scanning = false;
     84       
     85       $rootScope.startScanning = function() {
     86     	  $rootScope.scanning = true;
     87     	  scan();
     88       };
     89       
     90       $rootScope.stopScanning = function() {
     91     	  $rootScope.scanning = false;
     92       };
     93     }
     94   };
     95 }]);