2021-02-19 20:41:49 +01:00
|
|
|
package com.mrousavy.camera
|
2021-02-19 16:28:14 +01:00
|
|
|
|
|
|
|
import com.facebook.react.bridge.ReadableMap
|
|
|
|
import com.facebook.react.common.MapBuilder
|
|
|
|
import com.facebook.react.uimanager.ThemedReactContext
|
2023-08-21 12:50:14 +02:00
|
|
|
import com.facebook.react.uimanager.ViewGroupManager
|
2021-02-19 16:28:14 +01:00
|
|
|
import com.facebook.react.uimanager.annotations.ReactProp
|
2023-08-21 12:50:14 +02:00
|
|
|
import com.mrousavy.camera.parsers.Orientation
|
2023-08-21 14:43:12 +02:00
|
|
|
import com.mrousavy.camera.parsers.PixelFormat
|
2023-09-22 17:32:34 +02:00
|
|
|
import com.mrousavy.camera.parsers.ResizeMode
|
2023-08-21 12:50:14 +02:00
|
|
|
import com.mrousavy.camera.parsers.Torch
|
|
|
|
import com.mrousavy.camera.parsers.VideoStabilizationMode
|
2021-08-25 11:33:57 +02:00
|
|
|
|
|
|
|
@Suppress("unused")
|
2023-08-21 14:43:12 +02:00
|
|
|
class CameraViewManager : ViewGroupManager<CameraView>() {
|
2023-09-21 11:20:33 +02:00
|
|
|
public override fun createViewInstance(context: ThemedReactContext): CameraView = CameraView(context)
|
2021-08-25 11:33:57 +02:00
|
|
|
|
|
|
|
override fun onAfterUpdateTransaction(view: CameraView) {
|
|
|
|
super.onAfterUpdateTransaction(view)
|
|
|
|
val changedProps = cameraViewTransactions[view] ?: ArrayList()
|
|
|
|
view.update(changedProps)
|
|
|
|
cameraViewTransactions.remove(view)
|
|
|
|
}
|
|
|
|
|
2023-09-21 11:20:33 +02:00
|
|
|
override fun getExportedCustomDirectEventTypeConstants(): MutableMap<String, Any>? =
|
|
|
|
MapBuilder.builder<String, Any>()
|
2021-10-11 18:27:23 +02:00
|
|
|
.put("cameraViewReady", MapBuilder.of("registrationName", "onViewReady"))
|
2021-08-25 11:33:57 +02:00
|
|
|
.put("cameraInitialized", MapBuilder.of("registrationName", "onInitialized"))
|
|
|
|
.put("cameraError", MapBuilder.of("registrationName", "onError"))
|
|
|
|
.build()
|
|
|
|
|
2023-09-21 11:20:33 +02:00
|
|
|
override fun getName(): String = TAG
|
2021-02-26 10:56:20 +01:00
|
|
|
|
|
|
|
@ReactProp(name = "cameraId")
|
|
|
|
fun setCameraId(view: CameraView, cameraId: String) {
|
2023-09-21 11:20:33 +02:00
|
|
|
if (view.cameraId != cameraId) {
|
2021-02-26 10:56:20 +01:00
|
|
|
addChangedPropToTransaction(view, "cameraId")
|
2023-09-21 11:20:33 +02:00
|
|
|
}
|
2021-02-26 10:56:20 +01:00
|
|
|
view.cameraId = cameraId
|
|
|
|
}
|
|
|
|
|
2021-06-07 13:08:40 +02:00
|
|
|
@ReactProp(name = "photo")
|
|
|
|
fun setPhoto(view: CameraView, photo: Boolean?) {
|
2023-09-21 11:20:33 +02:00
|
|
|
if (view.photo != photo) {
|
2021-06-07 13:08:40 +02:00
|
|
|
addChangedPropToTransaction(view, "photo")
|
2023-09-21 11:20:33 +02:00
|
|
|
}
|
2021-06-07 13:08:40 +02:00
|
|
|
view.photo = photo
|
|
|
|
}
|
|
|
|
|
|
|
|
@ReactProp(name = "video")
|
|
|
|
fun setVideo(view: CameraView, video: Boolean?) {
|
2023-09-21 11:20:33 +02:00
|
|
|
if (view.video != video) {
|
2021-06-07 13:08:40 +02:00
|
|
|
addChangedPropToTransaction(view, "video")
|
2023-09-21 11:20:33 +02:00
|
|
|
}
|
2021-06-07 13:08:40 +02:00
|
|
|
view.video = video
|
|
|
|
}
|
|
|
|
|
|
|
|
@ReactProp(name = "audio")
|
|
|
|
fun setAudio(view: CameraView, audio: Boolean?) {
|
2023-09-21 11:20:33 +02:00
|
|
|
if (view.audio != audio) {
|
2021-06-07 13:08:40 +02:00
|
|
|
addChangedPropToTransaction(view, "audio")
|
2023-09-21 11:20:33 +02:00
|
|
|
}
|
2021-06-07 13:08:40 +02:00
|
|
|
view.audio = audio
|
|
|
|
}
|
|
|
|
|
2021-07-12 15:16:03 +02:00
|
|
|
@ReactProp(name = "enableFrameProcessor")
|
|
|
|
fun setEnableFrameProcessor(view: CameraView, enableFrameProcessor: Boolean) {
|
2023-09-21 11:20:33 +02:00
|
|
|
if (view.enableFrameProcessor != enableFrameProcessor) {
|
2021-07-12 15:16:03 +02:00
|
|
|
addChangedPropToTransaction(view, "enableFrameProcessor")
|
2023-09-21 11:20:33 +02:00
|
|
|
}
|
2021-07-12 15:16:03 +02:00
|
|
|
view.enableFrameProcessor = enableFrameProcessor
|
|
|
|
}
|
|
|
|
|
2023-08-21 12:50:14 +02:00
|
|
|
@ReactProp(name = "pixelFormat")
|
|
|
|
fun setPixelFormat(view: CameraView, pixelFormat: String?) {
|
|
|
|
val newPixelFormat = PixelFormat.fromUnionValue(pixelFormat)
|
2023-09-21 11:20:33 +02:00
|
|
|
if (view.pixelFormat != newPixelFormat) {
|
2023-08-21 12:50:14 +02:00
|
|
|
addChangedPropToTransaction(view, "pixelFormat")
|
2023-09-21 11:20:33 +02:00
|
|
|
}
|
2023-08-21 12:50:14 +02:00
|
|
|
view.pixelFormat = newPixelFormat ?: PixelFormat.NATIVE
|
|
|
|
}
|
|
|
|
|
2021-02-26 10:56:20 +01:00
|
|
|
@ReactProp(name = "enableDepthData")
|
|
|
|
fun setEnableDepthData(view: CameraView, enableDepthData: Boolean) {
|
2023-09-21 11:20:33 +02:00
|
|
|
if (view.enableDepthData != enableDepthData) {
|
2021-02-26 10:56:20 +01:00
|
|
|
addChangedPropToTransaction(view, "enableDepthData")
|
2023-09-21 11:20:33 +02:00
|
|
|
}
|
2021-02-26 10:56:20 +01:00
|
|
|
view.enableDepthData = enableDepthData
|
|
|
|
}
|
|
|
|
|
2023-08-23 15:39:24 +02:00
|
|
|
@ReactProp(name = "enableZoomGesture")
|
|
|
|
fun setEnableZoomGesture(view: CameraView, enableZoomGesture: Boolean) {
|
2023-09-21 11:20:33 +02:00
|
|
|
if (view.enableZoomGesture != enableZoomGesture) {
|
2023-08-23 15:39:24 +02:00
|
|
|
addChangedPropToTransaction(view, "enableZoomGesture")
|
2023-09-21 11:20:33 +02:00
|
|
|
}
|
2023-08-23 15:39:24 +02:00
|
|
|
view.enableZoomGesture = enableZoomGesture
|
|
|
|
}
|
|
|
|
|
2023-08-21 12:50:14 +02:00
|
|
|
@ReactProp(name = "videoStabilizationMode")
|
|
|
|
fun setVideoStabilizationMode(view: CameraView, videoStabilizationMode: String?) {
|
|
|
|
val newMode = VideoStabilizationMode.fromUnionValue(videoStabilizationMode)
|
2023-09-21 11:20:33 +02:00
|
|
|
if (view.videoStabilizationMode != newMode) {
|
2023-08-21 12:50:14 +02:00
|
|
|
addChangedPropToTransaction(view, "videoStabilizationMode")
|
2023-09-21 11:20:33 +02:00
|
|
|
}
|
2023-08-21 12:50:14 +02:00
|
|
|
view.videoStabilizationMode = newMode
|
|
|
|
}
|
|
|
|
|
2021-06-10 13:49:34 +02:00
|
|
|
@ReactProp(name = "enableHighQualityPhotos")
|
|
|
|
fun setEnableHighQualityPhotos(view: CameraView, enableHighQualityPhotos: Boolean?) {
|
2023-09-21 11:20:33 +02:00
|
|
|
if (view.enableHighQualityPhotos != enableHighQualityPhotos) {
|
2021-06-10 13:49:34 +02:00
|
|
|
addChangedPropToTransaction(view, "enableHighQualityPhotos")
|
2023-09-21 11:20:33 +02:00
|
|
|
}
|
2021-06-10 13:49:34 +02:00
|
|
|
view.enableHighQualityPhotos = enableHighQualityPhotos
|
2021-02-26 10:56:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@ReactProp(name = "enablePortraitEffectsMatteDelivery")
|
|
|
|
fun setEnablePortraitEffectsMatteDelivery(view: CameraView, enablePortraitEffectsMatteDelivery: Boolean) {
|
2023-09-21 11:20:33 +02:00
|
|
|
if (view.enablePortraitEffectsMatteDelivery != enablePortraitEffectsMatteDelivery) {
|
2021-02-26 10:56:20 +01:00
|
|
|
addChangedPropToTransaction(view, "enablePortraitEffectsMatteDelivery")
|
2023-09-21 11:20:33 +02:00
|
|
|
}
|
2021-02-26 10:56:20 +01:00
|
|
|
view.enablePortraitEffectsMatteDelivery = enablePortraitEffectsMatteDelivery
|
|
|
|
}
|
|
|
|
|
|
|
|
@ReactProp(name = "format")
|
|
|
|
fun setFormat(view: CameraView, format: ReadableMap?) {
|
2023-09-21 11:20:33 +02:00
|
|
|
if (view.format != format) {
|
2021-02-26 10:56:20 +01:00
|
|
|
addChangedPropToTransaction(view, "format")
|
2023-09-21 11:20:33 +02:00
|
|
|
}
|
2021-02-26 10:56:20 +01:00
|
|
|
view.format = format
|
|
|
|
}
|
|
|
|
|
2023-09-22 17:32:34 +02:00
|
|
|
@ReactProp(name = "resizeMode")
|
|
|
|
fun setResizeMode(view: CameraView, resizeMode: String) {
|
|
|
|
val newMode = ResizeMode.fromUnionValue(resizeMode)
|
|
|
|
if (view.resizeMode != newMode)
|
|
|
|
addChangedPropToTransaction(view, "resizeMode")
|
|
|
|
view.resizeMode = newMode
|
|
|
|
}
|
|
|
|
|
2021-08-25 11:33:57 +02:00
|
|
|
// TODO: Change when TurboModules release.
|
2021-02-26 10:56:20 +01:00
|
|
|
// We're treating -1 as "null" here, because when I make the fps parameter
|
|
|
|
// of type "Int?" the react bridge throws an error.
|
|
|
|
@ReactProp(name = "fps", defaultInt = -1)
|
|
|
|
fun setFps(view: CameraView, fps: Int) {
|
2023-09-21 11:20:33 +02:00
|
|
|
if (view.fps != fps) {
|
2021-02-26 10:56:20 +01:00
|
|
|
addChangedPropToTransaction(view, "fps")
|
2023-09-21 11:20:33 +02:00
|
|
|
}
|
2021-02-26 10:56:20 +01:00
|
|
|
view.fps = if (fps > 0) fps else null
|
|
|
|
}
|
|
|
|
|
|
|
|
@ReactProp(name = "hdr")
|
|
|
|
fun setHdr(view: CameraView, hdr: Boolean?) {
|
2023-09-21 11:20:33 +02:00
|
|
|
if (view.hdr != hdr) {
|
2021-02-26 10:56:20 +01:00
|
|
|
addChangedPropToTransaction(view, "hdr")
|
2023-09-21 11:20:33 +02:00
|
|
|
}
|
2021-02-26 10:56:20 +01:00
|
|
|
view.hdr = hdr
|
|
|
|
}
|
|
|
|
|
|
|
|
@ReactProp(name = "lowLightBoost")
|
|
|
|
fun setLowLightBoost(view: CameraView, lowLightBoost: Boolean?) {
|
2023-09-21 11:20:33 +02:00
|
|
|
if (view.lowLightBoost != lowLightBoost) {
|
2021-02-26 10:56:20 +01:00
|
|
|
addChangedPropToTransaction(view, "lowLightBoost")
|
2023-09-21 11:20:33 +02:00
|
|
|
}
|
2021-02-26 10:56:20 +01:00
|
|
|
view.lowLightBoost = lowLightBoost
|
|
|
|
}
|
|
|
|
|
|
|
|
@ReactProp(name = "isActive")
|
|
|
|
fun setIsActive(view: CameraView, isActive: Boolean) {
|
2023-09-21 11:20:33 +02:00
|
|
|
if (view.isActive != isActive) {
|
2021-02-26 10:56:20 +01:00
|
|
|
addChangedPropToTransaction(view, "isActive")
|
2023-09-21 11:20:33 +02:00
|
|
|
}
|
2021-02-26 10:56:20 +01:00
|
|
|
view.isActive = isActive
|
|
|
|
}
|
|
|
|
|
|
|
|
@ReactProp(name = "torch")
|
|
|
|
fun setTorch(view: CameraView, torch: String) {
|
2023-08-21 12:50:14 +02:00
|
|
|
val newMode = Torch.fromUnionValue(torch)
|
2023-09-21 11:20:33 +02:00
|
|
|
if (view.torch != newMode) {
|
2021-02-26 10:56:20 +01:00
|
|
|
addChangedPropToTransaction(view, "torch")
|
2023-09-21 11:20:33 +02:00
|
|
|
}
|
2023-08-21 12:50:14 +02:00
|
|
|
view.torch = newMode
|
2021-02-26 10:56:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@ReactProp(name = "zoom")
|
|
|
|
fun setZoom(view: CameraView, zoom: Double) {
|
2021-07-29 11:44:22 +02:00
|
|
|
val zoomFloat = zoom.toFloat()
|
2023-09-21 11:20:33 +02:00
|
|
|
if (view.zoom != zoomFloat) {
|
2021-02-26 10:56:20 +01:00
|
|
|
addChangedPropToTransaction(view, "zoom")
|
2023-09-21 11:20:33 +02:00
|
|
|
}
|
2021-07-29 11:44:22 +02:00
|
|
|
view.zoom = zoomFloat
|
2021-02-26 10:56:20 +01:00
|
|
|
}
|
|
|
|
|
2022-01-04 16:57:40 +01:00
|
|
|
@ReactProp(name = "orientation")
|
2023-08-21 12:50:14 +02:00
|
|
|
fun setOrientation(view: CameraView, orientation: String?) {
|
|
|
|
val newMode = Orientation.fromUnionValue(orientation)
|
2023-09-21 11:20:33 +02:00
|
|
|
if (view.orientation != newMode) {
|
2022-01-04 16:57:40 +01:00
|
|
|
addChangedPropToTransaction(view, "orientation")
|
2023-09-21 11:20:33 +02:00
|
|
|
}
|
2023-08-21 12:50:14 +02:00
|
|
|
view.orientation = newMode
|
2022-01-04 16:57:40 +01:00
|
|
|
}
|
|
|
|
|
2021-02-26 10:56:20 +01:00
|
|
|
companion object {
|
2021-07-07 15:00:32 +02:00
|
|
|
const val TAG = "CameraView"
|
2021-02-26 10:56:20 +01:00
|
|
|
|
|
|
|
val cameraViewTransactions: HashMap<CameraView, ArrayList<String>> = HashMap()
|
2021-08-25 11:33:57 +02:00
|
|
|
|
|
|
|
private fun addChangedPropToTransaction(view: CameraView, changedProp: String) {
|
|
|
|
if (cameraViewTransactions[view] == null) {
|
|
|
|
cameraViewTransactions[view] = ArrayList()
|
|
|
|
}
|
|
|
|
cameraViewTransactions[view]!!.add(changedProp)
|
|
|
|
}
|
2021-02-26 10:56:20 +01:00
|
|
|
}
|
2021-02-19 16:28:14 +01:00
|
|
|
}
|