NativeScript BarcodeScanner
Scan a barcode (or a QR code, or a lot of other formats really)
Installation
From the command prompt go to your app's root folder and execute:
tns plugin add nativescript-barcodescanner
Supported barcode types
iOS and Android
- CODE_39
- CODE_93
- CODE_128
- EAN_8
- EAN_13
- QR_CODE
- UPC_E
- AZTEC (on Android only when passed in explicity via
formats
) - PDF_417 (on Android only when passed in explicity via
formats
)
Android only
- DATA_MATRIX
- CODABAR
- MAXICODE
- ITF
- RSS_14
- UPC_A
Usage
function: scan
var barcodescanner = require("nativescript-barcodescanner");
barcodescanner.scan({
formats: "QR_CODE,PDF_417",
cancelLabel: "Stop scanning",
message: "Go scan something",
preferFrontCamera: false,
showFlipCameraButton: true,
orientation: "landscape"
}).then(
function(result) {
console.log("Scan format: " + result.format);
console.log("Scan text: " + result.text);
},
function(error) {
console.log("No scan: " + error);
}
)
function: available
Note that the Android implementation will always return true
at the moment.
var barcodescanner = require("nativescript-barcodescanner");
barcodescanner.available().then(
function(avail) {
console.log("Available? " + avail);
}
);
function: hasCameraPermission / requestCameraPermission
On Android 6 you need to request permission to use the camera at runtime when targeting API level 23+.
Even if the uses-permission
tag for the Camera is present in AndroidManifest.xml
.
Note that hasCameraPermission
will return true when:
- You're running this on iOS, or
- You're targeting an API level lower than 23, or
- You're using Android < 6, or
- You've already granted permission.
barcodescanner.hasCameraPermission().then(
function(granted) {
console.log("Has Camera Permission? " + result);
}
);
barcodescanner.requestCameraPermission().then(
function() {
console.log("Camera permission requested");
}
);
Note that the scan
function will also check for permission and ask for it if it wasn't previously granted.
If you're relying on that, then you should know that since we're not catching the consent result
the user will then need to allow camera access and launch the scanner again.