UNPKG

react-native-scanbot-sdk

Version:

Scanbot Document and Barcode Scanner SDK React Native Plugin for Android and iOS

689 lines (678 loc) 24.5 kB
import { NativeEventEmitter, NativeModules, Platform } from 'react-native'; import { AnalyticsEvent } from './analytics/Analytics'; import { AnalyticsSubscriber } from './analytics/AnalyticsSubscriber'; import { BarcodeScannerConfiguration, BarcodeScannerResult } from './barcode'; import { AddPageParams, ApplyImageFiltersOnPageResult, ApplyImageFiltersResult, CheckScannerScreenConfiguration, CreateDocumentParams, CreatePDFArguments, CreatePDFResult, CreatePageResult, CreateTIFFResult, CroppingConfiguration, CroppingResult, DetectDocumentOnPageResult, DetectDocumentResult, DocumentExistsResult, DocumentFromLegacyPagesParams, DocumentScannerMetadata, DocumentScannerResult, DocumentScannerScreenConfiguration, ExtractImagesFromPdfArguments, ExtractImagesFromPdfResult, ExtractPagesFromPdfArguments, ExtractPagesFromPdfResult, FinderDocumentScannerConfiguration, FinderDocumentScannerResult, HealthInsuranceCardScannerConfiguration, ImageDataResult, LicenseInfoResult, MedicalCertificateScannerConfiguration, MockCameraParams, ModifyPageParams, MovePageParams, OCRConfigsResult, PDFFromDocumentParams, Page, PerformOCRArguments, PerformOCRResult, RefreshImageUrisResult, RemovePageParams, ResultWrapper, ResultWrapperWithMetadata, RotateImageResult, RotatePageResult, ScanbotSdkConfiguration, SetDocumentImageResult, StoredDocumentIDsResult, TIFFFromDocumentParams, VinScannerScreenConfiguration, WriteTIFFArguments, WriteTIFFResult, } from './base'; import { CheckScannerConfiguration, CheckScanningResult } from './check/CheckScannerTypes'; import { ScanbotBarcodeCameraView } from './components/barcode-camera-view/ScanbotBarcodeCameraView'; import { ScanbotCroppingView } from './components/cropping-view/ScanbotCroppingView'; import { ScanbotDocumentScannerView } from './components/document-scanner-view/ScanbotDocumentScannerView'; import { CreditCardScannerConfiguration, CreditCardScanningResult, } from './credit_card/CreditCardTypes'; import { DocumentDataExtractionResult, DocumentDataExtractorConfiguration, DocumentVerificationReport, } from './document_data_extractor'; import { DocumentData } from './document_scanner'; import { GenericDocument } from './documents'; import { DocumentQualityAnalyzerConfiguration, DocumentQualityAnalyzerResult, } from './dqa/DocumentQualityAnalyzerTypes'; import { EuropeanHealthInsuranceCardRecognitionResult, EuropeanHealthInsuranceCardRecognizerConfiguration, } from './ehic/EuropeanHealthInsuranceCardTypes'; import { ParametricFilter } from './image_filters/ParametricFilters'; import { MedicalCertificateScanningParameters, MedicalCertificateScanningResult, } from './medical_certificate/MedicalCertificateTypes'; import { MrzScannerConfiguration, MrzScannerResult } from './mrz/MrzTypes'; import { DeepPartial, mapRTUUIResult } from './utils'; import { VinScannerResult } from './vin/VinScannerTypes'; 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 ScanbotSDKImpl = NativeModules.RNScanbotSDK ? NativeModules.RNScanbotSDK : new Proxy( {}, { get() { throw new Error(LINKING_ERROR); }, } ); const ScanbotSDKUIImpl = NativeModules.RNScanbotRTUUI ? NativeModules.RNScanbotRTUUI : new Proxy( {}, { get() { throw new Error(LINKING_ERROR); }, } ); const ScanbotDocument = { /** * Create a new document. */ createDocument(params: CreateDocumentParams): Promise<DocumentData> { return ScanbotSDKImpl.createDocument(params).then( (result: DeepPartial<DocumentData>) => new DocumentData(result) ); }, /** * Create a new document from legacy pages. */ createDocumentFromLegacyPages(params: DocumentFromLegacyPagesParams): Promise<DocumentData> { return ScanbotSDKImpl.createDocumentFromLegacyPages(params).then( (result: DeepPartial<DocumentData>) => new DocumentData(result) ); }, /** * Create a new document from a PDF file. */ createDocumentFromPDF(pdfUri: string): Promise<DocumentData> { return ScanbotSDKImpl.createDocumentFromPDF(pdfUri).then( (result: DeepPartial<DocumentData>) => new DocumentData(result) ); }, /** * Check if a document with the given ID exists. */ documentExists(documentID: string): Promise<DocumentExistsResult> { return ScanbotSDKImpl.documentExists(documentID); }, /** * Load a document by its ID. */ loadDocument(documentID: string): Promise<DocumentData> { return ScanbotSDKImpl.loadDocument(documentID).then( (result: DeepPartial<DocumentData>) => new DocumentData(result) ); }, /** * Gets all stored document IDs. */ storedDocumentIDs(): Promise<StoredDocumentIDsResult> { return ScanbotSDKImpl.storedDocumentIDs(); }, /** * Clone a document by its ID. */ cloneDocument(documentID: string): Promise<DocumentData> { return ScanbotSDKImpl.cloneDocument(documentID).then( (result: DeepPartial<DocumentData>) => new DocumentData(result) ); }, /** * Delete a document by its ID. */ deleteDocument(documentID: string): Promise<void> { return ScanbotSDKImpl.deleteDocument(documentID); }, /** * Delete all documents. */ deleteAllDocuments(): Promise<void> { return ScanbotSDKImpl.deleteAllDocuments(); }, /** * Creates a PDF for the given document. * Please check the extra options that are part of the input params to modify the created PDF file per your needs. */ createPDF(params: PDFFromDocumentParams): Promise<CreatePDFResult> { return ScanbotSDKImpl.createPDFForDocument(params); }, /** * Creates a TIFF for the given document. * Please check the extra options that are part of the input params to modify the created TIFF file per your needs. */ createTIFF(params: TIFFFromDocumentParams): Promise<CreateTIFFResult> { return ScanbotSDKImpl.createTIFFForDocument(params); }, /** * Add a new page to a document and return the updated document. */ addPage(params: AddPageParams): Promise<DocumentData> { return ScanbotSDKImpl.addPage(params).then( (result: DeepPartial<DocumentData>) => new DocumentData(result) ); }, /** * Move a page in a document and return the updated document. */ movePage(params: MovePageParams): Promise<DocumentData> { return ScanbotSDKImpl.movePage(params).then( (result: DeepPartial<DocumentData>) => new DocumentData(result) ); }, /** * Modify a page in a document and return the updated document. */ modifyPage(params: ModifyPageParams): Promise<DocumentData> { return ScanbotSDKImpl.modifyPage(params).then( (result: DeepPartial<DocumentData>) => new DocumentData(result) ); }, /** * Remove a page from a document. */ removePage(params: RemovePageParams): Promise<DocumentData> { return ScanbotSDKImpl.removePageFromDocument(params).then( (result: DeepPartial<DocumentData>) => new DocumentData(result) ); }, /** * Remove all pages from a document in one batch operation. */ removeAllPages(documentID: string): Promise<DocumentData> { return ScanbotSDKImpl.removeAllPages(documentID).then( (result: DeepPartial<DocumentData>) => new DocumentData(result) ); }, }; const ScanbotSDKUI = { /** * Opens the Ready-To-Use UI Document Scanner screen with the desired configuration. * * @deprecated Use ***startDocumentScanner*** from ***'react-native-scanbot-sdk/ui_v2'*** instead. */ startDocumentScanner( configuration: DocumentScannerScreenConfiguration ): Promise<ResultWrapperWithMetadata<DocumentScannerResult, DocumentScannerMetadata>> { return ScanbotSDKUIImpl.startDocumentScanner(configuration); }, /** * Forces the Ready-To-Use UI Document Scanner screen to close while it is running. * * @deprecated */ closeDocumentScanner(): Promise<void> { return ScanbotSDKUIImpl.closeDocumentScanner(); }, /** * Opens the Ready-To-Use UI Finder Document Scanner screen with the desired configuration. * * @deprecated Use ***startDocumentScanner*** from ***'react-native-scanbot-sdk/ui_v2'*** instead. */ startFinderDocumentScanner( configuration: FinderDocumentScannerConfiguration ): Promise<ResultWrapper<FinderDocumentScannerResult>> { return ScanbotSDKUIImpl.startFinderDocumentScanner(configuration); }, /** * Forces the Ready-To-Use UI Finder Document Scanner screen to close while it is running. * * @deprecated */ closeFinderDocumentScanner(): Promise<void> { return ScanbotSDKUIImpl.closeFinderDocumentScanner(); }, /** * Opens the Ready-To-Use UI Cropping screen with the desired configuration. * * @deprecated Use ***startCroppingScreen*** from ***'react-native-scanbot-sdk/ui_v2'*** instead. */ startCroppingScreen( page: Page, configuration: CroppingConfiguration ): Promise<ResultWrapper<CroppingResult>> { return ScanbotSDKUIImpl.startCroppingScreen(page, configuration); }, /** * Forces the Ready-To-Use UI Cropping screen to close while it is running. * * @deprecated */ closeCroppingScreen(): Promise<void> { return ScanbotSDKUIImpl.closeCroppingScreen(); }, /** * Opens the Ready-To-Use UI European Health Insurance Card Scanner screen with the desired configuration. * * @deprecated Use ***startDocumentDataExtractor*** instead and enable ***DE_HEALTH_INSURANCE_CARD_FRONT*** and ***EU_HEALTH_INSURANCE_CARD*** document formats. */ async startEHICScanner( configuration: HealthInsuranceCardScannerConfiguration ): Promise<ResultWrapper<EuropeanHealthInsuranceCardRecognitionResult>> { return ScanbotSDKUIImpl.startEHICScanner(configuration).then( (result: ResultWrapper<DeepPartial<EuropeanHealthInsuranceCardRecognitionResult>>) => mapRTUUIResult(result, EuropeanHealthInsuranceCardRecognitionResult) ); }, /** * Forces the Ready-To-Use UI European Health Insurance Card Scanner screen to close while it is running. * * @deprecated */ closeEHICScanner(): Promise<void> { return ScanbotSDKUIImpl.closeEHICScanner(); }, /** * Opens the Ready-To-Use UI Medical Certificate Scanner screen with the desired configuration. */ async startMedicalCertificateScanner( configuration: MedicalCertificateScannerConfiguration ): Promise<ResultWrapper<MedicalCertificateScanningResult>> { return ScanbotSDKUIImpl.startMedicalCertificateRecognizer(configuration).then( (result: ResultWrapper<DeepPartial<MedicalCertificateScanningResult>>) => mapRTUUIResult(result, MedicalCertificateScanningResult) ); }, /** * Forces the Ready-To-Use UI Medical Certificate Scanner screen to close while it is running. */ closeMedicalCertificateScanner(): Promise<void> { return ScanbotSDKUIImpl.closeMedicalCertificateRecognizer(); }, /** * Opens the Ready-To-Use UI Check Scanner screen with the desired configuration. * * @deprecated Use ***startCheckScanner*** from ***'react-native-scanbot-sdk/ui_v2'*** instead. */ async startCheckScanner( configuration: CheckScannerScreenConfiguration ): Promise<ResultWrapper<CheckScanningResult>> { return ScanbotSDKUIImpl.startCheckRecognizer(configuration).then( (result: ResultWrapper<DeepPartial<CheckScanningResult>>) => mapRTUUIResult(result, CheckScanningResult) ); }, /** * Forces the Ready-To-Use UI Check Scanner screen to close while it is running. * * @deprecated */ closeCheckScanner(): Promise<void> { return ScanbotSDKUIImpl.closeCheckRecognizer(); }, /** * Opens the Ready-To-Use UI VIN Scanner screen with the desired configuration. * * @deprecated Use ***startVINScanner*** from ***'react-native-scanbot-sdk/ui_v2'*** instead. */ async startVinScanner( configuration: VinScannerScreenConfiguration ): Promise<ResultWrapper<VinScannerResult>> { return ScanbotSDKUIImpl.startVinScanner(configuration).then( (result: ResultWrapper<DeepPartial<VinScannerResult>>) => mapRTUUIResult(result, VinScannerResult) ); }, /** * Forces the Ready-To-Use UI VIN Scanner screen to close while it is running. * * @deprecated */ closeVinScanner(): Promise<void> { return ScanbotSDKUIImpl.closeVinScanner(); }, }; const ScanbotSDK = { UI: ScanbotSDKUI, Document: ScanbotDocument, /** * Initializes the Scanbot SDK with the preferred configuration. */ initializeSDK(config: ScanbotSdkConfiguration): Promise<LicenseInfoResult> { return ScanbotSDKImpl.initializeSDK(config); }, /** * Provides complete information about the current license status. */ getLicenseInfo(): Promise<LicenseInfoResult> { return ScanbotSDKImpl.getLicenseInfo(); }, /** * Returns the available OCR configs. */ getOCRConfigs(): Promise<OCRConfigsResult> { return ScanbotSDKImpl.getOCRConfigs(); }, /** * Removes all files generated by this plugin. */ cleanup(): Promise<void> { return ScanbotSDKImpl.cleanup(); }, /** * Recreates the given pages to refresh the Image URIs. * * @deprecated Use {@link ScanbotSDK.Document} instead. */ refreshImageUris(params: { pages: Page[] }): Promise<RefreshImageUrisResult> { return ScanbotSDKImpl.refreshImageUris(params); }, /** * Detects barcodes on the image represented by the file URI. The image file URI is part of the input arguments. */ async detectBarcodesOnImage(params: { imageFileUri: string; configuration: BarcodeScannerConfiguration; }): Promise<BarcodeScannerResult> { return ScanbotSDKImpl.detectBarcodesOnImage(params).then( (result: DeepPartial<BarcodeScannerResult>) => new BarcodeScannerResult(result) ); }, /** * Applies the given filters to the given image, and returns its URI. */ applyImageFilters( imageFileUri: string, filters: ParametricFilter[] ): Promise<ApplyImageFiltersResult> { return ScanbotSDKImpl.applyImageFilters(imageFileUri, filters); }, /** * Applies the given filters to the given page. * * @deprecated Use {@link ScanbotSDK.Document.modifyPage} instead. */ applyImageFiltersOnPage( page: Page, filters: ParametricFilter[] ): Promise<ApplyImageFiltersOnPageResult> { return ScanbotSDKImpl.applyImageFiltersOnPage(page, filters); }, /** * Creates a page with the image located at the given URI. * * @deprecated Use {@link ScanbotSDK.Document.createDocument} instead. */ createPage(imageUri: string): Promise<CreatePageResult> { return ScanbotSDKImpl.createPage(imageUri); }, /** * Removes the given page from the storage. * * @deprecated Use {@link ScanbotSDK.Document.removePage} instead. */ removePage(page: Page): Promise<void> { return ScanbotSDKImpl.removePage(page); }, /** * Rotates the given page for the number of 90 degree counterclockwise rotations. Negative values will rotate clockwise. * * @deprecated Use {@link ScanbotSDK.Document.modifyPage} instead. */ rotatePage(page: Page, times: number): Promise<RotatePageResult> { return ScanbotSDKImpl.rotatePage(page, times); }, /** * Applies the given image to the desired page. * * @deprecated Use {@link ScanbotSDK.Document} instead. */ setDocumentImage(page: Page, imageUri: string): Promise<SetDocumentImageResult> { return ScanbotSDKImpl.setDocumentImage(page, imageUri); }, /** * Detects a document on the given image and returns the result. */ detectDocument(imageFileUri: string): Promise<DetectDocumentResult> { return ScanbotSDKImpl.detectDocument(imageFileUri); }, /** * Detects a document on the given page and returns the result. * * @deprecated Use {@link ScanbotSDK.Document} instead. */ detectDocumentOnPage(page: Page): Promise<DetectDocumentOnPageResult> { return ScanbotSDKImpl.detectDocumentOnPage(page); }, /** * Extracts images from a PDF represented by the file URL. The PDF file URL is part of the input arguments. */ extractImagesFromPdf(params: ExtractImagesFromPdfArguments): Promise<ExtractImagesFromPdfResult> { return ScanbotSDKImpl.extractImagesFromPdf(params); }, /** * Extracts images from a PDF represented by the file URL, creates pages from them and returns the created pages. * The PDF file URL is part of the input arguments. * * @deprecated Use {@link ScanbotSDK.Document.createDocumentFromPDF} instead. */ extractPagesFromPdf(params: ExtractPagesFromPdfArguments): Promise<ExtractPagesFromPdfResult> { return ScanbotSDKImpl.extractPagesFromPdf(params); }, /** * Returns the BASE64 Image Data for the given image. */ getImageData(imageFileUri: string): Promise<ImageDataResult> { return ScanbotSDKImpl.getImageData(imageFileUri); }, /** * Rotates the given image by the specified degrees counterclockwise. Negative values will rotate clockwise. */ rotateImage(imageFileUri: string, degrees: number): Promise<RotateImageResult> { return ScanbotSDKImpl.rotateImage(imageFileUri, degrees); }, /** * Detects the quality of the document on a still image. */ async documentQualityAnalyzer(params: { imageFileUri: string; configuration: DocumentQualityAnalyzerConfiguration; }): Promise<DocumentQualityAnalyzerResult> { return ScanbotSDKImpl.documentQualityAnalyzer(params).then( (result: DeepPartial<DocumentQualityAnalyzerResult>) => new DocumentQualityAnalyzerResult(result) ); }, /** * Recognizes a Check on the given image. */ async recognizeCheck(params: { imageFileUri: string; configuration: CheckScannerConfiguration; }): Promise<CheckScanningResult> { return ScanbotSDKImpl.recognizeCheck(params).then( (result: DeepPartial<CheckScanningResult>) => new CheckScanningResult(result) ); }, /** * Recognizes an MRZ on the given image. */ async recognizeMrz(params: { imageFileUri: string; configuration: MrzScannerConfiguration; }): Promise<MrzScannerResult> { return ScanbotSDKImpl.recognizeMrz(params).then( (result: DeepPartial<MrzScannerResult>) => new MrzScannerResult(result) ); }, /** * Recognizes a Medical Certificate on the given image. * Modify the result with extra options that are part of the input arguments. */ async recognizeMedicalCertificate(params: { imageFileUri: string; configuration: MedicalCertificateScanningParameters; }): Promise<MedicalCertificateScanningResult> { return ScanbotSDKImpl.recognizeMedicalCertificate(params).then( (result: DeepPartial<MedicalCertificateScanningResult>) => new MedicalCertificateScanningResult(result) ); }, /** * Recognizes a European Health Insurance Card (EHIC) on the given image. * * @deprecated Use {@link documentDataExtractor} instead and enable ***EuropeanHealthInsuranceCardConfiguration*** configuration element. */ async recognizeEHIC(params: { imageFileUri: string; configuration: EuropeanHealthInsuranceCardRecognizerConfiguration; }): Promise<EuropeanHealthInsuranceCardRecognitionResult> { return ScanbotSDKImpl.recognizeEHIC(params).then( (result: DeepPartial<EuropeanHealthInsuranceCardRecognitionResult>) => new EuropeanHealthInsuranceCardRecognitionResult(result) ); }, /** * Recognizes a Credit Card on the given image. * Modify the result with extra options that are part of the configuration. */ async recognizeCreditCard(params: { imageFileUri: string; configuration: CreditCardScannerConfiguration; }): Promise<CreditCardScanningResult> { return ScanbotSDKImpl.recognizeCreditCard(params).then( (result: DeepPartial<CreditCardScanningResult>) => new CreditCardScanningResult(result) ); }, /** * Extract data on the given image. * Set the expected document formats or leave it empty/undefined to recognize all supported document formats. */ async documentDataExtractor(params: { imageFileUri: string; configuration: DocumentDataExtractorConfiguration; }): Promise<DocumentDataExtractionResult> { return ScanbotSDKImpl.documentDataExtractor(params).then( (result: DeepPartial<DocumentDataExtractionResult>) => new DocumentDataExtractionResult(result) ); }, /** * Performs OCR on given images. Set preferred ***ocrConfiguration*** engine, or leave it undefined to use the default one which is ***OCRScanbotEngineConfiguration***. * If ***OCRTesseractConfiguration*** is used, the expected ***languages*** need to be set. */ performOCR(params: PerformOCRArguments): Promise<PerformOCRResult> { return ScanbotSDKImpl.performOCR(params); }, /** * Creates a PDF using the given list of image file URIs. * Please check the extra options that are part of the input arguments to modify the created PDF file per your needs. */ createPDF(params: CreatePDFArguments): Promise<CreatePDFResult> { return ScanbotSDKImpl.createPDF(params); }, /** * Creates a TIFF using the given list of image file URIs. * Please check the extra options that are part of the input arguments to modify the created TIFF file per your needs. */ writeTIFF(params: WriteTIFFArguments): Promise<WriteTIFFResult> { return ScanbotSDKImpl.writeTIFF(params); }, /** * Verifies the given document parts using the DocumentDataExtractorConfiguration. */ async verifyDocument(params: { documentParts: GenericDocument[]; configuration: DocumentDataExtractorConfiguration; }): Promise<DocumentVerificationReport> { return ScanbotSDKImpl.verifyDocument(params).then( (result: DeepPartial<DocumentVerificationReport>) => new DocumentVerificationReport(result) ); }, /** * Mock camera preview by using static images as a frame source. * This is useful for testing purposes. */ mockCamera(params: MockCameraParams): Promise<void> { return ScanbotSDKImpl.mockCamera(params); }, /** * Register analytics service callback to receive analytics events. */ setAnalyticsSubscriber(subscriber: AnalyticsSubscriber | null): Promise<void> { const eventEmitter = new NativeEventEmitter(ScanbotSDKImpl); if (subscriber === null) { eventEmitter.removeAllListeners('analyticsEvent'); return ScanbotSDKImpl.removeAnalyticsServiceCallback(); } else { eventEmitter.removeAllListeners('analyticsEvent'); eventEmitter.addListener('analyticsEvent', (event: DeepPartial<AnalyticsEvent>) => { if (event) { subscriber(new AnalyticsEvent(event)); } }); return ScanbotSDKImpl.setAnalyticsServiceCallback(); } }, }; export default ScanbotSDK; export type ScanbotSDKUI = typeof ScanbotSDKUI; export type ScanbotDocument = typeof ScanbotDocument; export * from './barcode'; export * from './base'; export * from './check/CheckScannerTypes'; export * from './credit_card/CreditCardTypes'; export * from './document_data_extractor'; export * from './document_scanner'; export * from './documents'; export * from './dqa/DocumentQualityAnalyzerTypes'; export * from './ehic/EuropeanHealthInsuranceCardTypes'; export * from './frame_accumulation/FrameAccumulationTypes'; export * from './imageRef'; export * from './image_filters/ParametricFilters'; export * from './medical_certificate/MedicalCertificateTypes'; export * from './mrz/MrzTypes'; export * from './ocr_renderer/PdfConfigurationTypes'; export * from './text_pattern_scanner/TextPatternScannerTypes'; export * from './tiff_wrapper/TiffTypes'; export * from './utils'; export * from './vin/VinScannerTypes'; export * from './components/CameraViewTypes'; export * from './components/barcode-camera-view/ScanbotBarcodeCameraViewProperties'; export * from './components/cropping-view/ScanbotCroppingViewProperties'; export * from './components/document-scanner-view/ScanbotDocumentScannerViewProperties'; export { ScanbotBarcodeCameraView, ScanbotCroppingView, ScanbotDocumentScannerView };