Skip to content

Commit

Permalink
fix: Fix focus not running on UI Thread
Browse files Browse the repository at this point in the history
  • Loading branch information
mrousavy committed Mar 1, 2024
1 parent c741c90 commit e4a5fe3
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package com.mrousavy.camera

import com.facebook.react.bridge.ReadableMap
import com.mrousavy.camera.utils.runOnUiThreadAndWait

suspend fun CameraView.focus(pointMap: ReadableMap) {
val x = pointMap.getDouble("x")
val y = pointMap.getDouble("y")

val point = previewView.meteringPointFactory.createPoint(x.toFloat(), y.toFloat())
val point = runOnUiThreadAndWait {
previewView.meteringPointFactory.createPoint(x.toFloat(), y.toFloat())
}
cameraSession.focus(point)
}
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,7 @@ class CameraSession(private val context: Context, private val callback: Callback
recording.resume()
}

@SuppressLint("RestrictedApi")
suspend fun focus(meteringPoint: MeteringPoint) {
val camera = camera ?: throw CameraNotReadyError()

Expand All @@ -517,8 +518,14 @@ class CameraSession(private val context: Context, private val callback: Callback
}

try {
Log.i(TAG, "Focusing to ${action.meteringPointsAf.joinToString { "(${it.x}, ${it.y})" }}...")
val future = camera.cameraControl.startFocusAndMetering(action)
future.await(CameraQueues.cameraExecutor)
val result = future.await(CameraQueues.cameraExecutor)
if (result.isFocusSuccessful) {
Log.i(TAG, "Focused successfully!")
} else {
Log.i(TAG, "Focus failed.")
}
} catch (e: CameraControl.OperationCanceledException) {
throw FocusCanceledError()
}
Expand Down

0 comments on commit e4a5fe3

Please sign in to comment.