4

Been trying this for a few hours now and have made a little progress but not in the right direction.

I have successfully setup an Android Cordova project which loads onto a phone and runs fine. I just cannot get the barcode scanner plugin to work in Cordova 3.1. I believe it has installed correctly but it does not appear in the config.xml, it does however appear in the cordova_plugins.js file etc.

I have this in my index.js

function clickScan() {
    var scanner = cordova.require("com.phonegap.plugins.barcodescanner.BarcodeScanner");
    scanner.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);
        }
   );
}

Now when I press the scan button it seems to run this code but jumps straight to the success function and just displays the alert box with blank results.

The scanner I am using and have installed via cordova plugin add is https://github.com/wildabeast/BarcodeScanner

I am not currently importing the barcodescanner.js file into the html as I have done with older versions of cordova as I believe this is handled differently in 3+ and seems to be defined in the cordova_plugins.js file?

Update: As far as I am aware with the config above there does not seem to be any glaring errors popup in Eclipse.

4

1 回答 1

5

是的,您不需要在 index.html 中导入任何插件特定的 javascript 文件。只需通过确认 YourProject/res/config.xml 文件具有以下条目来验证该插件是否正确安装在您的项目中:

<feature name="BarcodeScanner">
    <param name="android-package" value="com.phonegap.plugins.barcodescanner.BarcodeScanner" />
</feature>

要使用插件,只需使用调用插件函数的更新语法 -

function clickScan() {
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);
  });}
于 2013-11-14T13:24:56.470 回答