Phonegap条形码扫描仪:无法'我不能让它跑

Phonegap barcode scanner: couldn't make it run

本文关键字:不能 扫描仪 条形码 无法 Phonegap      更新时间:2023-09-26

我试图通过插件的基本安装创建一个默认的Phonegap条形码扫描仪,但效果不佳。我不知道发生了什么。这是我的代码:

var app = {
  // Application Constructor
  initialize: function() {
    this.bindEvents();
  },
  // Bind Event Listeners
  // Bind any events that are required on startup. Common events are:
  // 'load', 'deviceready', 'offline', and 'online'.
  bindEvents: function() {
    document.addEventListener('deviceready', this.onDeviceReady, false);
  },
  // deviceready Event Handler
  // The scope of 'this' is the event. In order to call the 'receivedEvent'
  // function, we must explicitly call 'app.receivedEvent(...);'
  onDeviceReady: function() {
    app.receivedEvent('deviceready');
  },
  // Update DOM on a Received Event
  receivedEvent: function(id) {
    var parentElement = document.getElementById(id);
    var listeningElement = parentElement.querySelector('.listening');
    var receivedElement = parentElement.querySelector('.received');
    listeningElement.setAttribute('style', 'display:none;');
    receivedElement.setAttribute('style', 'display:block;');
    console.log('Received Event: ' + id);
    console.log('-');
    console.log(cordova);
    console.log('-');
    console.log(cordova.plugins.barcodeScanner);
    console.log('-');
    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);
      }
    );
  }
};

它基本上是默认的Phonegap插件面板。问题是它无法识别cordova.plugin.barcodeScanner。我已经通过Phonegap Windows工具创建了该项目,并在文件夹中运行了cordova plugin add cordova-plugin-statusbar命令。请帮帮我,我看不到任何这样的代码示例。谢谢

您可以在javascript中调用getScanner()函数onClick事件Read More Here

 function getScanner(){
    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);
      }
    ); }

如果不起作用,请告诉我。。