【问题标题】:Cordova Javascript QR Scanner Plugin not working on Android (cordova-plugin-qrscanner)Cordova Javascript QR 扫描仪插件无法在 Android 上运行 (cordova-plugin-qrscanner)
【发布时间】:2020-05-11 18:29:03
【问题描述】:

我遵循了文档here

下面函数“qrPrep()”下的代码运行。但是,“qrScan01”功能不起作用。那是应该显示相机扫描代码的功能。

从 HTML 中调用它为;

<button onclick="qrPrep();">QR Prepare</button>
<button onclick="qrScan01();">QR Scan</button>
<button onclick="qrShow();">QR Show</button>

这不按预期工作:

这是我的代码:

function qrPrep() {
    //alert ('Button Clicked.');
    window.QRScanner.prepare(onDone); // show the prompt
    function onDone(err, status) {
        if (err) {
            // here we can handle errors and clean up any loose ends.
            alert('error');
            console.error(err);
        }
        if (status.authorized) {
            // W00t, you have camera access and the scanner is initialized.
            // QRscanner.show() should feel very fast.
            alert('Authorized.'); //This works.
        } else if (status.denied) {
            // The video preview will remain black, and scanning is disabled. We can
            // try to ask the user to change their mind, but we'll have to send them
            // to their device settings with `QRScanner.openSettings()`.
            alert('Denied.');
        } else {
            // we didn't get permission, but we didn't get permanently denied. (On
            // Android, a denial isn't permanent unless the user checks the "Don't
            // ask again" box.) We can ask again at the next relevant opportunity.
            alert('Not permanently denied.');
        }
    }
}

function qrScan01() {
    console.log("Button clicked.");
    QRScanner.scan(displayContents); //this does not work
    //Also tried window.QRScanner - doesn't work either.

    function displayContents(err, text) {
        console.log("Entered.");
        if (err) {
            // an error occurred, or the scan was canceled (error code `6`)
            console.log('error', err);
            alert('An error occurred.');
        } else {
            // The scan completed, display the contents of the QR code:
            console.log('text', text);
            alert(text);
        }
    }
}

function qrShow() {
    QRScanner.show(function(status) {
        console.log(status); //this only shows status like authorized: true etc.
    });
}

这里是 LOGCAT 输出。看起来相机正在启动,但应用程序屏幕崩溃并导航回 /index.html:

D/CameraPreview: resume()
D/CameraInstance: Opening camera
D/CameraInstance: Configuring camera
I/CameraManager: Camera Display Orientation: 90
I/CameraManager: Initial camera parameters: preview-size=1920x1080;video-size=1920x1080;preferred-preview-size-for-video=1920x1080;
I/CameraConfiguration: Requesting focus mode value from among: [auto]
I/CameraConfiguration: Supported focus mode values: [infinity, auto, macro, continuous-video, continuous-picture]
I/CameraConfiguration: Can set focus mode to: auto
I/CameraConfiguration: Focus mode already set to auto
I/CameraConfiguration: Requesting flash mode value from among: [off]
I/CameraConfiguration: Supported flash mode values: [off, auto, on, torch, red-eye]
I/CameraConfiguration: Can set flash mode to: off
I/CameraConfiguration: Flash mode already set to off
I/PreviewScalingStrategy: Viewfinder size: 2621x1440
I/PreviewScalingStrategy: Preview in order of preference: [1920x1080, 1920x960, 1920x1440, 1280x720, 1600x1200, 1280x768, 1280x960,
I/CameraManager: Final camera parameters: video-size=1920x1080;preferred-preview-size-for-video=1920x1080;preview-size-values=1920x
I/CenterCropStrategy: Preview: 1080x1920; Scaled: 1474x2621; Want: 1440x2621
I/CameraPreview: Starting preview
D/CameraInstance: Starting preview
D/JsMessageQueue: Set native->JS mode to null
D/CordovaWebViewImpl: onPageDidNavigate(file:///android_asset/www/index.html?)

【问题讨论】:

    标签: android cordova plugins qr-code bitpay


    【解决方案1】:

    在我的公司,我们使用Barcode Scanner Plugin for PhoneGap 实现了一个二维码扫描器。它处理多种扫描码,包括 QR 码,对于我们来说,它似乎比官方的 cordova 插件更容易处理。

    您可以通过以下方式使用它(所有代码示例均改编自文档):

    package.json

    {
      "name": "your-app",
      "dependencies": {
        "@ionic-native/barcode-scanner": "^5.19.1",
        "phonegap-plugin-barcodescanner": "^8.1.0",
        [...]
      },
      "cordova": {
        "plugins": {
          "phonegap-plugin-barcodescanner": {}
        }
      }
    }
    

    app.module.ts

    import {BarcodeScanner} from '@ionic-native/barcode-scanner/ngx';
    
    @NgModule({
        declarations: [AppComponent],
        entryComponents: [...],
        imports: [...],
        providers: [
            BarcodeScanner,
            QrService,  // an example service to handle QR codes; can also be omitted
            [...]
        ],
        bootstrap: [AppComponent]
    })
    export class AppModule {}
    

    qr.service.ts(示例服务)

    import { Injectable } from '@angular/core';
    import { from, Observable } from 'rxjs';
    import { map } from 'rxjs/operators';
    import { BarcodeScanner, BarcodeScanResult } from '@ionic-native/barcode-scanner/ngx';
    
    @Injectable({
        providedIn: 'root'
    })
    export class QrService {
    
        constructor(private barcodeScanner: BarcodeScanner) { }
    
        getQrCode$(): Observable<any> {
            return from(this.barcodeScanner.scan())
                .pipe(
                    map((data: BarcodeScanResult) => {
                        const barCodeData = JSON.parse(data.text);
                        return barCodeData;
                    })
                );
        }
    }
    
    

    【讨论】:

    • 嗨,我一点也不明白。我根本没有使用 PhoneGap。我只是使用 Cordova 在 Android 上构建和运行我的 HTML/CSS/Javascript 代码。如果没有PhoneGap,这个插件可以工作吗?另外,我不使用 Ionic,但我在您的代码中看到了一些与 Ionic 相关的内容 - 需要吗?
    • 你可以在没有PhoneGap的情况下使用这个插件,因为Cordova实际上是PhoneGap的衍生产品。抱歉,我没有意识到您没有使用 Ionic,所以很遗憾,如果没有 Ionic 条形码扫描仪服务,我无法说明该插件是如何工作的......
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-26
    相关资源
    最近更新 更多