The barcode is used mostly in all products, to ease the work of billing. If you have the barcode, there is no need remember the price, the product name, and specification. The barcode has a unique number which is linked to the database containing corresponding product information. This is not only applied to products but also to services.

Start Ionic project using.


ionic start barcode blank

Then go to barcode folder.


cd barcode

Then add cordovaBarcodeScanner plugin.


cordova plugin add https://github.com/phonegap/phonegap-plugin-barcodescanner.git

(To execute the above command use only Git Bash).

Download ngCordova using

https://github.com/driftyco/ng-cordova/archive/master.zip

Include ngCordova js file in index.html.


<script type="text/javascript" src="js/ng-cordova.min.js" ></script>

Add controller.


<body ng-app="starter" ng-controller="barctrl" >

Add ngCordova to app.js.


angular.module('starter', ['ionic','ngCordova'])

Create a button inside <ion-content> and when the button is clicked write an action to scan a barcode in app.js.

Add a button inside <ion-content> tag.


<button class="button button-positive" ng-click="scan()" >Scan</button>

Create scan() function inside your controller.


controller('barctrl',function($scope,$ionicPlatform,$cordovaBarcodeScanner){


$scope.scan=function()

{

 document.addEventListener("deviceready", function () {


    $cordovaBarcodeScanner

     .scan()

     .then(function(barcodeData) {

       // Success! Barcode data is here

       alert(JSON.stringify(barcodeData));

     }, function(error) {

       // An error occurred

     });



    // NOTE: encoding not functioning yet

    $cordovaBarcodeScanner

     .encode(BarcodeScanner.Encode.TEXT_TYPE, "http://www.ampersandacademy.com")

     .then(function(success) {

       // Success!

       alert(JSON.stringify(success));

     }, function(error) {

       // An error occurred

     });


 }, false);

}


})

That’s all. To generate apk.


ionic platform add android

ionic build android

Tested Device

Samsung J7 Android Lolipop

Source

APK