This package implements a minimalistic barcode-scanning View
for
NativeScript (from v. 7).
- Sample
- Attributes
- Events
- Clearing results
- Dismissal
- Image Parsing
- Barcode Formats
- Copyright Notice
- License
The BarcodeScannerView
exposes a few attributes to control the operation of
barcode scanning:
<BarcodeScannerView
formats="QR_CODE, CODE_39"
preferFrontCamera="false"
isPaused="false"
(scanResult)="barcodeScanned($event)"
/>
import { ScanResultEventData } from '@juit/nativescript-barcodeview'
export function barcodeScanned(event: ScanResultEventData) {
alert(`Scanned "${event.format}" barcode: ${event.text}`)
}
The formats
property is defined as a KnownBarcodeFormat[]
and informs the
barcode scanner what formats should be recognized.
When specified as a string
, the formats are case-insensitive, comma and/or
whitespace separated
See Barcode Formats below for a list of supported formats.
The default (empty array) represents all supported formats.
The isPaused
boolean
attribute specifies whether barcode scanning is
running (false
) or paused (true
)paused or running.
The preferFrontCamera
boolean
attribute specifies whether preferentially
the scanner should use the back camera (false
) or the front one (true
).
The scanResult
event is triggered once a barcode has been scanned. The event
instance associated with this is a ScanResultEventData
defined as follows:
export interface ScanResultEventData extends EventData {
/** The event name, always `scanResult` */
eventName: ScanResultEvent,
/** The `BarcodeScannerView` source of this event */
object: BarcodeScannerView,
/** The `BarcodeFormat` of the scanned barcode */
format: BarcodeFormat;
/** The text contained in the scanned barcode */
text: string;
}
The BarcodeScannerView
will not emit a scanResult
event once it detects the
same barcode. To clear the last result and be notified of the same barcode, you
can call the clearScanResult()
method on its instance.
barcodeScannerView.on('scanResult', (result: ScanResultEventData) => {
// do something with the result and then clear it,
// allowing it to be reported it once more
result.object.clearScanResult()
})
Make sure that the BarcodeScannerView
's own disposeNativeView()
is called
to release the camera and barcode-scanning resources
In some cases (e.g. simulators) it might be necessary to simulate the scanning of a barcode using an image stored on the device.
While this library doesn't support picking images (see the wonderful
@nativescript/imagepicker
plugin for a good implementation), it offers function to scan ImageAsset
s.
import { parseBarcodes, BarcodeFormat } from '@juit/nativescript-barcodeview'
const ImageAsset asset = // ... get this with '@nativescript/imagepicker'
parseBarcode(asset, [ BarcodeFormat.QR_CODE ])
.then((result: scanResult) => {
console.log(`Scanned "${event.format}" barcode from image: ${event.text}`)
})
Format | iOS | Android |
---|---|---|
AZTEC |
✓ | ✓ |
CODABAR |
✗ | ✓ |
CODE_128 |
✓ | ✓ |
CODE_39 |
✓ | ✓ |
CODE_39_MOD_43 |
✓ | ✗ |
CODE_93 |
✓ | ✓ |
DATA_MATRIX |
✓ | ✓ |
EAN_13 |
✓ | ✓ |
EAN_8 |
✓ | ✓ |
INTERLEAVED_2_OF_5 |
✓ | ✗ |
ITF_14 |
✓ | ✓ |
MAXICODE |
✗ | ✓ |
PDF_417 |
✓ | ✓ |
QR_CODE |
✓ | ✓ |
RSS_14 |
✗ | ✓ |
RSS_EXPANDED |
✗ | ✓ |
UPC_A |
✗ | ✓ |
UPC_E |
✓ | ✓ |
UPC_EAN_EXTENSION |
✗ | ✓ |