feat: Implement enableZoomGesture on Android

This commit is contained in:
Marc Rousavy 2023-08-23 15:39:24 +02:00
parent 86468e3e1f
commit efe6556fc2
2 changed files with 34 additions and 6 deletions

View File

@ -8,6 +8,7 @@ import android.content.res.Configuration
import android.hardware.camera2.CameraManager import android.hardware.camera2.CameraManager
import android.util.Log import android.util.Log
import android.util.Size import android.util.Size
import android.view.ScaleGestureDetector
import android.view.Surface import android.view.Surface
import android.view.View import android.view.View
import android.widget.FrameLayout import android.widget.FrameLayout
@ -16,8 +17,8 @@ import com.facebook.react.bridge.ReadableMap
import com.mrousavy.camera.extensions.containsAny import com.mrousavy.camera.extensions.containsAny
import com.mrousavy.camera.extensions.installHierarchyFitter import com.mrousavy.camera.extensions.installHierarchyFitter
import com.mrousavy.camera.frameprocessor.FrameProcessor import com.mrousavy.camera.frameprocessor.FrameProcessor
import com.mrousavy.camera.parsers.PixelFormat
import com.mrousavy.camera.parsers.Orientation import com.mrousavy.camera.parsers.Orientation
import com.mrousavy.camera.parsers.PixelFormat
import com.mrousavy.camera.parsers.PreviewType import com.mrousavy.camera.parsers.PreviewType
import com.mrousavy.camera.parsers.Torch import com.mrousavy.camera.parsers.Torch
import com.mrousavy.camera.parsers.VideoStabilizationMode import com.mrousavy.camera.parsers.VideoStabilizationMode
@ -80,6 +81,7 @@ class CameraView(context: Context) : FrameLayout(context) {
var torch: Torch = Torch.OFF var torch: Torch = Torch.OFF
var zoom: Float = 1f // in "factor" var zoom: Float = 1f // in "factor"
var orientation: Orientation? = null var orientation: Orientation? = null
var enableZoomGesture: Boolean = false
// private properties // private properties
private var isMounted = false private var isMounted = false
@ -102,9 +104,6 @@ class CameraView(context: Context) : FrameLayout(context) {
internal val outputOrientation: Orientation internal val outputOrientation: Orientation
get() = orientation ?: inputOrientation get() = orientation ?: inputOrientation
private var minZoom: Float = 1f
private var maxZoom: Float = 1f
init { init {
this.installHierarchyFitter() this.installHierarchyFitter()
setupPreviewView() setupPreviewView()
@ -167,10 +166,11 @@ class CameraView(context: Context) : FrameLayout(context) {
val shouldReconfigurePreview = changedProps.containsAny(propsThatRequirePreviewReconfiguration) val shouldReconfigurePreview = changedProps.containsAny(propsThatRequirePreviewReconfiguration)
val shouldReconfigureSession = shouldReconfigurePreview || changedProps.containsAny(propsThatRequireSessionReconfiguration) val shouldReconfigureSession = shouldReconfigurePreview || changedProps.containsAny(propsThatRequireSessionReconfiguration)
val shouldReconfigureFormat = shouldReconfigureSession || changedProps.containsAny(propsThatRequireFormatReconfiguration) val shouldReconfigureFormat = shouldReconfigureSession || changedProps.containsAny(propsThatRequireFormatReconfiguration)
val shouldReconfigureZoom = /* TODO: When should we reconfigure this? */ shouldReconfigureSession || changedProps.contains("zoom") val shouldReconfigureZoom = shouldReconfigureSession || changedProps.contains("zoom")
val shouldReconfigureTorch = /* TODO: When should we reconfigure this? */ shouldReconfigureSession || changedProps.contains("torch") val shouldReconfigureTorch = shouldReconfigureSession || changedProps.contains("torch")
val shouldUpdateOrientation = /* TODO: When should we reconfigure this? */ shouldReconfigureSession || changedProps.contains("orientation") val shouldUpdateOrientation = /* TODO: When should we reconfigure this? */ shouldReconfigureSession || changedProps.contains("orientation")
val shouldCheckActive = shouldReconfigureFormat || changedProps.contains("isActive") val shouldCheckActive = shouldReconfigureFormat || changedProps.contains("isActive")
val shouldReconfigureZoomGesture = changedProps.contains("enableZoomGesture")
if (shouldReconfigurePreview) { if (shouldReconfigurePreview) {
setupPreviewView() setupPreviewView()
@ -194,6 +194,9 @@ class CameraView(context: Context) : FrameLayout(context) {
if (shouldUpdateOrientation) { if (shouldUpdateOrientation) {
// TODO: updateOrientation() // TODO: updateOrientation()
} }
if (shouldReconfigureZoomGesture) {
updateZoomGesture()
}
} catch (e: Throwable) { } catch (e: Throwable) {
Log.e(TAG, "update() threw: ${e.message}") Log.e(TAG, "update() threw: ${e.message}")
invokeOnError(e) invokeOnError(e)
@ -249,4 +252,22 @@ class CameraView(context: Context) : FrameLayout(context) {
cameraSession.setTorchMode(torch == Torch.ON) cameraSession.setTorchMode(torch == Torch.ON)
} }
} }
@SuppressLint("ClickableViewAccessibility")
private fun updateZoomGesture() {
if (enableZoomGesture) {
val scaleGestureDetector = ScaleGestureDetector(context, object: ScaleGestureDetector.SimpleOnScaleGestureListener() {
override fun onScale(detector: ScaleGestureDetector): Boolean {
zoom *= detector.scaleFactor
cameraSession.setZoom(zoom)
return true
}
})
setOnTouchListener { _, event ->
scaleGestureDetector.onTouchEvent(event)
}
} else {
setOnTouchListener(null)
}
}
} }

View File

@ -87,6 +87,13 @@ class CameraViewManager : ViewGroupManager<CameraView>() {
view.enableDepthData = enableDepthData view.enableDepthData = enableDepthData
} }
@ReactProp(name = "enableZoomGesture")
fun setEnableZoomGesture(view: CameraView, enableZoomGesture: Boolean) {
if (view.enableZoomGesture != enableZoomGesture)
addChangedPropToTransaction(view, "enableZoomGesture")
view.enableZoomGesture = enableZoomGesture
}
@ReactProp(name = "videoStabilizationMode") @ReactProp(name = "videoStabilizationMode")
fun setVideoStabilizationMode(view: CameraView, videoStabilizationMode: String?) { fun setVideoStabilizationMode(view: CameraView, videoStabilizationMode: String?) {
val newMode = VideoStabilizationMode.fromUnionValue(videoStabilizationMode) val newMode = VideoStabilizationMode.fromUnionValue(videoStabilizationMode)