簡體   English   中英

條碼掃描儀 - Cordova插件

[英]Barcode Scanner - Cordova Plugin

我在Android應用程序中使用了PhoneGap Plugin Barcode Scanner。 當我將它們附加到按鈕上的onclick事件時,函數window.plugins.barcodeScanner.encodewindow.plugins.barcodeScanner.scan工作得很好。

但是,當我嘗試在body / page init / page show事件的onload事件上執行encode函數時,我在Eclipse中遇到以下錯誤

Uncaught TypeError: Cannot call method 'encode' of undefined at file:///android_asset/www/indexx.html:32

謝謝..

您使用的是哪個版本的phonegap? 1.9.0或2.0.0?

2.0.0有新的調用插件的方法。 1.9 ,您將使用:

window.plugins.barcodeScanner.scan( function(result) {
    ...
    ..
}

如果您使用的是2.0.0,請嘗試使用其他方式初始化插件:

window.barcodeScanner = new BarcodeScanner();

function scanBarcode()
{
    window.barcodeScanner.scan(function(result) {
        alert("We got a barcode\n" +
              "Result: " + result.text + "\n" +
              "Format: " + result.format); 
    }, function(error) {
        alert("Error scanning Barcode: " + error);
    });
}

Cordova完成加載后嘗試調用該方法(deviceready事件)

我是科爾多瓦的新手,但基於我所看到的,聽起來像@traumalles是正確的。 要在Cordova加載完成后調用window.plugins.barcodeScanner.encode或window.plugins.barcodeScanner.scan函數,請在javascript文件中執行以下操作:

// Wait for Cordova to load
document.addEventListener("deviceready", onDeviceReady, false);

// Cordova is ready
function onDeviceReady() {
  // As an example, you now have the device name, Cordova version, etc. available
  alert('Device Name: ' + device.name);
  alert('Device Cordova: ' + device.cordova);
  alert('Device Platform: ' + device.platform);
  alert('Device UUID: ' + device.uuid);
  alert('Device Version: ' + device.version);

  // Now call one of your barcode functions, etc.
}

有關更多信息,請參見http://docs.phonegap.com/en/2.0.0/cordova_device_device.md.html#Device

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM