安装Phonegap/Cordova 3.1插件(条形码扫描器)

Aar*_*her 4 javascript android phonegap-plugins cordova

现在已经尝试了几个小时并取得了一些进展,但没有朝着正确的方向发展.

我已经成功设置了一个Android Cordova项目,该项目加载到手机上运行良好.我只是无法让条形码扫描器插件在Cordova 3.1中工作.我相信它已经正确安装但它没有出现在config.xml中,但它确实出现在cordova_plugins.js文件中.

我在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);
        }
   );
}
Run Code Online (Sandbox Code Playgroud)

现在,当我按下扫描按钮时,似乎运行此代码,但直接跳转到成功功能,只显示带有空白结果的警告框.

我正在使用并通过cordova插件添加的扫描仪是https://github.com/wildabeast/BarcodeScanner

我目前没有将barcodescanner.js文件导入html中,因为我已经使用旧版本的cordova,因为我相信这在3+中的处理方式不同,似乎在cordova_plugins.js文件中定义了?

更新:据我所知,上面的配置似乎没有任何明显错误弹出Eclipse.

小智 5

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

<feature name="BarcodeScanner">
    <param name="android-package" value="com.phonegap.plugins.barcodescanner.BarcodeScanner" />
</feature>
Run Code Online (Sandbox Code Playgroud)

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

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);
  });}
Run Code Online (Sandbox Code Playgroud)