PhoneGap BarcodeScanner插件无法正常工作

时间:2015-03-08 12:32:39

标签: ios cordova phonegap-plugins phonegap-build barcode-scanner

我已经为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的parahraph。

<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 个答案:

答案 0 :(得分:1)

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