package io.scanbot.sdk.reactnative import android.app.Activity import android.app.Application import com.facebook.react.bridge.Promise import com.facebook.react.bridge.ReactApplicationContext import com.facebook.react.bridge.ReactContextBaseJavaModule import com.facebook.react.bridge.ReactMethod import com.facebook.react.bridge.ReadableArray import com.facebook.react.bridge.ReadableMap import io.scanbot.sdk.reactnative.extensions.toJSON import io.scanbot.sdk.reactnative.extensions.toJSONArray import io.scanbot.sdk_wrapper.document.operations.barcode_detection.SBDBarcodesDetection import io.scanbot.sdk_wrapper.document.operations.doc_analyzer.SBDDocumentQualityAnalyzer import io.scanbot.sdk_wrapper.document.operations.doc_detection.SBDDocumentDetection import io.scanbot.sdk_wrapper.document.operations.document.SBDDocument import io.scanbot.sdk_wrapper.document.operations.image_filters.SBDImageFilters import io.scanbot.sdk_wrapper.document.operations.image_operations.SBDImageOperations import io.scanbot.sdk_wrapper.document.operations.legacy_page.SBDLegacyPage import io.scanbot.sdk_wrapper.document.operations.license.SBDLicenseOperations import io.scanbot.sdk_wrapper.document.operations.ocr.SBDOcr import io.scanbot.sdk_wrapper.document.operations.pdf.SBDPdf import io.scanbot.sdk_wrapper.document.operations.pdf_extraction.SBDExtractFromPdf import io.scanbot.sdk_wrapper.document.operations.recognizers.check.SBDCheckRecognizer import io.scanbot.sdk_wrapper.document.operations.recognizers.ehic.SBDEhicRecognizer import io.scanbot.sdk_wrapper.document.operations.recognizers.generic_doc.SBDGenericDocRecognizer import io.scanbot.sdk_wrapper.document.operations.recognizers.medical_cert.SBDMedicalCertRecognizer import io.scanbot.sdk_wrapper.document.operations.recognizers.mrz.SBDMrzRecognizer import io.scanbot.sdk_wrapper.document.operations.setup.SBDSetupOperations import io.scanbot.sdk_wrapper.document.operations.tif.SBDTiff import org.json.JSONObject import kotlin.math.roundToInt class ScanbotSDKModule(reactContext: ReactApplicationContext) : ReactContextBaseJavaModule(reactContext) { companion object { const val NAME = "RNScanbotSDK" } override fun getName(): String { return NAME } @ReactMethod fun initializeSDK(options: ReadableMap, promise: Promise) { reactApplicationContextIfActiveOrWarn?.let { SBDSetupOperations.initializeSdk( it.applicationContext as Application, options.toJSON(), ScanbotSDKPluginResultDelegate(promise) ) } ?: run { promise.reject("", "React Native Instance has already disappeared.") } } @ReactMethod fun getLicenseInfo(promise: Promise) { SBDLicenseOperations.getLicenseInfo(ScanbotSDKPluginResultDelegate(promise)) } @ReactMethod fun detectDocument(imageFileUri: String, promise: Promise) { SBDDocumentDetection.detectDocumentOnImage( imageFileUri, ScanbotSDKPluginResultDelegate(promise) ) } @ReactMethod fun documentQualityAnalyzer(configuration: ReadableMap, promise: Promise) { SBDDocumentQualityAnalyzer.analyze( configuration.toJSON(), ScanbotSDKPluginResultDelegate(promise) ) } @ReactMethod fun applyImageFilters(imageFileUri: String, filters: ReadableArray, promise: Promise) { val filterAsListOfJsons = filters.toArrayList().map { JSONObject(it as Map) } SBDImageFilters.applyFiltersOnImage( imageFileUri, filterAsListOfJsons, ScanbotSDKPluginResultDelegate(promise) ) } @ReactMethod fun getOCRConfigs(promise: Promise) { SBDOcr.getConfigs(ScanbotSDKPluginResultDelegate(promise)) } @ReactMethod fun performOCR(args: ReadableMap, promise: Promise) { SBDOcr.perform(args.toJSON(), ScanbotSDKPluginResultDelegate(promise)) } @ReactMethod fun createPDF(args: ReadableMap, promise: Promise) { SBDPdf.create(args.toJSON(), ScanbotSDKPluginResultDelegate(promise)) } @ReactMethod fun writeTIFF(args: ReadableMap, promise: Promise) { SBDTiff.write(args.toJSON(), ScanbotSDKPluginResultDelegate(promise)) } @ReactMethod fun extractPagesFromPdf(options: ReadableMap, promise: Promise) { SBDExtractFromPdf.extractLegacyPages( options.toJSON(), ScanbotSDKPluginResultDelegate(promise) ) } @ReactMethod fun extractImagesFromPdf(options: ReadableMap, promise: Promise) { SBDExtractFromPdf.extractImages( options.toJSON(), ScanbotSDKPluginResultDelegate(promise) ) } @ReactMethod fun rotateImage(imageFileUri: String, degrees: Double, promise: Promise) { SBDImageOperations.rotateImage( imageFileUri, degrees, ScanbotSDKPluginResultDelegate(promise) ) } @ReactMethod fun createPage(imageUri: String, promise: Promise) { SBDLegacyPage.createPage(imageUri, ScanbotSDKPluginResultDelegate(promise)) } @ReactMethod fun detectDocumentOnPage(pageMap: ReadableMap, promise: Promise) { SBDDocumentDetection.detectDocumentOnPage( pageMap.toJSON(), ScanbotSDKPluginResultDelegate(promise) ) } @ReactMethod fun detectBarcodesOnImage(options: ReadableMap, promise: Promise) { SBDBarcodesDetection.detectOnImage( options.toJSON(), ScanbotSDKPluginResultDelegate(promise) ) } @ReactMethod fun applyImageFiltersOnPage(pageMap: ReadableMap, filters: ReadableArray, promise: Promise) { val filterAsListOfJsons = filters.toArrayList().map { JSONObject(it as Map) } SBDImageFilters.applyFiltersOnPage( pageMap.toJSON(), filterAsListOfJsons, ScanbotSDKPluginResultDelegate(promise) ) } @ReactMethod fun rotatePage(pageMap: ReadableMap, times: Double, promise: Promise) { SBDLegacyPage.rotatePage( pageMap.toJSON(), times.roundToInt(), ScanbotSDKPluginResultDelegate(promise) ) } @ReactMethod fun removePage(pageMap: ReadableMap, promise: Promise) { SBDLegacyPage.removePage(pageMap.toJSON(), ScanbotSDKPluginResultDelegate(promise)) } @ReactMethod fun setDocumentImage(pageMap: ReadableMap, imageUri: String, promise: Promise) { SBDLegacyPage.setDocumentImage( pageMap.toJSON(), imageUri, ScanbotSDKPluginResultDelegate(promise) ) } @ReactMethod fun cleanup(promise: Promise) { SBDSetupOperations.cleanup(ScanbotSDKPluginResultDelegate(promise)) } @ReactMethod fun recognizeMrz(imageFileUri: String, promise: Promise) { SBDMrzRecognizer.recognize( imageFileUri, ScanbotSDKPluginResultDelegate(promise) ) } @ReactMethod fun recognizeCheck(map: ReadableMap, promise: Promise) { val imageFileUri = map.getString("imageFileUri")!! val acceptedCheckStandards = map.getArray("acceptedCheckStandards")?.toJSONArray() SBDCheckRecognizer.recognize( imageFileUri, acceptedCheckStandards, ScanbotSDKPluginResultDelegate(promise) ) } @ReactMethod fun recognizeMedicalCertificate(map: ReadableMap, promise: Promise) { SBDMedicalCertRecognizer.recognize( map.toJSON(), ScanbotSDKPluginResultDelegate(promise) ) } @ReactMethod fun recognizeEHIC(imageFileUri: String, promise: Promise) { SBDEhicRecognizer.recognize( imageFileUri, ScanbotSDKPluginResultDelegate(promise) ) } @ReactMethod fun recognizeGenericDocument(map: ReadableMap, promise: Promise) { SBDGenericDocRecognizer.recognize( map.toJSON(), ScanbotSDKPluginResultDelegate(promise) ) } @ReactMethod fun refreshImageUris(map: ReadableMap, promise: Promise) { SBDLegacyPage.refreshImageUris(map.toJSON(), ScanbotSDKPluginResultDelegate(promise)) } @ReactMethod fun getImageData(imageFileUri: String, promise: Promise) { SBDImageOperations.getImageData( imageFileUri, ScanbotSDKPluginResultDelegate(promise) ) } //region DocumentAPIs @ReactMethod fun createDocument(params: ReadableMap, promise: Promise) { SBDDocument.create(params.toJSON(), ScanbotSDKPluginResultDelegate(promise)) } @ReactMethod fun createDocumentFromLegacyPages(params: ReadableMap, promise: Promise) { SBDDocument.createFromLegacyPages(params.toJSON(), ScanbotSDKPluginResultDelegate(promise)) } @ReactMethod fun createDocumentFromPDF(pdfUri: String, promise: Promise) { SBDDocument.createFromPDF(pdfUri, ScanbotSDKPluginResultDelegate(promise)) } @ReactMethod fun documentExists(documentID: String, promise: Promise) { SBDDocument.exists(documentID, ScanbotSDKPluginResultDelegate(promise)) } @ReactMethod fun loadDocument(documentID: String, promise: Promise) { SBDDocument.load(documentID, ScanbotSDKPluginResultDelegate(promise)) } @ReactMethod fun storedDocumentIDs(promise: Promise) { SBDDocument.storedIDs(ScanbotSDKPluginResultDelegate(promise)) } @ReactMethod fun cloneDocument(documentID: String, promise: Promise) { SBDDocument.clone(documentID, ScanbotSDKPluginResultDelegate(promise)) } @ReactMethod fun deleteDocument(documentID: String, promise: Promise) { SBDDocument.delete(documentID, ScanbotSDKPluginResultDelegate(promise)) } @ReactMethod fun deleteAllDocuments(promise: Promise) { SBDDocument.deleteAll(ScanbotSDKPluginResultDelegate(promise)) } @ReactMethod fun createPDFForDocument(params: ReadableMap, promise: Promise) { SBDPdf.createFromDocument(params.toJSON(), ScanbotSDKPluginResultDelegate(promise)) } @ReactMethod fun createTIFFForDocument(params: ReadableMap, promise: Promise) { SBDTiff.createFromDocument(params.toJSON(), ScanbotSDKPluginResultDelegate(promise)) } @ReactMethod fun addPage(params: ReadableMap, promise: Promise) { SBDDocument.addPage(params.toJSON(), ScanbotSDKPluginResultDelegate(promise)) } @ReactMethod fun movePage(params: ReadableMap, promise: Promise) { SBDDocument.movePage(params.toJSON(), ScanbotSDKPluginResultDelegate(promise)) } @ReactMethod fun modifyPage(params: ReadableMap, promise: Promise) { SBDDocument.modifyPage(params.toJSON(), ScanbotSDKPluginResultDelegate(promise)) } @ReactMethod fun removePageFromDocument(params: ReadableMap, promise: Promise) { SBDDocument.removePage(params.toJSON(), ScanbotSDKPluginResultDelegate(promise)) } @ReactMethod fun removeAllPages(documentID: String, promise: Promise) { SBDDocument.removeAllPages(documentID, ScanbotSDKPluginResultDelegate(promise)) } //endregion DocumentAPIs //region Helpers private fun runOperationWithSafeActivity( promise: Promise, operation: (currentActivity: Activity) -> Unit ) { reactApplicationContext.currentActivity?.let { operation(it) } ?: run { promise.reject(Exception("$NAME is not attached to activity")) } } //endregion Helpers }