无法让 Pdf417Scanner 作为插件与 Cordova/PhoneGap 一起工作

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

我正在尝试让 Pdf417Scanner 插件 (https://github.com/PDF417/pdf417-phonegap) 与 Cordova/PhoneGap 一起使用。

这是我到目前为止所做的。

  1. 使用 PhoneGap 创建了一个新项目(UI 使用 Framework7)
  2. 添加插件,使用命令phonegap plugin add pdf417-phonegap
  3. 使用命令 phonegap platform add android
  4. 添加了 Android 平台

调用 Pdf417Scanner 进行扫描的 JavaScript 代码片段。大部分代码直接来自他们的 Github 项目文档。

$$(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
        );
    });
});

它确实到达了 console.log("Inside the scan click"); 部分;但不确定它达到 cordova.plugins.pdf417Scanner.scan 后会发生什么 - 它只是不起作用。澄清一下,我直接在我的 Android phone 上测试了这个(使用 Android 7)。

有什么想法吗?有人用过这个吗library/plugin?

问题是有人在 PDF417 团队之前在 NPM 上注册了 pdf417-phonegap,所以当您像这样 phonegap plugin add pdf417-phonegap 安装插件时,您不会得到 https://github.com/PDF417/pdf417-phonegap,但是 https://github.com/alejonext/pdf417-phonegap,这是一个不同的插件,它已被弃用。

要安装插件,请执行以下操作:

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

从当前项目内部进行,或者您可以在外部进行并将 pdf417-phonegap/Pdf417/ 更改为克隆的路径,但请确保保留 /Pdf417/ 部分,因为它们在该文件夹中有插件在根上。

我已经测试过这个并且扫描仪可以工作,我只是在 hex2a 上收到一个错误,因为我没有那个功能。

但你可以从他们的代码中挑选它 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;
}