Skip to content

Commit

Permalink
feat: Add androidPreviewViewType prop
Browse files Browse the repository at this point in the history
  • Loading branch information
mrousavy committed Feb 29, 2024
1 parent ec6026e commit dd56925
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 1 deletion.
16 changes: 15 additions & 1 deletion package/android/src/main/java/com/mrousavy/camera/CameraView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -134,6 +135,16 @@ class CameraViewManager : ViewGroupManager<CameraView>() {
}
}

@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.
Expand Down
Original file line number Diff line number Diff line change
@@ -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<PreviewViewType> {
override fun fromUnionValue(unionValue: String?): PreviewViewType =
when (unionValue) {
"surface-view" -> SURFACE_VIEW
"texture-view" -> TEXTURE_VIEW
else -> throw InvalidTypeScriptUnionError("androidPreviewViewType", unionValue)
}
}
}

0 comments on commit dd56925

Please sign in to comment.