From a5aee05a8117eb1cfc80cdd317e619561071573c Mon Sep 17 00:00:00 2001 From: Luca Bartoli Date: Tue, 31 Oct 2017 18:22:30 +0100 Subject: [PATCH] Supporting cordova-plugin-barcodescanner and phonegap-plugin-barcodescanner --- docs/_plugins/barcodescanner.md | 71 ++++++++++++++++++++ src/index.js | 3 +- src/plugins/cordova-plugin-barcodescanner.js | 14 ++++ 3 files changed, 87 insertions(+), 1 deletion(-) create mode 100644 docs/_plugins/barcodescanner.md create mode 100644 src/plugins/cordova-plugin-barcodescanner.js diff --git a/docs/_plugins/barcodescanner.md b/docs/_plugins/barcodescanner.md new file mode 100644 index 0000000..749df4a --- /dev/null +++ b/docs/_plugins/barcodescanner.md @@ -0,0 +1,71 @@ +--- +title: BarcodeScanner +add_url: cordova-plugin-barcodescanner +description: Access the barcodescanner plugin +--- + +The Barcode Scanner is made available in `Vue.cordova.barcodeScanner`. You may then access the two functions to scan and encode barcodes. + +Works with `cordova-plugin-barcodescanner` and the better `phonegap-plugin-barcodescanner` (which supports configuration options as third argument in the scan function and works also on Android API level >=23) + +###### Sample code +With the `phonegap-plugin-barcodescanner`: +```javascript +Vue.cordova.barcodeScanner.scan( + function (result) { + alert('We got a barcode\n' + + 'Result: ' + result.text + '\n' + + 'Format: ' + result.format + '\n' + + 'Cancelled: ' + result.cancelled) + }, + function (error) { + alert('Scanning failed: ' + error) + }, + { + preferFrontCamera: false, // iOS and Android + showFlipCameraButton: true, // iOS and Android + showTorchButton: true, // iOS and Android + torchOn: false, // Android, launch with the torch switched on (if available) + saveHistory: false, // Android, save scan history (default false) + prompt: 'Scan a barcode', // 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 + } + ) +} +``` + +With the `cordova-plugin-barcodescanner`: +```javascript +Vue.cordova.barcodeScanner.scan( + function (result) { + alert('We got a barcode\n' + + 'Result: ' + result.text + '\n' + + 'Format: ' + result.format + '\n' + + 'Cancelled: ' + result.cancelled) + }, + function (error) { + alert('Scanning failed: ' + error) + } + ) +} +``` + +###### Install + +```bash +cordova plugin add phonegap-plugin-barcodescanner +``` +or +```bash +cordova plugin add cordova-plugin-barcodescanner +``` + +###### Source + +phonegap-plugin-barcodescanner + +cordova-plugin-barcodescanner diff --git a/src/index.js b/src/index.js index ccb2467..ad63c74 100644 --- a/src/index.js +++ b/src/index.js @@ -5,7 +5,8 @@ const pluginsList = [ 'cordova-plugin-geolocation', 'cordova-plugin-contacts', 'cordova-plugin-chrome-apps-sockets-tcp', - 'cordova-plugin-sms' + 'cordova-plugin-sms', + 'cordova-plugin-barcodescanner' ] exports.install = (Vue, options) => { diff --git a/src/plugins/cordova-plugin-barcodescanner.js b/src/plugins/cordova-plugin-barcodescanner.js new file mode 100644 index 0000000..b606f6b --- /dev/null +++ b/src/plugins/cordova-plugin-barcodescanner.js @@ -0,0 +1,14 @@ +exports.install = function (Vue, options, cb) { + document.addEventListener('deviceready', () => { + + if (typeof window.cordova.plugins.barcodeScanner === 'undefined') { + return cb(false) + } + + // pass through the barcodeScanner object + Vue.cordova.barcodeScanner = window.cordova.plugins.barcodeScanner + + return cb(true) + + }, false) +}