package com.dynamsoft.reactnativelib.basicutils import com.dynamsoft.core.basic_structures.CapturedResultItem import com.dynamsoft.dlr.CharacterResult import com.dynamsoft.dlr.RecognizedTextLinesResult import com.dynamsoft.dlr.SimplifiedLabelRecognizerSettings import com.dynamsoft.dlr.TextLineResultItem import com.facebook.react.bridge.Arguments import com.facebook.react.bridge.ReadableMap import com.facebook.react.bridge.WritableArray import com.facebook.react.bridge.WritableMap fun Array.toWitableArray() :WritableArray = Arguments.createArray().apply { forEach { pushMap(it.toWritableMap()) } } fun CharacterResult.toWritableMap() : WritableMap = Arguments.createMap().apply { putString("characterH",characterH.toString()) putString("characterM",characterM.toString()) putString("characterL",characterL.toString()) putInt("characterHConfidence",characterHConfidence) putInt("characterMConfidence",characterMConfidence) putInt("characterLConfidence",characterLConfidence) putMap("location", location.toWritableMap()) } fun TextLineResultItem.toWritableMap(): WritableMap = Arguments.createMap().apply { merge((this@toWritableMap as CapturedResultItem).toSharedWritableMap()) putMap("location", location.toWritableMap()) putArray("characterResults", characterResults.toWitableArray()) putString("text", text) putString("rawText", rawText) putString("specificationName", specificationName) putInt("confidence", confidence) } fun Array.toWritableArray(): WritableArray = Arguments.createArray().apply { forEach { pushMap(it.toWritableMap()) } } fun RecognizedTextLinesResult.toWritableMap(): WritableMap = Arguments.createMap().apply { putInt("errorCode", errorCode) putString("originalImageHashId", originalImageHashId) putString("errorMessage", errorMessage) putArray("items", items.toWritableArray()) putArray("rotationTransformMatrix",FloatArray(9).apply { rotationTransformMatrix.getValues(this) }.toWritableArray()) } fun SimplifiedLabelRecognizerSettings.toWritableMap(): WritableMap = Arguments.createMap().apply { putArray("grayscaleTransformationModes", grayscaleTransformationModes.toWritableArray()) putArray("grayscaleEnhancementModes", grayscaleEnhancementModes.toWritableArray()) putString("characterModelName", characterModelName) putString("lineStringRegExPattern", lineStringRegExPattern) putInt("scaleDownThreshold", scaleDownThreshold) putInt("maxThreadsInOneTask", maxThreadsInOneTask) } fun SimplifiedLabelRecognizerSettings.updateFromReadableMap(newSettings: ReadableMap) { characterModelName = if (newSettings.hasKey("characterModelName")) newSettings.getString("characterModelName") else characterModelName lineStringRegExPattern = if (newSettings.hasKey("lineStringRegExPattern")) newSettings.getString("lineStringRegExPattern") else lineStringRegExPattern scaleDownThreshold = if (newSettings.hasKey("scaleDownThreshold")) newSettings.getInt("scaleDownThreshold") else scaleDownThreshold grayscaleTransformationModes = if (newSettings.hasKey("grayscaleTransformationModes")) newSettings.getArray("grayscaleTransformationModes")?.toIntArray() else grayscaleTransformationModes grayscaleEnhancementModes = if (newSettings.hasKey("grayscaleEnhancementModes")) newSettings.getArray("grayscaleEnhancementModes")?.toIntArray() else grayscaleEnhancementModes maxThreadsInOneTask = if (newSettings.hasKey("maxThreadsInOneTask")) newSettings.getInt("maxThreadsInOneTask") else maxThreadsInOneTask }