package com.dynamsoft.reactnativelib.basicutils import android.content.Context import android.graphics.Point import android.graphics.Rect import android.graphics.drawable.Drawable import androidx.appcompat.content.res.AppCompatResources import com.dynamsoft.core.basic_structures.DSRect import com.dynamsoft.dce.CameraView import com.facebook.react.bridge.Arguments import com.facebook.react.bridge.ReadableMap import com.facebook.react.bridge.ReadableType import com.facebook.react.bridge.WritableMap fun ReadableMap.toRect() = Rect().apply { left = if (hasKey("x")) getInt("x") else left top = if (hasKey("y")) getInt("y") else top right = (if (hasKey("width")) getInt("width") else 0) + left bottom = (if (hasKey("height")) getInt("height") else 0) + top } fun ReadableMap.toScanRegion() = DSRect().apply { top = if (hasKey("top")) getDouble("top").toFloat() else top bottom = if (hasKey("bottom")) getDouble("bottom").toFloat() else bottom left = if (hasKey("left")) getDouble("left").toFloat() else left right = if (hasKey("right")) getDouble("right").toFloat() else right if (hasKey("measuredInPercentage")) { when (getType("measuredInPercentage")) { ReadableType.Number -> measuredInPercentage = getInt("measuredInPercentage") != 0 ReadableType.Boolean -> measuredInPercentage = getBoolean("measuredInPercentage") else -> {/*keep*/ } } } } fun Rect.toWritableMap(): WritableMap = Arguments.createMap().apply { top.let { putInt("y", it) } left.let { putInt("x", it) } width().let { putInt("width", it) } height().let { putInt("height", it) } } fun DSRect.toWritableMap(): WritableMap = Arguments.createMap().apply { putDouble("top", this@toWritableMap.top.toDouble()) putDouble("bottom", this@toWritableMap.bottom.toDouble()) putDouble("left", this@toWritableMap.left.toDouble()) putDouble("right", this@toWritableMap.right.toDouble()) putBoolean("measuredInPercentage", this@toWritableMap.measuredInPercentage) } fun ReadableMap.toTorchButtonState(): CameraViewButtonState { val rect = if (hasKey("location")) getMap("location")!!.toRect() else Rect(25,100,70,145) val visible = if (hasKey("visible")) getBoolean("visible") else null val torchOnImageBase64 = if (hasKey("torchOnImageBase64")) getString("torchOnImageBase64") else null val torchOffImageBase64 = if (hasKey("torchOffImageBase64")) getString("torchOffImageBase64") else null return CameraViewButtonState(rect, visible, torchOnImageBase64, torchOffImageBase64) } fun ReadableMap.toCameraToggleButtonState(): CameraViewButtonState { val rect = if (hasKey("location")) getMap("location")!!.toRect() else Rect(25,100,70,145) val visible = if (hasKey("visible")) getBoolean("visible") else false val cameraImageBase64 = if (hasKey("cameraToggleImageBase64")) getString("cameraToggleImageBase64") else null return CameraViewButtonState(rect, visible, cameraBase64 = cameraImageBase64) } fun CameraView.setTorchButtonState(torchButtonState: CameraViewButtonState) { setTorchButton( torchButtonState.getLeftTopPoint(), torchButtonState.getWidth(), torchButtonState.getHeight(), torchButtonState.getTouchOnDrawable(context), torchButtonState.getTouchOffDrawable(context) ) torchButtonState.visible?.apply { torchButtonVisible = this } } fun CameraView.setCameraToggleButtonState(cameraToggleButtonState: CameraViewButtonState) { setCameraToggleButton( cameraToggleButtonState.getLeftTopPoint(), cameraToggleButtonState.getWidth(), cameraToggleButtonState.getHeight(), cameraToggleButtonState.getCameraToggleDrawable(context) ) } class CameraViewButtonState( val location: Rect, val visible: Boolean? = null, val touchOnBase64: String? = null, val touchOffBase64: String? = null, val cameraBase64: String? = null, ) { fun getLeftTopPoint() = Point(location.left, location.top) fun getWidth() = location.width() fun getHeight() = location.height() fun getTouchOnDrawable(context: Context): Drawable { return base64ToDrawable(touchOnBase64, context.resources!!) ?: AppCompatResources.getDrawable( context, com.dynamsoft.R.drawable.icon_flash_on )!! } fun getTouchOffDrawable(context: Context): Drawable { return base64ToDrawable(touchOffBase64, context.resources!!) ?: AppCompatResources.getDrawable( context, com.dynamsoft.R.drawable.icon_flash_off )!! } fun getCameraToggleDrawable(context: Context): Drawable { return base64ToDrawable(cameraBase64, context.resources!!) ?: AppCompatResources.getDrawable(context, com.dynamsoft.R.drawable.icon_turn_camera)!! } override fun toString(): String { return "CameraViewButtonState(location=$location, visible=$visible, touchOnBase64=$touchOnBase64, touchOffBase64=$touchOffBase64)" } }