@juit/nativescript-barcodeview
作者:juit | v1.1.3
为 NativeScript 7 集成条形码扫描视图
npm i --save @juit/nativescript-barcodeview

NativeScript 7 条形码扫描视图

本包实现了 NativeScript(从 v. 7 开始)的一个简约条形码扫描 视图

示例

Sample

属性

BarcodeScannerView 提供了一些属性来控制条形码扫描的操作

XML

<BarcodeScannerView
formats="QR_CODE, CODE_39"
preferFrontCamera="false"
isPaused="false"
(scanResult)="barcodeScanned($event)"
/>

TypeScript

import { ScanResultEventData } from '@juit/nativescript-barcodeview'

export function barcodeScanned(event: ScanResultEventData) {
alert(`Scanned "${event.format}" barcode: ${event.text}`)
}

格式

formats 属性定义为 KnownBarcodeFormat[],并通知条形码扫描器应识别哪些格式。

当指定为 string 时,格式不区分大小写,以逗号和/或空格分隔

请参阅下文 条形码格式 中的支持格式列表。

默认(空数组)代表所有支持的格式。

isPaused

isPaused boolean 属性指定条形码扫描是正在运行(false)还是暂停(true)。

preferFrontCamera

preferFrontCamera boolean 属性指定扫描器是否优先使用后置摄像头(false)或前置摄像头(true)。

事件

当扫描到一个条形码时,将触发 scanResult 事件。与该事件关联的实例是 ScanResultEventData,其定义如下

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;
}

清除结果

BarcodeScannerView 检测到相同的条形码时,它不会发出 scanResult 事件。要清除最后一个结果并通知相同的条形码,可以在其实例上调用 clearScanResult() 方法。

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()
})

取消

确保调用 BarcodeScannerViewdisposeNativeView() 以释放相机和条形码扫描资源

图像解析

在某些情况下(例如模拟器),可能需要使用设备上存储的图像来模拟条形码扫描。

虽然这个库不支持选择图像(请参阅@nativescript/imagepicker 插件以获取良好实现),但它提供了扫描 ImageAsset 的功能。

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}`)
})

条形码格式

格式 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