PhoneGap BarcodeScanner 插件不起作用

Posted

技术标签:

【中文标题】PhoneGap BarcodeScanner 插件不起作用【英文标题】:PhoneGap BarcodeScanner plugin not working 【发布时间】:2015-03-08 12:32:39 【问题描述】:

我已经为 ios 安装了最新版本的 Node.js、PhoneGap 和 PhoneGap 开发者应用程序。我的系统是 Windows 8.1(64 位)。测试设备为最新 iOS 的 iPhone 6。

我正在尝试使用BarcodeScanner plugin,但没有成功。有人可以解释我做错了什么还是这是某种错误?

以下是复制问题的步骤:

phonegap create HelloWorld
cd HelloWorld
phonegap serve

编辑config.xml并添加以下行

<gap:plugin name="com.phonegap.plugins.barcodescanner" version="2.0.0" />

编辑www/index.html。添加一个带有 id btn-scan 的按钮,带有 id 信息的段落和引用 barcodescanner.js

<body>
    <div class="app">
        <h1>PhoneGap</h1>
        <div id="deviceready" class="blink">
            <p class="event listening">Connecting to Device</p>
            <p class="event received">Device is Ready</p>
        </div>
        <button id="btn-scan">Scan</button>
        <p id="info"></p>
    </div>
    <script type="text/javascript" src="cordova.js"></script>
    <script type="text/javascript" src="barcodescanner.js"></script>
    <script type="text/javascript" src="js/index.js"></script>
    <script type="text/javascript">
        app.initialize();
    </script>
</body>

编辑js/index.js 文件...

...
bindEvents: function() 
    document.addEventListener('deviceready', this.onDeviceReady, false);
    document.getElementById('btn-scan').addEventListener('click', this.scan, false);
,
...

第一个失败的例子

scan: function()
    console.log('scanning');

    try 
        var scanner = cordova.require("cordova/plugin/BarcodeScanner");
     catch (e) 
        console.log('Error 1');
        console.log('Name: ' + e.name);
        console.log('Message: ' + e.message);
        console.log('Stack: ' + e.stack);
        return;
    

    try 
        scanner.scan(function (result) 
            alert("We got a barcode\n" +
            "Result: " + result.text + "\n" +
            "Format: " + result.format + "\n" +
            "Cancelled: " + result.cancelled);

            console.log("Scanner result: \n" +
                 "text: " + result.text + "\n" +
                 "format: " + result.format + "\n" +
                 "cancelled: " + result.cancelled + "\n");
            document.getElementById("info").innerHTML = result.text;
            console.log(result);
        , function (error) 
            console.log("Scanning failed: ", error);
        );
     catch (e) 
        console.log('Error 2');
        console.log('Name: ' + e.name);
        console.log('Message: ' + e.message);
        console.log('Stack: ' + e.stack);
        return;
    

,

错误:

[phonegap] [console.log] Received Event: deviceready
[phonegap] [console.log] scanning
[phonegap] [console.log] Error 1
[phonegap] [console.log] Name: undefined
[phonegap] [console.log] Message: undefined
[phonegap] [console.log] Stack: undefined

第二个失败的例子:

    scan: function()
    console.log('scanning');

    try 
        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);
          
       );
     catch (e) 
            console.log('Name: ' + e.name);
            console.log('Message: ' + e.message);
            console.log('Stack: ' + e.stack);
            return;
    
,

错误:

[phonegap] [console.log] Received Event: deviceready
[phonegap] [console.log] scanning
[phonegap] [console.log] Name: TypeError
[phonegap] [console.log] Message: undefined is not an object (evaluating 'cordova.plugins.barcodeScanner')
[phonegap] [console.log] Stack: scan@http://x.x.x.x:x/js/index.js:43:28

【问题讨论】:

【参考方案1】:

您还不能通过 Phonegap 的服务器运行插件。您必须进行构建并将其放在手机上才能进行调试。

【讨论】:

以上是关于PhoneGap BarcodeScanner 插件不起作用的主要内容,如果未能解决你的问题,请参考以下文章

Phonegap 插件:BarcodeScanner 在 Eclipse 中导致错误

PhoneGap: BarcodeScanner 分享插件

iOS 的 phonegap-plugin-barcodescanner(数据矩阵)问题

phonegap 插件barcodescanner 在iOS 上不起作用

phonegap-plugin-barcodescanner 在 ios 上冻结

如何将barcodescanner生成的条形码图像存储在phonegap中