Beginner struggling to integrate barcode scanner


#1

Hi there,

Firstly, apologies if this is a bit of a newbie question. I’m fairly familiar with Angular but have never used Ionic before (though I did use Phone gap about 18 months ago). So My knowledge isn’t the best.

Basically I’m trying to throw together a proof of concept that includes a barcode scanner. I have a button within a ionic modal. This button has

ng-click="scan()"

Then in my controller I have:

$scope.scan = function () {
    
    cordova.plugins.barcodeScanner.scan(
		function (result) {
			var s = "Result: " + result.text + "<br/>" +
			"Format: " + result.format + "<br/>" +
			"Cancelled: " + result.cancelled;
			//resultDiv.innerHTML = s;
			console.log("Success");
		}, 
		function (error) {
			alert("Scanning failed: " + error);
		}
	);
};

All I need at this stage is for the scanner to pop up and me to be able to scan a barcode and write the details to console. Very simple, but I am obviously missing something as I can’t get it to work. I’ve tried injecting cordova into my controllers deps with no success.

What do I need to do to enable/include the plugin and then use it in my controllers?

Thanks in advance


#2

Hey i got this working yesterday

$ionicPlatform.ready(function(){

	var scanner = cordova.plugins.barcodeScanner.scan;
	$scope.scan = function(){
		scanner( function (result) { 

        alert("We got a barcode\n" + 
        "Result: " + result.text + "\n" + 
        "Format: " + result.format + "\n" + 
        "Cancelled: " + result.cancelled);  

      console.log("Scanner result: \n" +
            "text: " + result.text + "\n" +
            "format: " + result.format + "\n" +
            "cancelled: " + result.cancelled + "\n");

               console.log(result);
   
    }, function (error) { 
        console.log("Scanning failed: ", error); 
    } );
}

});

Enjoy coding


#3

Hey @programming_kid , thanks for the reply. It’s still not working though, here my controller code - can you see anything I’m doing wrong?

.controller('BooksCtrl', function($scope, Books, $ionicModal, $ionicPlatform, $window) {
$scope.books = Books.all();

// Create and load the Modal
$ionicModal.fromTemplateUrl('add-book.html', function(modal) {
	$scope.addBookModal = modal;
}, {
	scope: $scope,
	animation: 'slide-in-up'
});

// Called when the form is submitted
$scope.createBook = function(book) {
	$scope.books.push({
		id: $scope.books.length + 1,
		name: book.name,
		isbn: book.isbn
	});
	$scope.addBookModal.hide();
	book.name = "";
	book.isbn = "";
};

// Open our new book modal
$scope.newBook = function() {
	$scope.addBookModal.show();
};

// Close the new book modal
$scope.closeNewBook = function() {
	$scope.addBookModal.hide();
};

// Experimental
$ionicPlatform.ready(function(){

	var scanner = cordova.plugins.barcodeScanner.scan;
	$scope.scan = function(){
		scanner( function (result) { 

        alert("We got a barcode\n" + 
        "Result: " + result.text + "\n" + 
        "Format: " + result.format + "\n" + 
        "Cancelled: " + result.cancelled);  

      	console.log("Scanner result: \n" +
            "text: " + result.text + "\n" +
            "format: " + result.format + "\n" +
            "cancelled: " + result.cancelled + "\n");

               console.log(result);

    	}, 

    	function (error) { 
	        console.log("Scanning failed: ", error); 
	    });
	}
});

})

The plugin was installed via terminal and lives here

plugins/com.phonegap.plugins.barcodescanner

If you can’t see anything wrong could you post your controller code for me to inspect? Thanks in advance


#4

there is nothing in my controller except above mentioned code .

What errors do u get in elipse
Have you included barcodescanner.js in index.html


#5

barcodescanner.js? There isn’t one in the plugin I have? Could that be why?


#6

Which plugin are you using


#7

Managed to solve this - for some reason when I cloned the barcode scanner repo it missed half the files and create a load of duplicate folders… Deleted these and recloned and it works fine. Thanks for your help :smile: