You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
88 lines
2.2 KiB
88 lines
2.2 KiB
import {
|
|
BarcodeScanner,
|
|
BarcodeFormat,
|
|
LensFacing
|
|
} from '@capacitor-mlkit/barcode-scanning'
|
|
import type { PluginListenerHandle } from '@capacitor/core'
|
|
import { QRScannerService, ScanListener } from './types'
|
|
|
|
export class NativeQRScanner implements QRScannerService {
|
|
private scanListener: ScanListener | null = null
|
|
private isScanning = false
|
|
private listenerHandle: PluginListenerHandle | null = null
|
|
|
|
async checkPermissions(): Promise<boolean> {
|
|
const { camera } = await BarcodeScanner.checkPermissions()
|
|
return camera === 'granted'
|
|
}
|
|
|
|
async requestPermissions(): Promise<boolean> {
|
|
const { camera } = await BarcodeScanner.requestPermissions()
|
|
return camera === 'granted'
|
|
}
|
|
|
|
async isSupported(): Promise<boolean> {
|
|
const { supported } = await BarcodeScanner.isSupported()
|
|
return supported
|
|
}
|
|
|
|
async startScan(): Promise<void> {
|
|
if (this.isScanning) {
|
|
throw new Error('Scanner is already running')
|
|
}
|
|
|
|
try {
|
|
this.isScanning = true
|
|
await BarcodeScanner.startScan({
|
|
formats: [BarcodeFormat.QrCode],
|
|
lensFacing: LensFacing.Back
|
|
})
|
|
|
|
this.listenerHandle = await BarcodeScanner.addListener(
|
|
'barcodesScanned',
|
|
async (result) => {
|
|
if (result.barcodes.length > 0 && this.scanListener) {
|
|
const barcode = result.barcodes[0]
|
|
this.scanListener.onScan(barcode.rawValue)
|
|
await this.stopScan()
|
|
}
|
|
}
|
|
)
|
|
} catch (error) {
|
|
this.isScanning = false
|
|
if (this.scanListener?.onError) {
|
|
this.scanListener.onError(new Error(String(error)))
|
|
}
|
|
throw error
|
|
}
|
|
}
|
|
|
|
async stopScan(): Promise<void> {
|
|
if (!this.isScanning) {
|
|
return
|
|
}
|
|
|
|
try {
|
|
await BarcodeScanner.stopScan()
|
|
this.isScanning = false
|
|
} catch (error) {
|
|
if (this.scanListener?.onError) {
|
|
this.scanListener.onError(new Error(String(error)))
|
|
}
|
|
throw error
|
|
}
|
|
}
|
|
|
|
addListener(listener: ScanListener): void {
|
|
this.scanListener = listener
|
|
}
|
|
|
|
async cleanup(): Promise<void> {
|
|
await this.stopScan()
|
|
if (this.listenerHandle) {
|
|
await this.listenerHandle.remove()
|
|
this.listenerHandle = null
|
|
}
|
|
this.scanListener = null
|
|
}
|
|
}
|
|
|