未捕获的TypeError:无法在file:///android_asset/www/index.html上读取未定义的属性'barcodeScanner':29

时间:2013-11-19 05:33:33

标签: android cordova phonegap-plugins

我正在尝试使用PhoneGap插件扫描条形码并卡在中间。

问题似乎与index.html中的window.plugins.barcodeScanner.scan有关。不知道如何解决它。


的index.html:

   <script type="text/javascript" src="cordova-2.2.0.js"></script>
    <script type="text/javascript" src="barcodescanner.js"></script>
    <script type="text/javascript">
        function scanCode(){
                window.plugins.barcodeScanner.scan(
                function(result){
                    alert("Scanned Code: " + result.text 
                    + ". Format: " + result.format
                    + ". Cancelled: " + result.cancelled);
                }, 
                function(error){
                    alert("Scan failed: " + error);
                }
            );
        }

        function encodeData(){
            var data = document.getElementById("data").value;
            if (data != ''){
                    window.plugins.barcodeScanner.encode(
                    BarcodeScanner.Encode.TEXT_TYPE, data, 
                    function(success){
                        alert("Encode success: " + success);
                    }, 
                    function(fail){
                        alert("Encoding failed: " + fail);
                    }
                );
            }
            else{
                alert("Please enter some data.");
                return false;
            }
        }
    </script>
</head>

<body>
    <h3>Barcode/QR Code Scanner And Encoder</h3>

    <input type="button" value="Scan Code" onclick="scanCode();"/><br/><br/>

    Data : <br/>
    <input type="text" name="data" id="data" /><br/><br/>
    <input type="button" value="Encode Data" onclick="encodeData();"/>
</body>


barcodescanner.js:


/ **  * cordova在 <修订版BSD许可条款下可用  *麻省理工学院执照(2008年)。有关全文,请参阅http://opensource.org/licenses/alphabetical。  *  *版权所有(c)Matt Kane 2010  * IBM Corporation版权所有(c)2011  * /

    var exec = require("cordova/exec");

    /**
     * Constructor.
     *
     * @returns {BarcodeScanner}
     */
    function BarcodeScanner() {

        /**
         * Encoding constants.
         *
         * @type Object
         */
        this.Encode = {
            TEXT_TYPE: "TEXT_TYPE",
            EMAIL_TYPE: "EMAIL_TYPE",
            PHONE_TYPE: "PHONE_TYPE",
            SMS_TYPE: "SMS_TYPE"
            //  CONTACT_TYPE: "CONTACT_TYPE",  // TODO:  not implemented, requires passing a Bundle class from Javascript to Java
            //  LOCATION_TYPE: "LOCATION_TYPE" // TODO:  not implemented, requires passing a Bundle class from Javascript to Java
        };
    };

    /**
     * Read code from scanner.
     *
     * @param {Function} successCallback This function will recieve a result object: {
     *        text : '12345-mock',    // The code that was scanned.
     *        format : 'FORMAT_NAME', // Code format.
     *        cancelled : true/false, // Was canceled.
     *    }
     * @param {Function} errorCallback
     */
    BarcodeScanner.prototype.scan = function (successCallback, errorCallback) {
        if (errorCallback == null) {
            errorCallback = function () {
            };
        }

        if (typeof errorCallback != "function") {
            console.log("BarcodeScanner.scan failure: failure parameter not a function");
            return;
        }

        if (typeof successCallback != "function") {
            console.log("BarcodeScanner.scan failure: success callback parameter must be a function");
            return;
        }

        exec(successCallback, errorCallback, 'BarcodeScanner', 'scan', []);
    };

    //-------------------------------------------------------------------
    BarcodeScanner.prototype.encode = function (type, data, successCallback, errorCallback, options) {
        if (errorCallback == null) {
            errorCallback = function () {
            };
        }

        if (typeof errorCallback != "function") {
            console.log("BarcodeScanner.encode failure: failure parameter not a function");
            return;
        }

        if (typeof successCallback != "function") {
            console.log("BarcodeScanner.encode failure: success callback parameter must be a function");
            return;
        }

        exec(successCallback, errorCallback, 'BarcodeScanner', 'encode', [
            {"type": type, "data": data, "options": options}
        ]);
    };

    var barcodeScanner = new BarcodeScanner();
    module.exports = barcodeScanner;

0 个答案:

没有答案