import { NativeEventEmitter } from 'react-native'; import { BarcodeDocumentFormat, BarcodeDocumentParserResult, BarcodeItem, BarcodeItemMapper, BarcodeMappedData, BarcodeScannerConfiguration, BarcodeScannerResult, BarcodeScannerScreenConfiguration, BarcodeScannerUiResult, createSBError, handleImageInput, ImageInput, mapRTUUIResult, MultipleScanningMode, ResultWrapper, SingleScanningMode, withSBErrorHandling, } from '../types'; import { isIOS, ScanbotSDKNativeModule, ScanbotSDKUINativeModule } from './ScanbotSDKModule'; /** * @internal * @hidden */ export const ScanbotBarcodeImpl = { async startScanner( configuration: BarcodeScannerScreenConfiguration ): Promise> { const barcodeItemMapperEventName: string = 'barcodeItemMapperEvent'; let barcodeItemMapperCallback: BarcodeItemMapper | null = null; let barcodeItemMapperEventEmitter: NativeEventEmitter | undefined; if ( configuration.useCase instanceof SingleScanningMode || configuration.useCase instanceof MultipleScanningMode ) { barcodeItemMapperCallback = configuration.useCase.barcodeInfoMapping.barcodeItemMapper; if (barcodeItemMapperCallback) { barcodeItemMapperEventEmitter = new NativeEventEmitter(ScanbotSDKUINativeModule); barcodeItemMapperEventEmitter.removeAllListeners(barcodeItemMapperEventName); barcodeItemMapperEventEmitter.addListener( barcodeItemMapperEventName, (barcodeItem: BarcodeItem) => { const barcodeItemUuid = `${barcodeItem.text}${barcodeItem.upcEanExtension}_${barcodeItem.format}`; barcodeItemMapperCallback!( barcodeItem, (barcodeMappedData: BarcodeMappedData) => ScanbotSDKUINativeModule.onBarcodeItemMapper(barcodeItemUuid, barcodeMappedData), () => ScanbotSDKUINativeModule.onBarcodeItemMapper(barcodeItemUuid, null) ); } ); // On iOS, the communication with the native part is throwing an error (startBarcodeScannerV2) because of the barcodeItemMapper // callback in the configuration class (not parsable), that's why we need to remove it before executing startBarcodeScannerV2 method configuration.useCase.barcodeInfoMapping.barcodeItemMapper = null; } } try { const barcodeResult = await ScanbotSDKUINativeModule.startBarcodeScanner( isIOS ? JSON.stringify(configuration) : configuration, !!barcodeItemMapperCallback ); return mapRTUUIResult(barcodeResult, BarcodeScannerUiResult); } catch (error: any) { throw createSBError(error); } finally { barcodeItemMapperEventEmitter?.removeAllListeners(barcodeItemMapperEventName); barcodeItemMapperEventEmitter = undefined; } }, async scanFromImage(params: { image: ImageInput; configuration: BarcodeScannerConfiguration; }): Promise { return withSBErrorHandling( async () => new BarcodeScannerResult( await ScanbotSDKNativeModule.scanBarcodesFromImage({ configuration: params.configuration, image: handleImageInput(params.image), }) ) ); }, async scanFromPdf(params: { pdfFileUri: string; configuration: BarcodeScannerConfiguration; }): Promise { return withSBErrorHandling( async () => new BarcodeScannerResult(await ScanbotSDKNativeModule.scanBarcodesFromPdf(params)) ); }, async parseDocument(params: { rawText: string; acceptedFormats?: BarcodeDocumentFormat[]; }): Promise { return withSBErrorHandling( async () => new BarcodeDocumentParserResult(await ScanbotSDKNativeModule.parseBarcodeDocument(params)) ); }, };