package io.scanbot.sdk.capacitor import android.content.Intent import com.getcapacitor.JSObject import com.getcapacitor.Plugin import com.getcapacitor.PluginCall import com.getcapacitor.PluginMethod import com.getcapacitor.annotation.CapacitorPlugin import io.scanbot.sdk_wrapper.barcode.rtuui.SBRtuUiRequestCodes 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 io.scanbot.sdk_wrapper.document.rtuui.SBDRtuUi import io.scanbot.sdk_wrapper.document.rtuui.SBDRtuUiRequestCodes import io.scanbot.sdk_wrapper.extensions.getStringOrDefault import org.json.JSONObject @CapacitorPlugin( name = "ScanbotSDKCapacitor", requestCodes = [ SBDRtuUiRequestCodes.DOCUMENT_SCANNER, SBDRtuUiRequestCodes.LEGACY_DOCUMENT_SCANNER, SBDRtuUiRequestCodes.LEGACY_FINDER_DOCUMENT_SCANNER, SBDRtuUiRequestCodes.CROPPING_SCREEN, SBDRtuUiRequestCodes.LEGACY_CROPPING_SCREEN, SBDRtuUiRequestCodes.MRZ_SCANNER, SBDRtuUiRequestCodes.EHIC_SCANNER, SBDRtuUiRequestCodes.TEXT_DATA_SCANNER, SBDRtuUiRequestCodes.LICENSE_PLATE_SCANNER, SBDRtuUiRequestCodes.MC_RECOGNIZER, SBDRtuUiRequestCodes.GENERIC_DOC_RECOGNIZER, SBDRtuUiRequestCodes.CHECK_RECOGNIZER, SBDRtuUiRequestCodes.VIN_SCANNER, SBRtuUiRequestCodes.BARCODE_SCANNER, SBRtuUiRequestCodes.LEGACY_BARCODE_SCANNER, SBRtuUiRequestCodes.LEGACY_BATCH_BARCODE_SCANNER, ] ) class ScanbotSDKCapacitorPlugin : Plugin() { companion object { const val missingRequiredPropertyErrorMsg = "Missing required property " } private var barcodeItemMapperPluginCall: PluginCall? = null @PluginMethod fun initializeSDK(call: PluginCall) { SBDSetupOperations.initializeSdk( activity.application, call.data, ScanbotSDKPluginResultDelegate(call) ) } @PluginMethod fun getLicenseInfo(call: PluginCall) { SBDLicenseOperations.getLicenseInfo(ScanbotSDKPluginResultDelegate(call)) } @PluginMethod fun detectBarcodesOnImage(call: PluginCall) { SBDBarcodesDetection.detectOnImage(call.data, ScanbotSDKPluginResultDelegate(call)) } @PluginMethod fun applyImageFilters(call: PluginCall) { val imageFileUri = call.asStringOrReject("imageFileUri") ?: return val imageFilters = call.asArrayOrReject("filters")?.toList() ?: return SBDImageFilters.applyFiltersOnImage( imageFileUri, imageFilters, ScanbotSDKPluginResultDelegate(call) ) } @PluginMethod fun applyImageFiltersOnPage(call: PluginCall) { val page = call.asJsonOrReject("page") ?: return val imageFilters = call.asArrayOrReject("filters")?.toList() ?: return SBDImageFilters.applyFiltersOnPage(page, imageFilters, ScanbotSDKPluginResultDelegate(call)) } @PluginMethod fun getImageData(call: PluginCall) { val imageFileUri = call.asStringOrReject("imageFileUri") ?: return SBDImageOperations.getImageData(imageFileUri, ScanbotSDKPluginResultDelegate(call)) } @PluginMethod fun rotateImage(call: PluginCall) { val imageFileUri = call.asStringOrReject("imageFileUri") ?: return val degrees = call.asDoubleOrReject("degrees") ?: return SBDImageOperations.rotateImage(imageFileUri, degrees, ScanbotSDKPluginResultDelegate(call)) } @PluginMethod fun createPage(call: PluginCall) { val imageUri = call.asStringOrReject("imageUri") ?: return SBDLegacyPage.createPage(imageUri, ScanbotSDKPluginResultDelegate(call)) } @PluginMethod fun removePage(call: PluginCall) { val page = call.asJsonOrReject("page") ?: return SBDLegacyPage.removePage(page, ScanbotSDKPluginResultDelegate(call)) } @PluginMethod fun rotatePage(call: PluginCall) { val page = call.asJsonOrReject("page") ?: return val times = call.asIntOrReject("times") ?: return SBDLegacyPage.rotatePage(page, times, ScanbotSDKPluginResultDelegate(call)) } @PluginMethod fun setDocumentImage(call: PluginCall) { val page = call.asJsonOrReject("page") ?: return val imageFileUri = call.asStringOrReject("imageFileUri") ?: return SBDLegacyPage.setDocumentImage(page, imageFileUri, ScanbotSDKPluginResultDelegate(call)) } @PluginMethod fun detectDocument(call: PluginCall) { val imageFileUri = call.asStringOrReject("imageFileUri") ?: return SBDDocumentDetection.detectDocumentOnImage( imageFileUri, ScanbotSDKPluginResultDelegate(call) ) } @PluginMethod fun detectDocumentOnPage(call: PluginCall) { val page = call.asJsonOrReject("page") ?: return SBDDocumentDetection.detectDocumentOnPage(page, ScanbotSDKPluginResultDelegate(call)) } @PluginMethod fun documentQualityAnalyzer(call: PluginCall) { SBDDocumentQualityAnalyzer.analyze( call.data, ScanbotSDKPluginResultDelegate(call) ) } @PluginMethod fun extractImagesFromPdf(call: PluginCall) { SBDExtractFromPdf.extractImages(call.data, ScanbotSDKPluginResultDelegate(call)) } @PluginMethod fun extractPagesFromPdf(call: PluginCall) { SBDExtractFromPdf.extractLegacyPages(call.data, ScanbotSDKPluginResultDelegate(call)) } @PluginMethod fun recognizeCheck(call: PluginCall) { val imageFileUri = call.asStringOrReject("imageFileUri") ?: return val acceptedCheckStandards = call.getArray("acceptedCheckStandards", null) SBDCheckRecognizer.recognize( imageFileUri, acceptedCheckStandards, ScanbotSDKPluginResultDelegate(call) ) } @PluginMethod fun recognizeMrz(call: PluginCall) { val imageFileUri = call.asStringOrReject("imageFileUri") ?: return SBDMrzRecognizer.recognize(imageFileUri, ScanbotSDKPluginResultDelegate(call)) } @PluginMethod fun recognizeMedicalCertificate(call: PluginCall) { SBDMedicalCertRecognizer.recognize(call.data, ScanbotSDKPluginResultDelegate(call)) } @PluginMethod fun recognizeEHIC(call: PluginCall) { val imageFileUri = call.asStringOrReject("imageFileUri") ?: return SBDEhicRecognizer.recognize(imageFileUri, ScanbotSDKPluginResultDelegate(call)) } @PluginMethod fun recognizeGenericDocument(call: PluginCall) { SBDGenericDocRecognizer.recognize(call.data, ScanbotSDKPluginResultDelegate(call)) } @PluginMethod fun refreshImageUris(call: PluginCall) { SBDLegacyPage.refreshImageUris(call.data, ScanbotSDKPluginResultDelegate(call)) } @PluginMethod fun getOCRConfigs(call: PluginCall) { SBDOcr.getConfigs(ScanbotSDKPluginResultDelegate(call)) } @PluginMethod fun cleanup(call: PluginCall) { SBDSetupOperations.cleanup(ScanbotSDKPluginResultDelegate(call)) } @PluginMethod fun performOCR(call: PluginCall) { SBDOcr.perform(call.data, ScanbotSDKPluginResultDelegate(call)) } @PluginMethod fun createPDF(call: PluginCall) { SBDPdf.create(call.data, ScanbotSDKPluginResultDelegate(call)) } @PluginMethod fun writeTIFF(call: PluginCall) { SBDTiff.write(call.data, ScanbotSDKPluginResultDelegate(call)) } //region DocumentAPIs @PluginMethod fun createDocument(call: PluginCall) { SBDDocument.create(call.data, ScanbotSDKPluginResultDelegate(call)) } @PluginMethod fun createDocumentFromLegacyPages(call: PluginCall) { SBDDocument.createFromLegacyPages(call.data, ScanbotSDKPluginResultDelegate(call)) } @PluginMethod fun createDocumentFromPDF(call: PluginCall) { val pdfUri = call.asStringOrReject("pdfUri") ?: return SBDDocument.createFromPDF(pdfUri, ScanbotSDKPluginResultDelegate(call)) } @PluginMethod fun documentExists(call: PluginCall) { val documentID = call.asStringOrReject("documentID") ?: return SBDDocument.exists(documentID, ScanbotSDKPluginResultDelegate(call)) } @PluginMethod fun loadDocument(call: PluginCall) { val documentID = call.asStringOrReject("documentID") ?: return SBDDocument.load(documentID, ScanbotSDKPluginResultDelegate(call)) } @PluginMethod fun storedDocumentIDs(call: PluginCall) { SBDDocument.storedIDs(ScanbotSDKPluginResultDelegate(call)) } @PluginMethod fun cloneDocument(call: PluginCall) { val documentID = call.asStringOrReject("documentID") ?: return SBDDocument.clone(documentID, ScanbotSDKPluginResultDelegate(call)) } @PluginMethod fun deleteDocument(call: PluginCall) { val documentID = call.asStringOrReject("documentID") ?: return SBDDocument.delete(documentID, ScanbotSDKPluginResultDelegate(call)) } @PluginMethod fun deleteAllDocuments(call: PluginCall) { SBDDocument.deleteAll(ScanbotSDKPluginResultDelegate(call)) } @PluginMethod fun createPDFForDocument(call: PluginCall) { SBDPdf.createFromDocument(call.data, ScanbotSDKPluginResultDelegate(call)) } @PluginMethod fun createTIFF(call: PluginCall) { SBDTiff.createFromDocument(call.data, ScanbotSDKPluginResultDelegate(call)) } @PluginMethod fun addPage(call: PluginCall) { SBDDocument.addPage(call.data, ScanbotSDKPluginResultDelegate(call)) } @PluginMethod fun movePage(call: PluginCall) { SBDDocument.movePage(call.data, ScanbotSDKPluginResultDelegate(call)) } @PluginMethod fun modifyPage(call: PluginCall) { SBDDocument.modifyPage(call.data, ScanbotSDKPluginResultDelegate(call)) } @PluginMethod fun removePageFromDocument(call: PluginCall) { SBDDocument.removePage(call.data, ScanbotSDKPluginResultDelegate(call)) } @PluginMethod fun removeAllPages(call: PluginCall) { val documentID = call.asStringOrReject("documentID") ?: return SBDDocument.removeAllPages(documentID, ScanbotSDKPluginResultDelegate(call)) } //endregion DocumentAPIs //region ----RTU UI---- @PluginMethod fun startDocumentScanner(call: PluginCall) { SBDRtuUi.startLegacyDocumentScanner( activity, call.data, ScanbotSDKPluginResultDelegate(call) ) } @PluginMethod fun closeDocumentScanner(call: PluginCall) { SBDRtuUi.closeLegacyDocumentScanner(activity, ScanbotSDKPluginResultDelegate(call)) } @PluginMethod fun startFinderDocumentScanner(call: PluginCall) { SBDRtuUi.startLegacyFinderDocumentScanner( activity, call.data, ScanbotSDKPluginResultDelegate(call) ) } @PluginMethod fun closeFinderDocumentScanner(call: PluginCall) { SBDRtuUi.closeLegacyFinderDocumentScanner(activity, ScanbotSDKPluginResultDelegate(call)) } @PluginMethod fun startCroppingScreen(call: PluginCall) { val page = call.asJsonOrReject("page") ?: return val config = call.asJsonOrReject("configuration") ?: return SBDRtuUi.startLegacyCroppingScreen( activity, page, config, ScanbotSDKPluginResultDelegate(call) ) } @PluginMethod fun closeCroppingScreen(call: PluginCall) { SBDRtuUi.closeLegacyCroppingScreen(activity, ScanbotSDKPluginResultDelegate(call)) } @PluginMethod fun startMrzScanner(call: PluginCall) { SBDRtuUi.startMrzScanner(activity, call.data, ScanbotSDKPluginResultDelegate(call)) } @PluginMethod fun closeMrzScanner(call: PluginCall) { SBDRtuUi.closeMrzScanner(activity, ScanbotSDKPluginResultDelegate(call)) } @PluginMethod fun startBarcodeScanner(call: PluginCall) { SBDRtuUi.startLegacyBarcodeScanner( activity, call.data, ScanbotSDKPluginResultDelegate(call) ) } @PluginMethod fun closeBarcodeScanner(call: PluginCall) { SBDRtuUi.closeLegacyBarcodeScanner(activity, ScanbotSDKPluginResultDelegate(call)) } @PluginMethod fun startBatchBarcodeScanner(call: PluginCall) { SBDRtuUi.startLegacyBatchBarcodeScanner( activity, call.data, ScanbotSDKPluginResultDelegate(call) ) } @PluginMethod fun closeBatchBarcodeScanner(call: PluginCall) { SBDRtuUi.closeLegacyBatchBarcodeScanner(activity, ScanbotSDKPluginResultDelegate(call)) } @PluginMethod fun startTextDataScanner(call: PluginCall) { SBDRtuUi.startTextDataScanner( activity, call.data, ScanbotSDKPluginResultDelegate(call) ) } @PluginMethod fun closeTextDataScanner(call: PluginCall) { SBDRtuUi.closeTextDataScanner(activity, ScanbotSDKPluginResultDelegate(call)) } @PluginMethod fun startGenericDocumentRecognizer(call: PluginCall) { SBDRtuUi.startGenericDocRecognizer( activity, call.data, ScanbotSDKPluginResultDelegate(call) ) } @PluginMethod fun closeGenericDocumentRecognizer(call: PluginCall) { SBDRtuUi.closeGenericDocRecognizer(activity, ScanbotSDKPluginResultDelegate(call)) } @PluginMethod fun startEHICScanner(call: PluginCall) { SBDRtuUi.startEhicScanner( activity, call.data, ScanbotSDKPluginResultDelegate(call) ) } @PluginMethod fun closeEHICScanner(call: PluginCall) { SBDRtuUi.closeEhicScanner(activity, ScanbotSDKPluginResultDelegate(call)) } @PluginMethod fun startLicensePlateScanner(call: PluginCall) { SBDRtuUi.startLicensePlateScanner( activity, call.data, ScanbotSDKPluginResultDelegate(call) ) } @PluginMethod fun closeLicensePlateScanner(call: PluginCall) { SBDRtuUi.closeLicensePlateScanner(activity, ScanbotSDKPluginResultDelegate(call)) } @PluginMethod fun startMedicalCertificateRecognizer(call: PluginCall) { SBDRtuUi.startMedicalCertRecognizer( activity, call.data, ScanbotSDKPluginResultDelegate(call) ) } @PluginMethod fun closeMedicalCertificateRecognizer(call: PluginCall) { SBDRtuUi.closeMedicalCertRecognizer(activity, ScanbotSDKPluginResultDelegate(call)) } @PluginMethod fun startCheckRecognizer(call: PluginCall) { SBDRtuUi.startCheckRecognizer( activity, call.data, ScanbotSDKPluginResultDelegate(call) ) } @PluginMethod fun closeCheckRecognizer(call: PluginCall) { SBDRtuUi.closeCheckRecognizer(activity, ScanbotSDKPluginResultDelegate(call)) } @PluginMethod fun startVinScanner(call: PluginCall) { SBDRtuUi.startVinScanner( activity, call.data, ScanbotSDKPluginResultDelegate(call) ) } @PluginMethod fun closeVinScanner(call: PluginCall) { SBDRtuUi.closeVinScanner(activity, ScanbotSDKPluginResultDelegate(call)) } //endregion ----RTU UI---- //region RTU UI V2 @PluginMethod fun startDocumentScannerV2(call: PluginCall) { SBDRtuUi.startDocumentScanner( activity, call.data, ScanbotSDKPluginResultDelegate(call) ) } @PluginMethod fun startCroppingScreenV2(call: PluginCall) { SBDRtuUi.startCroppingScreen( activity, call.data, ScanbotSDKPluginResultDelegate(call) ) } @PluginMethod fun startBarcodeScannerV2(pluginCall: PluginCall) { if (barcodeItemMapperPluginCall != null) { SBDRtuUi.startBarcodeScanner( activity, pluginCall.data, ScanbotSDKPluginResultDelegate(pluginCall) ) { barcodeItem -> barcodeItemMapperPluginCall?.resolve(JSObject.fromJSONObject(barcodeItem)) ?: run { // This code should never be executed, but just in case SBDRtuUi.onBarcodeItemMapper(barcodeItemUuid(barcodeItem)) } } } else { SBDRtuUi.startBarcodeScanner( activity, pluginCall.data, ScanbotSDKPluginResultDelegate(pluginCall) ) } } //endregion RTU UI V2 //region Helper methods that are used only by us from the plugin middle layer (not exposed to the users) @PluginMethod(returnType = PluginMethod.RETURN_NONE) fun registerBarcodeItemMapperCallback(pluginCall: PluginCall) { barcodeItemMapperPluginCall = pluginCall.apply { setKeepAlive(true) } } @PluginMethod(returnType = PluginMethod.RETURN_NONE) fun onBarcodeItemMapper(pluginCall: PluginCall) { val barcodeMappedData = pluginCall.getObject("barcodeMappedData") SBDRtuUi.onBarcodeItemMapper( pluginCall.getString("barcodeItemUuid")!!, barcodeMappedData ) } //endregion Helper methods that are used only by us from the plugin middle layer (not exposed to the users) //region Private helper methods private fun barcodeItemUuid(barcodeItem: JSONObject): String { val textWithExt = barcodeItem.getString("textWithExtension") val type = barcodeItem.getStringOrDefault("type") ?: "" return "${textWithExt}_$type" } //endregion Private helper methods @Suppress("OVERRIDE_DEPRECATION", "DEPRECATION") override fun handleOnActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { val handledByWrapper = SBDRtuUi.onActivityResult(requestCode, resultCode, data) if (handledByWrapper) { if (requestCode == SBRtuUiRequestCodes.BARCODE_SCANNER) { barcodeItemMapperPluginCall?.release(bridge) barcodeItemMapperPluginCall = null } } else { super.handleOnActivityResult(requestCode, resultCode, data) } } }