react-native-scanbot-sdk
Version:
Scanbot Document and Barcode Scanner SDK React Native Plugin for Android and iOS
211 lines (185 loc) • 9.32 kB
text/typescript
import { NativeEventEmitter, NativeModules, Platform } from 'react-native';
import { BarcodeItem } from '../barcode';
import { ResultWrapper } from '../base';
import { DocumentData } from '../document_scanner';
import { DeepPartial, mapRTUUIResult } from '../utils';
import { BarcodeMappedData } from './barcode/BarcodeInfoMapping';
import { BarcodeItemMapper } from './barcode/BarcodeItemMapper';
import { BarcodeScannerScreenConfiguration } from './barcode/BarcodeScannerScreenConfiguration';
import { BarcodeScannerUiResult } from './barcode/BarcodeScannerUiResult';
import { MultipleScanningMode } from './barcode/MultipleScanningModeUseCase';
import { SingleScanningMode } from './barcode/SingleScanningModeUseCase';
import { CreditCardScannerScreenConfiguration } from './creditcard/CreditCardScannerScreenConfiguration';
import { CreditCardScannerUiResult } from './creditcard/CreditCardScannerUIResult';
import { CroppingConfiguration } from './document/CroppingConfiguration';
import { DocumentScanningFlow } from './document/DocumentScanningFlow';
import { MrzScannerScreenConfiguration } from './mrz/MRZScannerScreenConfiguration';
import { MrzScannerUiResult } from './mrz/MRZScannerUIResult';
import { TextPatternScannerScreenConfiguration } from './textpattern/TextPatternScannerScreenConfiguration';
import { TextPatternScannerUiResult } from './textpattern/TextPatternScannerUIResult';
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 async function startBarcodeScanner(
config: BarcodeScannerScreenConfiguration
): Promise<ResultWrapper<BarcodeScannerUiResult>> {
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.text}${barcodeItem.upcEanExtension}_${barcodeItem.format}`;
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<BarcodeScannerUiResult>>((resolve, reject) => {
ScanbotSDKUIImpl.startBarcodeScanner(isIOS ? JSON.stringify(config) : config, true)
.then((result: ResultWrapper<DeepPartial<BarcodeScannerUiResult>>) => {
barcodeItemMapperEventEmitter?.removeAllListeners(barcodeItemMapperEventName);
barcodeItemMapperEventEmitter = null;
resolve(mapRTUUIResult(result, BarcodeScannerUiResult));
})
.catch((error: any) => {
barcodeItemMapperEventEmitter?.removeAllListeners(barcodeItemMapperEventName);
barcodeItemMapperEventEmitter = null;
reject(error);
});
});
}
}
return ScanbotSDKUIImpl.startBarcodeScanner(isIOS ? JSON.stringify(config) : config, false).then(
(result: ResultWrapper<DeepPartial<BarcodeScannerUiResult>>) =>
mapRTUUIResult(result, BarcodeScannerUiResult)
);
}
/**
* Opens the Ready-To-Use UI Document Scanner screen with the desired configuration.
*/
export async 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);
}
/**
* Opens the Ready-To-Use UI MRZ Scanner screen with the desired configuration.
*/
export async function startMRZScanner(
config: MrzScannerScreenConfiguration
): Promise<ResultWrapper<MrzScannerUiResult>> {
return ScanbotSDKUIImpl.startMRZScannerV2(isIOS ? JSON.stringify(config) : config).then(
(result: ResultWrapper<DeepPartial<MrzScannerUiResult>>) =>
mapRTUUIResult(result, MrzScannerUiResult)
);
}
/**
* Opens the Ready-To-Use UI Text Pattern scanner with the desired configuration.
*/
export function startTextPatternScanner(
config: TextPatternScannerScreenConfiguration
): Promise<ResultWrapper<TextPatternScannerUiResult>> {
return ScanbotSDKUIImpl.startTextPatternScannerV2(isIOS ? JSON.stringify(config) : config);
}
/**
* Opens the Ready-To-Use UI Credit Card scanner with the desired configuration.
*/
export async function startCreditCardScanner(
config: CreditCardScannerScreenConfiguration
): Promise<ResultWrapper<CreditCardScannerUiResult>> {
return ScanbotSDKUIImpl.startCreditCardScannerV2(isIOS ? JSON.stringify(config) : config).then(
(result: ResultWrapper<DeepPartial<CreditCardScannerUiResult>>) =>
mapRTUUIResult(result, CreditCardScannerUiResult)
);
}
export * from './barcode/ArTrackingOverlayConfiguration';
export * from './barcode/BarcodeInfoMapping';
export * from './barcode/BarcodeItemMapper';
export * from './barcode/BarcodeScannerConfiguration';
export * from './barcode/BarcodeScannerScreenConfiguration';
export * from './barcode/BarcodeScannerUiResult';
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/ScanCompletionOverlay';
export * from './common/ScanbotAlertDialog';
export * from './common/TopBarConfiguration';
export * from './common/UserGuidanceConfiguration';
export * from './common/ViewFinderConfiguration';
export * from './creditcard/CreditCardScannerIntroScreenConfiguration';
export * from './creditcard/CreditCardScannerScreenConfiguration';
export * from './creditcard/CreditCardScannerScreenTextLocalization';
export * from './creditcard/CreditCardScannerUIResult';
export * from './creditcard/CreditCardScannerUserGuidance';
export * from './creditcard/CreditCardScanningProgressConfiguration';
export * from './document/AcknowledgementScreenConfiguration';
export * from './document/CameraScreenConfiguration';
export * from './document/CroppingConfiguration';
export * from './document/CroppingResult';
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 './mrz/MRZFinderLayoutPreset';
export * from './mrz/MRZScannerIntroScreenConfiguration';
export * from './mrz/MRZScannerScreenConfiguration';
export * from './mrz/MRZScannerScreenTextLocalization';
export * from './mrz/MRZScannerUIResult';
export * from './textpattern/TextPatternScannerIntroScreenConfiguration';
export * from './textpattern/TextPatternScannerScreenConfiguration';
export * from './textpattern/TextPatternScannerScreenTextLocalization';
export * from './textpattern/TextPatternScannerUIResult';