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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19848671/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 17:02:00  来源:igfitidea点击:

Installing Phonegap/Cordova 3.1 plugins (barcodescanner)

javascriptandroidcordovaphonegap-plugins

提问by Aaron Fisher

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.

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

I have this in my index.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);
        }
   );
}

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

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

I am notcurrently 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?

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

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

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

回答by GemK

Yes, You dont need to import any plugin specific javascript file in your index.html. Just Verify that plugin in properly installed in your project by confirming YourProject/res/config.xml file has the following entry:

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

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

For using the plugin, just use the updated syntax of calling the plugin functions -

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

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);
  });}