Click here to Skip to main content
15,868,141 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I took the qr code reader code from this website "https://github.com/wildabeast/BarcodeDemo".I am developing in jquerymobile. My problem is if i place the scan as first page,it works perfectly. if i use this page other than startup page then i'm facing problem, that is when i scan the image first time ,it does not show any result. If scan 2nd time i got result, next 3rd time i am not getting result, next 4th time i am getting result, finally i got the result in even number of times scanning only, this is my application problem.

  <span class="ui-btn-inner custom_button_red" style="margin-removed10px;"><span class="ui-btn-text"  style="color:white;" id="scan">Scan & Buy</span></span>

						
 <script type="text/javascript" src="cordova.js"></script>
 <script type="text/javascript" src="barcodescanner.js"></script> 
 <script type="text/javascript" src="childbrowser.js"></script>
 <script type="text/javascript" src="js/index.js"></script>
 <script type="text/javascript">
	app.initialize();
 </script> 




here "app.initialize()" method is initiated from index.js 




here i'm rendering the code in index.js :

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
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);
        document.getElementById('scan').addEventListener('click', this.scan, false);
        //document.getElementById('encode').addEventListener('click', this.encode, false);
console.log("hi");
   },

    // deviceready Event Handler
    //
    // The scope of `this` is the event. In order to call the `receivedEvent`
    // function, we must explicity 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');
      //alert("1st click");
        var scanner = cordova.require("cordova/plugin/BarcodeScanner");
        // scanner = cordova.require("cordova/plugin/BarcodeScanner");
        //alert("lybrary loading");
        scanner.scan( function (result) {
        alert("scan function starts from here");
            var regx = /^[A-Z0-9a-z]+$/;
                if (regx.test(result.text))
                {
        /*  alert("We got a QRcode\n" +
            "Result: " + result.text + "\n" +
            "Format: " + result.format + "\n");   */
                window.open("productinfonew.html?id="+result.text);
          // document.getElementById("scan").innerHTML = result.text;
            }
            else
            {
                alert("Scanned Cancel,there is no html page with this name");
            }


        }, function (error) {
        alert("scan function error");
            console.log("Scanning failed: ", error);
        } );
    },

   /*  encode: function() {
        var scanner = cordova.require("cordova/plugin/BarcodeScanner");

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

    } */

};
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900