diff --git a/package/android/src/main/java/com/mrousavy/camera/CameraView.kt b/package/android/src/main/java/com/mrousavy/camera/CameraView.kt index 70e6fc6ca6..bae66f7513 100644 --- a/package/android/src/main/java/com/mrousavy/camera/CameraView.kt +++ b/package/android/src/main/java/com/mrousavy/camera/CameraView.kt @@ -20,10 +20,12 @@ import com.mrousavy.camera.types.CameraDeviceFormat import com.mrousavy.camera.types.CodeScannerOptions import com.mrousavy.camera.types.Orientation import com.mrousavy.camera.types.PixelFormat +import com.mrousavy.camera.types.PreviewViewType import com.mrousavy.camera.types.QualityBalance import com.mrousavy.camera.types.ResizeMode import com.mrousavy.camera.types.Torch import com.mrousavy.camera.types.VideoStabilizationMode +import com.mrousavy.camera.utils.runOnUiThread import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch @@ -74,6 +76,11 @@ class CameraView(context: Context) : var zoom: Float = 1f // in "factor" var exposure: Double = 1.0 var orientation: Orientation = Orientation.PORTRAIT + var androidPreviewViewType: PreviewViewType = PreviewViewType.SURFACE_VIEW + set(value) { + field = value + updatePreviewType() + } var enableZoomGesture = false set(value) { field = value @@ -102,7 +109,7 @@ class CameraView(context: Context) : internal val cameraSession: CameraSession internal var frameProcessor: FrameProcessor? = null internal val previewView: PreviewView - private var previewSurfaceProvider: SurfaceProvider + private val previewSurfaceProvider: SurfaceProvider private var currentConfigureCall: Long = System.currentTimeMillis() // other @@ -255,6 +262,13 @@ class CameraView(context: Context) : } } + private fun updatePreviewType() { + runOnUiThread { + previewView.implementationMode = androidPreviewViewType.toPreviewImplementationMode() + update() + } + } + override fun onFrame(frame: Frame) { frameProcessor?.call(frame) diff --git a/package/android/src/main/java/com/mrousavy/camera/CameraViewManager.kt b/package/android/src/main/java/com/mrousavy/camera/CameraViewManager.kt index de08993131..b4c322fcb7 100644 --- a/package/android/src/main/java/com/mrousavy/camera/CameraViewManager.kt +++ b/package/android/src/main/java/com/mrousavy/camera/CameraViewManager.kt @@ -9,6 +9,7 @@ import com.mrousavy.camera.types.CameraDeviceFormat import com.mrousavy.camera.types.CodeScannerOptions import com.mrousavy.camera.types.Orientation import com.mrousavy.camera.types.PixelFormat +import com.mrousavy.camera.types.PreviewViewType import com.mrousavy.camera.types.QualityBalance import com.mrousavy.camera.types.ResizeMode import com.mrousavy.camera.types.Torch @@ -134,6 +135,16 @@ class CameraViewManager : ViewGroupManager() { } } + @ReactProp(name = "androidPreviewViewType") + fun setAndroidPreviewViewType(view: CameraView, androidPreviewViewType: String?) { + if (androidPreviewViewType != null) { + val newMode = PreviewViewType.fromUnionValue(androidPreviewViewType) + view.androidPreviewViewType = newMode + } else { + view.androidPreviewViewType = PreviewViewType.SURFACE_VIEW + } + } + // TODO: Change when TurboModules release. // We're treating -1 as "null" here, because when I make the fps parameter // of type "Int?" the react bridge throws an error. diff --git a/package/android/src/main/java/com/mrousavy/camera/types/PreviewViewType.kt b/package/android/src/main/java/com/mrousavy/camera/types/PreviewViewType.kt new file mode 100644 index 0000000000..9812faf0ce --- /dev/null +++ b/package/android/src/main/java/com/mrousavy/camera/types/PreviewViewType.kt @@ -0,0 +1,24 @@ +package com.mrousavy.camera.types + +import androidx.camera.view.PreviewView +import com.mrousavy.camera.core.InvalidTypeScriptUnionError + +enum class PreviewViewType(override val unionValue: String) : JSUnionValue { + SURFACE_VIEW("surface-view"), + TEXTURE_VIEW("texture-view"); + + fun toPreviewImplementationMode(): PreviewView.ImplementationMode = + when (this) { + SURFACE_VIEW -> PreviewView.ImplementationMode.PERFORMANCE + TEXTURE_VIEW -> PreviewView.ImplementationMode.COMPATIBLE + } + + companion object : JSUnionValue.Companion { + override fun fromUnionValue(unionValue: String?): PreviewViewType = + when (unionValue) { + "surface-view" -> SURFACE_VIEW + "texture-view" -> TEXTURE_VIEW + else -> throw InvalidTypeScriptUnionError("androidPreviewViewType", unionValue) + } + } +}