2021-02-19 12:41:49 -07:00
|
|
|
package com.mrousavy.camera
|
2021-02-19 08:28:14 -07:00
|
|
|
|
|
|
|
import androidx.camera.core.FocusMeteringAction
|
|
|
|
import com.facebook.react.bridge.ReadableMap
|
|
|
|
import kotlinx.coroutines.guava.await
|
2022-03-31 10:01:21 -06:00
|
|
|
import kotlinx.coroutines.withContext
|
2021-02-19 08:28:14 -07:00
|
|
|
import java.util.concurrent.TimeUnit
|
|
|
|
|
|
|
|
suspend fun CameraView.focus(pointMap: ReadableMap) {
|
2021-02-26 02:56:20 -07:00
|
|
|
val cameraControl = camera?.cameraControl ?: throw CameraNotReadyError()
|
|
|
|
if (!pointMap.hasKey("x") || !pointMap.hasKey("y")) {
|
|
|
|
throw InvalidTypeScriptUnionError("point", pointMap.toString())
|
|
|
|
}
|
2021-02-19 08:28:14 -07:00
|
|
|
|
2021-02-26 02:56:20 -07:00
|
|
|
val dpi = resources.displayMetrics.density
|
|
|
|
val x = pointMap.getDouble("x") * dpi
|
|
|
|
val y = pointMap.getDouble("y") * dpi
|
2021-02-19 08:28:14 -07:00
|
|
|
|
2022-03-31 10:01:21 -06:00
|
|
|
// Getting the point from the previewView needs to be run on the UI thread
|
|
|
|
val point = withContext(coroutineScope.coroutineContext) {
|
|
|
|
previewView.meteringPointFactory.createPoint(x.toFloat(), y.toFloat());
|
|
|
|
}
|
|
|
|
|
2021-02-26 02:56:20 -07:00
|
|
|
val action = FocusMeteringAction.Builder(point, FocusMeteringAction.FLAG_AF or FocusMeteringAction.FLAG_AE)
|
|
|
|
.setAutoCancelDuration(5, TimeUnit.SECONDS) // auto-reset after 5 seconds
|
|
|
|
.build()
|
2021-02-19 08:28:14 -07:00
|
|
|
|
2021-02-26 02:56:20 -07:00
|
|
|
cameraControl.startFocusAndMetering(action).await()
|
2021-02-19 08:28:14 -07:00
|
|
|
}
|