条码扫描器 PhoneGap 插件问题

3
我正在尝试在我的phonegap项目中实现条形码扫描器,但是我完全迷失了方向,因为我阅读了很多与条形码扫描器相关的主题,所有提供的可能的解决方案都不适用于我。
首先,一些教程和文档说我必须使用cordova.plugin.barcodeScanner.scan(...)。但对我来说,cordova.plugin总是未定义的。
还有一些人说我必须执行cordova.require("cordova/plugin/BarcodeScanner"),但它不起作用,当我运行我的应用程序时,我会得到以下错误:"module "cordova/plugin/BarcodeScanner" not found."

你安装了插件吗?你是如何构建你的项目的? - QuickFix
使用哪个PhoneGap/Cordova版本? - Mister Smith
1个回答

2
如果您使用PhoneGap Build... 这是一个实现例子...
在config.xml文件中添加这行:
<!-- We'll include the Barcode plugin  -->
<gap:plugin name="com.phonegap.plugins.barcodescanner" />

然后在 index.html 文件中:

<script type="text/javascript">
function Scan() {
    cordova.plugins.barcodeScanner.scan(
      function (result) {
          window.open(result.text,'_self', 'location=no') //Opens URL in browser
          //alert("We got a barcode\n" +
          //      "Result: " + result.text + "\n" +
          //      "Format: " + result.format + "\n" +
          //      "Cancelled: " + result.cancelled);
      }, 
      function (error) {
          alert("Scanning failed: " + error);
      }
    );
}
</script>

在页面的body部分用按钮调用脚本:

<button onclick="Scan()">Barcode</button>

祝你好运!


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接