react-native-scanbot-sdk
Version:
Scanbot Document and Barcode Scanner SDK React Native Plugin for Android and iOS
152 lines (129 loc) • 6.29 kB
text/typescript
import { NativeEventEmitter, NativeModules, Platform } from 'react-native';
import { ResultWrapper } from '../customTypes';
import { BarcodeScannerConfiguration } from './barcode/BarcodeScannerConfiguration';
import { BarcodeScannerResult } from './barcode/BarcodeResult';
import { BarcodeMappedData } from './barcode/BarcodeInfoMapping';
import { BarcodeItemMapper } from './BarcodeItemMapper';
import { SingleScanningMode } from './barcode/SingleScanningModeUseCase';
import { MultipleScanningMode } from './barcode/MultipleScanningModeUseCase';
import { BarcodeItem } from './barcode/BarcodeItem';
import { DocumentScanningFlow } from './document/DocumentScanningFlow';
import { DocumentData } from '../document/DocumentData';
import { CroppingConfiguration } from './document/CroppingConfiguration';
const LINKING_ERROR =
`The package 'react-native-scanbot-sdk' doesn't seem to be linked. Make sure: \n\n` +
Platform.select({ ios: "- You have run 'pod install'\n", default: '' }) +
'- You rebuilt the app after installing the package\n' +
'- You are not using Expo Go\n';
const ScanbotSDKUIImpl = NativeModules.RNScanbotRTUUI
? NativeModules.RNScanbotRTUUI
: new Proxy(
{},
{
get() {
throw new Error(LINKING_ERROR);
},
}
);
const isIOS: boolean = Platform.OS === 'ios';
/**
* Opens the Ready-To-Use UI screen for scanning Barcodes and QR-Codes with the desired configuration.
*/
export function startBarcodeScanner(
config: BarcodeScannerConfiguration
): Promise<ResultWrapper<BarcodeScannerResult>> {
const barcodeItemMapperEventName: string = 'barcodeItemMapperEvent';
let barcodeItemMapperCallback: BarcodeItemMapper | null = null;
let barcodeItemMapperEventEmitter: NativeEventEmitter | null = null;
if (
config.useCase instanceof SingleScanningMode ||
config.useCase instanceof MultipleScanningMode
) {
barcodeItemMapperCallback = config.useCase.barcodeInfoMapping.barcodeItemMapper;
if (barcodeItemMapperCallback) {
barcodeItemMapperEventEmitter = new NativeEventEmitter(ScanbotSDKUIImpl);
barcodeItemMapperEventEmitter.removeAllListeners(barcodeItemMapperEventName);
barcodeItemMapperEventEmitter.addListener(
barcodeItemMapperEventName,
(barcodeItem: BarcodeItem) => {
const barcodeItemUuid = `${barcodeItem.textWithExtension}_${barcodeItem.type ?? ''}`;
barcodeItemMapperCallback!(
barcodeItem,
(barcodeMappedData: BarcodeMappedData) =>
ScanbotSDKUIImpl.onBarcodeItemMapper(barcodeItemUuid, barcodeMappedData),
() => ScanbotSDKUIImpl.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
config.useCase.barcodeInfoMapping.barcodeItemMapper = null;
return new Promise<ResultWrapper<BarcodeScannerResult>>((resolve, reject) => {
ScanbotSDKUIImpl.startBarcodeScannerV2(isIOS ? JSON.stringify(config) : config, true)
.then((result: ResultWrapper<BarcodeScannerResult>) => {
barcodeItemMapperEventEmitter?.removeAllListeners(barcodeItemMapperEventName);
barcodeItemMapperEventEmitter = null;
resolve(result);
})
.catch((error: any) => {
barcodeItemMapperEventEmitter?.removeAllListeners(barcodeItemMapperEventName);
barcodeItemMapperEventEmitter = null;
reject(error);
});
});
}
}
return ScanbotSDKUIImpl.startBarcodeScannerV2(isIOS ? JSON.stringify(config) : config, false);
}
/**
* Opens the Ready-To-Use UI Document Scanner screen with the desired configuration.
*/
export function startDocumentScanner(
config: DocumentScanningFlow
): Promise<ResultWrapper<DocumentData>> {
return ScanbotSDKUIImpl.startDocumentScannerV2(isIOS ? JSON.stringify(config) : config);
}
/**
* Opens the Ready-To-Use UI Cropping screen with the desired configuration.
*/
export function startCroppingScreen(
config: CroppingConfiguration
): Promise<ResultWrapper<DocumentData>> {
return ScanbotSDKUIImpl.startCroppingScreenV2(isIOS ? JSON.stringify(config) : config);
}
export * from './barcode/ArTrackingOverlayConfiguration';
export * from './barcode/BarcodeInfoMapping';
export * from './barcode/BarcodeItem';
export * from './barcode/BarcodeRecognizerConfiguration';
export * from './barcode/BarcodeResult';
export * from './barcode/BarcodeScannerConfiguration';
export * from './barcode/BarcodeTextLocalization';
export * from './barcode/BarcodeUseCase';
export * from './barcode/FindAndPickScanningModeUseCase';
export * from './barcode/MultipleScanningModeUseCase';
export * from './barcode/SingleScanningModeUseCase';
export * from './common/ActionBarConfiguration';
export * from './common/CameraConfiguration';
export * from './common/CameraPermission';
export * from './common/Common';
export * from './common/NavigationBarConfiguration';
export * from './common/ScanbotAlertDialog';
export * from './common/TopBarConfiguration';
export * from './common/UserGuidanceConfiguration';
export * from './common/ViewFinderConfiguration';
export * from './document/AcknowledgementScreenConfiguration';
export * from './document/CameraScreenConfiguration';
export * from './document/CroppingConfiguration';
export * from './document/CroppingScreenConfiguration';
export * from './document/CroppingTextLocalization';
export * from './document/DocumentScannerCameraConfiguration';
export * from './document/DocumentScannerGuidanceVisibility';
export * from './document/DocumentScannerOutputSettings';
export * from './document/DocumentScannerScreens';
export * from './document/DocumentScannerTextLocalization';
export * from './document/DocumentScannerUserGuidance';
export * from './document/DocumentScanningFlow';
export * from './document/IntroductionScreenConfiguration';
export * from './document/ReorderPagesScreenConfiguration';
export * from './document/ReviewScreenConfiguration';
export * from './BarcodeItemMapper';