NativeScript BarcodeScanner
Want a quick demo?
Supported barcode types
iOS and Android
- AZTEC (on Android only when passed in explicity via
formats
) - CODE_39
- CODE_93
- CODE_128
- DATA_MATRIX
- EAN_8
- EAN_13
- ITF (also known as ITF14)
- PDF_417 (on Android only when passed in explicity via
formats
) - QR_CODE
- UPC_E
Android only
- CODABAR
- MAXICODE
- RSS_14
- UPC_A
Installation
From the command prompt go to your app's root folder and execute:
tns plugin add nativescript-barcodescanner
iOS runtime permission reason
You've probably seen a permission popup like this before (this plugin will trigger one as well, automatically):
iOS 10+ requires not only this popup, but also a reason. In this case it's "We'd like to use the Camera ..".
You can provide your own reason for accessing the camera by adding something like this to app/App_Resources/ios/Info.plist
:
<key>NSCameraUsageDescription</key>
<string>My reason justifying fooling around with your camera</string>
To not crash your app in case you forgot to provide the reason this plugin adds an empty reason to the .plist
during build. This value gets overridden by anything you specify yourself.
Usage
Tip: during a scan you can use the volume up/down buttons to toggle the torch.
function: scan (single mode)
TypeScript
import { BarcodeScanner } from "nativescript-barcodescanner";
let barcodescanner = new BarcodeScanner();
barcodescanner.scan({
formats: "QR_CODE, EAN_13",
cancelLabel: "EXIT. Also, try the volume buttons!",
cancelLabelBackgroundColor: "#333333",
message: "Use the volume buttons for extra light",
showFlipCameraButton: true,
preferFrontCamera: false,
showTorchButton: true,
beepOnScan: true,
torchOn: false,
closeCallback: () => { console.log("Scanner closed")},
resultDisplayDuration: 500,
orientation: orientation,
openSettingsIfPermissionWasPreviouslyDenied: true
}).then((result) => {
alert({
title: "Scan result",
message: "Format: " + result.format + ",\nValue: " + result.text,
okButtonText: "OK"
});
}, (errorMessage) => {
console.log("No scan. " + errorMessage);
}
);
JavaScript
var BarcodeScanner = require("nativescript-barcodescanner").BarcodeScanner;
var barcodescanner = new BarcodeScanner();
barcodescanner.scan({
formats: "QR_CODE,PDF_417",
cancelLabel: "EXIT. Also, try the volume buttons!",
cancelLabelBackgroundColor: "#333333",
message: "Use the volume buttons for extra light",
showFlipCameraButton: true,
preferFrontCamera: false,
showTorchButton: true,
beepOnScan: true,
torchOn: false,
closeCallback: function () { console.log("Scanner closed"); },
resultDisplayDuration: 500,
orientation: "landscape",
openSettingsIfPermissionWasPreviouslyDenied: true
}).then(
function(result) {
console.log("Scan format: " + result.format);
console.log("Scan text: " + result.text);
},
function(error) {
console.log("No scan: " + error);
}
);
function: scan (bulk / continuous mode)
In this mode the scanner will continuously report scanned codes back to your code,
but it will only be dismissed if the user tells it to, or you call stop
programmatically.
The plugin handles duplicates for you so don't worry about checking those;
every result withing the same scan session is unique unless you set reportDuplicates
to true
.
Here's an example of scanning 3 unique QR codes and then stopping scanning programmatically.
You'll notice that the Promise will no longer receive the result as there may be many results:
JavaScript
var count = 0;
barcodescanner.scan({
formats: "QR_CODE",
continuousScanCallback: function (result) {
count++;
console.log(result.format + ": " + result.text + " (count: " + count + ")");
if (count === 3) {
barcodescanner.stop();
}
},
closeCallback: function () { console.log("Scanner closed"); },
reportDuplicates: false
}).then(
function() {
console.log("We're now reporting scan results in 'continuousScanCallback'");
},
function(error) {
console.log("No scan: " + error);
}
);
function: available
Note that the iOS implementation will always return true
at the moment,
on Android we actually check for a camera to be available.
JavaScript
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
.
On iOS 10+ there's something similar going on.
Since version 1.5.0 you can let the plugin handle this for you
(if need be a prompt will be shown to the user when the scanner launches),
but if for some reason you want to handle permissions yourself you can use these functions.
JavaScript
barcodescanner.hasCameraPermission().then(
function(granted) {
console.log("Has Camera Permission? " + result);
}
);
barcodescanner.requestCameraPermission().then(
function() {
console.log("Camera permission requested");
}
);
Usage with nativescript-angular
When using Angular 2, it is best to inject dependencies into your classes. Here is an example of how you
can set up nativescript-barcodescanner
in an Angular 2 app with dependency injection.
- Register the provider with your module
import { NgModule, ValueProvider } from '@angular/core';
import { BarcodeScanner } from 'nativescript-barcodescanner';
@NgModule({
providers: [
BarcodeScanner
]
})
export class AppModule {}
- Inject it into your component
import { Component, Inject } from '@angular/core';
import { BarcodeScanner } from 'nativescript-barcodescanner';
@Component({ ... })
export class MyComponent {
constructor(private barcodeScanner: BarcodeScanner) {
}
scanBarcode() {
this.barcodeScanner.scan({ ... });
}
}
Webpack usage
If you run into an error when Webpacking, open app.module.ts
and add this:
import { BarcodeScanner } from "nativescript-barcodescanner";
export function createBarcodeScanner() {
return new BarcodeScanner();
}
providers: [
{ provide: BarcodeScanner, useFactory: (createBarcodeScanner) }
]