【cordova-react】cordova-barcodescannerプラグインでスキャンしたURLに遷移する方法

 

少し前にcordova-barcodescanner-pluginを導入したのですが、肝心の読み込みー>そのページに移動するという機能を付けられずにいた

のですが、最近になってようやく実装できました。方法としては、以下の一行を加えるだけです。

<pre>window.location.href = result.text;</pre>

読み込んだresult.textをhrefで遷移しているだけなのでとても簡単に実装することができます。

コード全体がこちらです。

 

<pre>        window.cordova.plugins.barcodeScanner.scan(
            function (result) {
                alert("Success!\n" +
                "Result: " + result.text + "\n" +
                "Format: " + result.format + "\n" +
                "Cancelled: " + result.cancelled);
                window.location.href = result.text;
            },
            function (error) {
                alert("Scanning failed: " + error);
            },
            {
                preferFrontCamera : true, // iOS and Android
                showFlipCameraButton : true, // iOS and Android
                showTorchButton : true, // iOS and Android
                torchOn: true, // Android, launch with the torch switched on (if available)
                saveHistory: true, // Android, save scan history (default false)
                prompt : "Place a barcode inside the scan area", // Android
                resultDisplayDuration: 500, // Android, display scanned text for X ms. 0 suppresses it entirely, default 1500
                formats : "QR_CODE,PDF_417", // default: all but PDF_417 and RSS_EXPANDED
                orientation : "landscape", // Android only (portrait|landscape), default unset so it rotates with the device
                disableAnimations : true, // iOS
                disableSuccessBeep: false // iOS and Android
            }
        );</pre>

 

設定は遷移に影響しないのでお好みで変えていただいても大丈夫だと思います。試したところほとんどのQRコードで遷移成功できたので

実際に使ってみる分にも申し分ない性能だと思います。

今回無事に実装ができたきっかけはまたまたスタックオーバーフロー様の記事です。

リンク記事ー>https://github.com/phonegap/phonegap-plugin-barcodescanner/issues/262

 

>株式会社シーポイントラボ

株式会社シーポイントラボ

TEL:053-543-9889
営業時間:9:00~18:00(月〜金)
住所:〒432-8003
   静岡県浜松市中央区和地山3-1-7
   浜松イノベーションキューブ 315
※ご来社の際はインターホンで「316」をお呼びください

CTR IMG