androidcordovaphonegaphtml-framework-7pdf417

Unable to get Pdf417Scanner working as a plugin with Cordova/PhoneGap


I'm trying to get the Pdf417Scanner plugin (https://github.com/PDF417/pdf417-phonegap) to work with Cordova/PhoneGap.

Here's what I've done so far.

  1. Created a new project using PhoneGap (with Framework7 for the UI)
  2. Added the plugin, using the command phonegap plugin add pdf417-phonegap
  3. Added the Android platform using the command phonegap platform add android

Snippet of JavaScript code that calls Pdf417Scanner, to scan. Most of this code is straight from their Github project documentation.

$$(document).on('deviceready', function() {
    console.log("Device is ready!");

    $$('#scan').on('click', function () {
        console.log("Inside the scan click");

        var types = ["PDF417", "QR Code"];

        /**
         * Initiate scan with options
         * NOTE: Some features are unavailable without a license
         * Obtain your key at http://pdf417.mobi
         */
        var options = {
            beep : true,  // Beep on
            noDialog : true, // Skip confirm dialog after scan
            uncertain : false, //Recommended
            quietZone : false, //Recommended
            highRes : false, //Recommended
            inverseScanning: false,
            frontFace : false
        };

        var licenseiOs = "sRwAAAEQbW9iaS5wZGY0MTcuZGVtbz/roBZ34ygXMQRMupTjSPXnoj0Mz1jPfk1iRX7f78Ux6a+pfXVyW0HCjPTxl5ocxgXWF66PTrtFUbJFCDUpyznreSWY4akvhvqVFfcTYgVEKjB+UqO6vPD5iIaUCaEYhF4dVmM=";

        // This license is only valid for package name "mobi.pdf417.demo"
        var licenseAndroid = "sRwAAAAQbW9iaS5wZGY0MTcuZGVtb2uCzTSwE5Pixw1pJL5UEN7nyXbOdXB61Ysy/sgAYt4SaB0T/g6JvisLn6HtB8LzLDmpFjULMxmB8iLsy3tFdHtMhLWOM6pr0tQmSLGyhrXfe6rVoHAxJtPrFEoCNTk4RjLltQ==";

        cordova.plugins.pdf417Scanner.scan(
            // Register the callback handler
            function callback(scanningResult) {

                // handle cancelled scanning
                if (scanningResult.cancelled == true) {
                    myApp.alert("Cancelled!");
                    return;
                }

                // Obtain list of recognizer results
                var resultList = scanningResult.resultList;

                var resToShow = "";

                // Iterate through all results
                for (var i = 0; i < resultList.length; i++) {
                    // Get individual resilt
                    var recognizerResult = resultList[i];
                    resToShow += "(Result type: " + recognizerResult.resultType + ") <br>"
                    if (recognizerResult.resultType == "Barcode result") {
                        // handle Barcode scanning result
                        var raw = "";
                        if (typeof(recognizerResult.raw) != "undefined" && recognizerResult.raw != null) {
                            raw = " (raw: " + hex2a(recognizerResult.raw) + ")";
                        }
                        resToShow += "(Barcode type: " + recognizerResult.type + ")<br>"
                                     + "Data: " + recognizerResult.data + "<br>"
                                     + raw;
                    } else if (recognizerResult.resultType == "USDL result") {
                        // handle USDL parsing result

                        var fields = recognizerResult.fields;

                        resToShow += /** Personal information */
                                    "USDL version: " + fields[kPPStandardVersionNumber] + "; " +
                                    "Family name: " + fields[kPPCustomerFamilyName] + "; " +
                                    "First name: " + fields[kPPCustomerFirstName] + "; " +
                                    "Date of birth: " + fields[kPPDateOfBirth] + "; " +
                                    "Sex: " + fields[kPPSex] + "; " +
                                    "Eye color: " + fields[kPPEyeColor] + "; " +
                                    "Height: " + fields[kPPHeight] + "; " +
                                    "Street: " + fields[kPPAddressStreet] + "; " +
                                    "City: " + fields[kPPAddressCity] + "; " +
                                    "Jurisdiction: " + fields[kPPAddressJurisdictionCode] + "; " +
                                    "Postal code: " + fields[kPPAddressPostalCode] + "; " +

                                    /** License information */
                                    "Issue date: " + fields[kPPDocumentIssueDate] + "; " +
                                    "Expiration date: " + fields[kPPDocumentExpirationDate] + "; " +
                                    "Issuer ID: " + fields[kPPIssuerIdentificationNumber] + "; " +
                                    "Jurisdiction version: " + fields[kPPJurisdictionVersionNumber] + "; " +
                                    "Vehicle class: " + fields[kPPJurisdictionVehicleClass] + "; " +
                                    "Restrictions: " + fields[kPPJurisdictionRestrictionCodes] + "; " +
                                    "Endorsments: " + fields[kPPJurisdictionEndorsementCodes] + "; " +
                                    "Customer ID: " + fields[kPPCustomerIdNumber] + "; ";
                    }
                    resToShow += "<br><br>";
                }
                myApp.alert(resToShow);
            },

            // Register the error callback
            function errorHandler(err) {
                myApp.alert('Error: ' + err);
            },

            types, options, licenseiOs, licenseAndroid
        );
    });
});

It does get to the console.log("Inside the scan click"); part; but not sure what happens after it hits cordova.plugins.pdf417Scanner.scan - it just doesn't work. Just to clarify, I test this straight on my Android phone (using Android 7).

Any ideas? Has anyone used this library/plugin?


Solution

  • The problem is somebody registered pdf417-phonegap on NPM before PDF417 team did, so when you install the plugin like this phonegap plugin add pdf417-phonegap, you don't get https://github.com/PDF417/pdf417-phonegap, but https://github.com/alejonext/pdf417-phonegap, which is a different plugin and it's deprecated.

    To install the plugin do this:

    git clone https://github.com/PDF417/pdf417-phonegap
    phonegap plugin add pdf417-phonegap/Pdf417/
    

    from inside your current project, or you can do it outside and change pdf417-phonegap/Pdf417/ to the path of your clone, but make sure you keep the /Pdf417/ part as they have the plugin inside that folder instead of being on the root.

    I've tested this and the scanner works, I just get an error on hex2a as I didn't have that function.

    But you can pick it from their code https://github.com/PDF417/pdf417-phonegap/blob/master/www/js/index.js#L21-L27:

    function hex2a(hex) {
        var str = '';
        for (var i = 0; i < hex.length; i += 2) {
            str += String.fromCharCode(parseInt(hex.substr(i, 2), 16));
        }
        return str;
    }