I implemented the $cordovaBarcodeScannerSource to an ionic application like this:
.controller('FoobarController', function($scope, $cordovaBarcodeScanner,
$http, $ionicPlatform) {
$scope.scanQR = function() {
$ionicPlatform.ready(function() {
$cordovaBarcodeScanner.scan().then(function(imageData) {
alert(imageData.text);
}, function(error) {
console.log("An error happened -> " + error);
});
});
};
And just call it with this button: <button ng-click="scanQR()">scan</button>
.
All works, except that the plugin causes freezes sometimes when opening the camera app.
When I click the button, it opens the camera and the image from the camera is frozen. You have to cancel it, re-open it to hope it'll work. And what always works is to switch to front-camera and go back to the back-camera. It only occurs sometimes on an iPhone, so it's hard to debug. Is there maybe something extra I need to add in the code?
Maybe your button (in some magic reasons) pressed twice. So try the following:
.controller('FoobarController', function($scope, $cordovaBarcodeScanner, $http) {
$scope.currentlyScanning = false;
$scope.scanQR = function() {
if(!$scope.currentlyScanning) {
$scope.currentlyScanning = true;
$cordovaBarcodeScanner.scan().then(function(imageData) {
$scope.currentlyScanning = false;
alert(imageData.text);
}, function(error) {
$scope.currentlyScanning = false;
console.log("An error happened -> " + error);
});
}
};