QR/Barcode Reader Plugin
This plugin integrates native QR code and barcode scanning functionality using the device’s camera.
All Premium Plugins are now available for free and open source to developers. Consider becoming Project Sponsor.
Setup and Configuration
- Enable Plugin: Add
QRScannerPluginto theplugins.enabledlist inapp/src/main/assets/swv.properties.# In swv.properties plugins.enabled=QRScannerPlugin,ToastPlugin,... - Dependencies: This plugin relies on the
zxing-android-embeddedlibrary. Ensure the following dependencies are present in yourapp/build.gradlefile (they are included by default in v8.0.0+):implementation 'com.journeyapps:zxing-android-embedded:4.3.0' implementation 'com.google.zxing:core:3.5.2' - Permissions: The app automatically requests the
CAMERApermission declared inAndroidManifest.xmlwhen the scanner is invoked.
Usage
The plugin is controlled via a JavaScript interface.
Starting a Scan from JavaScript
The plugin injects a window.QRScanner object into your web page.
// Open the camera and start scanning for a code
window.QRScanner.scan();
Callbacks in JavaScript
Define callback functions in your JavaScript to handle the results of the scan.
// Called when a code is successfully scanned
window.QRScanner.onScanSuccess = function(contents) {
console.log('Scanned content:', contents);
alert('Scanned: ' + contents);
// Process the scanned data in your web app (e.g., redirect to URL)
};
// Called if the user cancels the scan (e.g., by pressing the back button)
window.QRScanner.onScanCancelled = function() {
console.log('Scan was cancelled by the user.');
};