Minozar
Minozar

Reputation: 11

use barcodeScanner in cordova (with angularjs)

(I'm fr) I'm trying to use barcodeScanner in my cordova (works with angularjs) app but it seems my code can't works. I'd like to understand what should I do. :c

First I do :

cordova plugin rm https://github.com/wildabeast/Barcode
cordova plugin add https://github.com/wildabeast/Barcode

Then, here is my code : Index.html

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta name="format-detection" content="telephone=no">
    <meta name="msapplication-tap-highlight" content="no">
    <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width">
    <link rel="stylesheet" type="text/css" href="css/style.css">
    <title>Hello World</title>
</head>
<body ng-app="app" ng-controller="BarcodeCtrl">
    <header class="header unSelectable">
        <a ng-click="panel=0" ng-hide="panel==0"><img src="img/back1.png" class="backLogo"></a>
        <!--<img src="img/logo.png" class="logoHeader">-->
        <a ng-click="callBarcodeScanner()"><img src="img/barcode.png" class="barcodeScanner"></a>
    </header>

    <section ng-view class="container">
    </section>

    <script type="text/javascript" src="js/jquery-2.1.4.min.js"></script>
    <script type="text/javascript" src="js/angular.min.js"></script>
    <script type="text/javascript" src="js/angular-route.min.js"></script>
    <script type="text/javascript" src="js/app.js"></script>
</body>
</html>

app.js

document.addEventListener('deviceready', function(){
    navigator.splashscreen.hide();
}, false);

var app = angular.module('app', ['ngRoute']);

app.config(function($routeProvider){
    $routeProvider
    .when('/home', {templateUrl: 'partials/home.html'})
    .when('/list', {templateUrl: 'partials/list.html'})
    .otherwise({redirectTo: '/home'});
});

app.controller('BarcodeCtrl', function($scope, $http){
    $scope.panel = 0;

    $scope.callBarcodeScanner = function(){
        $('.barCode').val('Scan en cours ...');
        barcodeScanner.scan().then(function(imageData){
            alert(imageData.text);
            console.log("Barcode Format -> " + imageData.format);
            console.log("Cancelled -> " + imageData.cancelled);
        }, function(error){
            console.log("An error happened -> " + error);
        });
    };
});

$('.barCode').focus(function(){
    $('.barCode').val('');
});

If someone can help me...

Upvotes: 0

Views: 1248

Answers (2)

Minozar
Minozar

Reputation: 11

Finally.. To make the plugin operational, I've to include cordova.js (it doesn't matter if the include fail on the web browser) and use ng-Cordova.

Upvotes: 1

bios
bios

Reputation: 299

Your call looks different than the example provided. You're omitting cordova.plugins

cordova.plugins.barcodeScanner.scan(
  function (result) {
      alert("We got a barcode\n" +
            "Result: " + result.text + "\n" +
            "Format: " + result.format + "\n" +
            "Cancelled: " + result.cancelled);
  }, 
  function (error) {
      alert("Scanning failed: " + error);
  }

);

Upvotes: 0

Related Questions