javascript - 安装 Phonegap/Cordova 3.1 插件 (barcodescanner)

标签 javascript android cordova phonegap-plugins

现在已经尝试了几个小时,取得了一些进展,但方向不对。

我已经成功设置了一个 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);
        }
   );
}

现在,当我按下扫描按钮时,它似乎运行了这段代码,但直接跳转到成功函数,只显示带有空白结果的警告框。

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

目前将barcodescanner.js文件导入到html中,就像我对旧版本的cordova所做的那样,因为我相信这在3+中的处理方式不同并且似乎在cordova_plugins中定义.js 文件?

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

最佳答案

是的,您不需要在 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);
  });}

关于javascript - 安装 Phonegap/Cordova 3.1 插件 (barcodescanner),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19848671/

相关文章:

javascript - 计算 bool 值数组中真实成员的数量

javascript - p5.j​​s中让粒子出现OnClick函数

php - javascript html弹出窗口

Android Studio 渲染问题,找不到类

java - java 中的 toString - System.out.println 与 Android 中的 log

node.js - 在cmd中安装ionic时出错

javascript - 智能表.js : pagination and filter not working

android - 通过国家代码检查手机号码是否有效

cordova - 将现有 Cordova 项目添加到 Visual Studio

android - 为什么我的phonegap build android 命令不起作用?