使用Ionic / ngcordova的条形码扫描仪,无法获取数据

时间:2015-08-11 10:54:21

标签: angularjs ionic-framework barcode ngcordova

我有一个控制器:

.controller('BarCodeScanCtrl', function($scope, $cordovaBarcodeScanner) {

    $scope.scanBarcode = function() {
        $cordovaBarcodeScanner.scan().then(function(barcodeData) {
            // Success! Barcode data is here
            console.log(barcodeData.text);
            alert('barcode scanned:' + barcodeData);

        }, function(error) {
            alert('Error');
            console.log(error);
            // An error occurred
        });

    };
});

我正在使用QR代码生成器:http://www.qr-code-generator.com/但我似乎无法检索我输入的任何数据。它会为barCode对象返回text&的值。 format属性为空。 cancel属性为true

{
  "cancelled": true,
  "text":"",
  "format": ""
}

有什么想法吗?

3 个答案:

答案 0 :(得分:0)

已在github上传了一份工作样本,请查看。

步骤:

1)创建项目
 2)cd进入项目
3)添加所需的平台
    4)将ngcordova-min.js文件添加到js文件夹
5)添加ngcordova     在cordova路径之前的index.html中的js文件路径
6)添加ngcordova     依赖于app.js
7)添加插件
8)在html页面中添加功能     
9)在控制器中添加逻辑
)10)测试应用程序

答案 1 :(得分:0)

我猜你正在使用Ionic。你应该使用离子非ng-click的触摸指令,因为当你使用ng-click时它会触发扫描视图两次并导致强制退出(CMIIW)。

在你的观点中使用它:

<button on-touch="scanBarcode()">Scan</button>

希望它有效!

答案 2 :(得分:0)

如果cancel属性为true,则手动停止扫描功能。

在您的控制器中注入$ionicPlatform

.controller('BarCodeScanCtrl', function($scope, $cordovaBarcodeScanner, $ionicPlatform) {
  $scope.scanQR = function () {
    $ionicPlatform.ready(function() {
      $cordovaBarcodeScanner.scan()
      .then(function(barcodeData) {
        alert('result = '+barcodeData.text);
        alert('type = '+barcodeData.format);
        alert('cancelled = '+barcodeData.cancelled);
      }, function(error) {
        alert(error);
      });
    });
  }
});