PhoneGap条形码阅读器插件无法正常工作

时间:2016-05-09 14:47:35

标签: javascript cordova plugins barcode

我一直试图在我的应用程序中实现条形码阅读器插件几天,我没有运气。按下按钮时它什么都不做。 多数民众赞成我的javascript文件

    var app = {
// Application Constructor
initialize: function() {
    this.bindEvents();
},
// Bind Event Listeners
//
// Bind any events that are required on startup. Common events are:
// 'load', 'deviceready', 'offline', and 'online'.
bindEvents: function() {
    document.addEventListener('deviceready', this.onDeviceReady, false);
},
// deviceready Event Handler
//
// The scope of 'this' is the event. In order to call the 'receivedEvent'
// function, we must explicitly call 'app.receivedEvent(...);'
onDeviceReady: function() {
    app.receivedEvent('deviceready');
},
// Update DOM on a Received Event
receivedEvent: function(id) {
    var parentElement = document.getElementById(id);
    var listeningElement = parentElement.querySelector('.listening');
    var receivedElement = parentElement.querySelector('.received');

    listeningElement.setAttribute('style', 'display:none;');
    receivedElement.setAttribute('style', 'display:block;');

    console.log('Received Event: ' + id);
},
scan: function() {
    console.log('scanning');

    var scanner = cordova.require("cordova/plugins/BarcodeScanner");

    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);
        /*
        if (args.format == "QR_CODE") {
            window.plugins.childBrowser.showWebPage(args.text, { showLocationBar: false });
        }
        */

    }, function (error) { 
        console.log("Scanning failed: ", error); 
    } );
},
    encode: function() {
    var scanner = cordova.require("cordova/plugins/BarcodeScanner");

    scanner.encode(scanner.Encode.TEXT_TYPE, "http://www.nhl.com", function(success) {
        alert("encode success: " + success);
      }, function(fail) {
        alert("encoding failed: " + fail);
      }
    );

}};

那就是index.html

    <html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <meta name="format-detection" content="telephone=no" />
    <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, height=device-height, target-densitydpi=device-dpi" />
    <link rel="stylesheet" type="text/css" href="css/index.css" />
    <script type="text/javascript" href="JQuery/jquery-1.12.3.min.js"></script>
    <script type="text/javascript" href="JQuery/jquery.mobile-1.4.5.js"></script>
    <link rel="stylesheet" type="text/css" href="JQuery/jquery.mobile-1.4.5.css" />

    <title>Barcode Scanner Demo</title>
</head>
<body>
    <div class="app">
        <h1>Apache Cordova</h1>
        <div id="deviceready" class="blink">
            <p class="event listening">Connecting to Device</p>
            <p class="event received">Device is Ready</p>
        </div>
        <p><a href="#" class="topcoat-button" id="scan">SCAN</a></p>
        <p><a href="#" class="topcoat-button" id="encode">ENCODE</a></p>
        <p id="info"></p>
        <h2>OPENING LINKS:</h2>
        <p><a href="#" class="topcoat-button" onclick="window.open('http://www.nhl.com', '_blank', 'location=yes');">InAppBrowser</a></p>
        <p><a href="#" class="topcoat-button" onclick="window.open('http://www.nhl.com', '_system', 'location=yes');">System Browser</a></p>
        <p><a href="#" class="topcoat-button" onclick="window.open('http://www.yahoo.com', '_self', 'location=yes');">This Webview</a></p>
        <p><a href="#" class="topcoat-button" onclick="window.plugins.childBrowser.showWebPage('http://www.nhl.com', {});">ChildBrowser</a></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>

我是否必须部署应用程序,还是只使用PhoneGap for iOS提供的应用程序? 任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

我相信您需要在设备上编译和安装应用程序或在模拟器中运行(假设您希望条形码扫描设备最好,这样您就可以使用真正的相机)。这与使用PhoneGap for iOS Developer应用程序无关的原因是条形码扫描器似乎不适合您在该应用程序中安装的list of plugins,这是:

<plugin name="cordova-plugin-battery-status" spec="~1.1.0" />
<plugin name="cordova-plugin-camera" spec="~2.0.0" />
<plugin name="cordova-plugin-console" spec="~1.0.1" />
<plugin name="cordova-plugin-contacts" spec="~2.0.0" />
<plugin name="cordova-plugin-device" spec="~1.0.1" />
<plugin name="cordova-plugin-device-motion" spec="~1.1.1" />
<plugin name="cordova-plugin-device-orientation" spec="~1.0.1" />
<plugin name="cordova-plugin-dialogs" spec="~1.1.1" />
<plugin name="cordova-plugin-file" spec="~4.0.0" />
<plugin name="cordova-plugin-file-transfer" spec="~1.3.0" />
<plugin name="cordova-plugin-geolocation" spec="~2.0.0" />
<plugin name="cordova-plugin-globalization" spec="~1.0.1" />
<plugin name="cordova-plugin-inappbrowser" spec="~1.1.1" />
<plugin name="cordova-plugin-media" spec="~2.0.0" />
<plugin name="cordova-plugin-media-capture" spec="~1.0.1" />
<plugin name="cordova-plugin-network-information" spec="~1.0.1" />
<plugin name="cordova-plugin-splashscreen" spec="~3.0.0" />
<plugin name="cordova-plugin-statusbar" spec="~2.0.0" />
<plugin name="cordova-plugin-vibration" spec="~2.0.0" />
<plugin name="phonegap-plugin-contentsync" spec="~1.2.3" />
<plugin name="cordova-plugin-whitelist" spec="~1.0.0" />
<plugin name="cordova-plugin-insomnia" spec="~4.0.1" />
<plugin name="cordova-plugin-ble-central" spec="~1.0.4" />
<plugin name="phonegap-plugin-push" spec="~1.6.3">

以上列表取自PhoneGap Developer app Github repo,config.xml文件。