@@ -23,9 +23,13 @@ import androidx.camera.core.CameraSelector
2323import androidx.camera.core.CameraXConfig
2424import androidx.camera.core.ExperimentalGetImage
2525import androidx.camera.core.ExperimentalLensFacing
26+ import androidx.camera.core.FocusMeteringAction
2627import androidx.camera.core.ImageAnalysis
2728import androidx.camera.core.ImageProxy
29+ import androidx.camera.core.MeteringPoint
30+ import androidx.camera.core.MeteringPointFactory
2831import androidx.camera.core.Preview
32+ import androidx.camera.core.SurfaceOrientedMeteringPointFactory
2933import androidx.camera.core.SurfaceRequest
3034import androidx.camera.core.TorchState
3135import androidx.camera.core.resolutionselector.ResolutionSelector
@@ -343,6 +347,7 @@ class MobileScanner(
343347 detectionTimeout : Long ,
344348 cameraResolutionWanted : Size ? ,
345349 invertImage : Boolean ,
350+ initialZoom : Double ,
346351 ) {
347352 this .detectionSpeed = detectionSpeed
348353 this .detectionTimeout = detectionTimeout
@@ -471,6 +476,18 @@ class MobileScanner(
471476 if (it.cameraInfo.hasFlashUnit()) {
472477 it.cameraControl.enableTorch(torch)
473478 }
479+
480+ try {
481+ if (initialZoom in 0.0 .. 1.0 ) {
482+ it.cameraControl.setLinearZoom(initialZoom.toFloat())
483+ } else {
484+ it.cameraControl.setZoomRatio(initialZoom.toFloat())
485+ }
486+ } catch (e: Exception ) {
487+ mobileScannerErrorCallback(ZoomNotInRange ())
488+
489+ return @addListener
490+ }
474491 }
475492
476493 val resolution = analysis.resolutionInfo!! .resolution
@@ -701,6 +718,23 @@ class MobileScanner(
701718 camera?.cameraControl?.setZoomRatio(1f )
702719 }
703720
721+ fun setFocus (x : Float , y : Float ) {
722+ val cam = camera ? : throw ZoomWhenStopped ()
723+
724+ // Ensure x,y are normalized (0f..1f)
725+ if (x !in 0f .. 1f || y !in 0f .. 1f ) {
726+ throw IllegalArgumentException (" Focus coordinates must be between 0.0 and 1.0" )
727+ }
728+
729+ val factory: MeteringPointFactory = SurfaceOrientedMeteringPointFactory (1f , 1f )
730+ val afPoint: MeteringPoint = factory.createPoint(x, y)
731+
732+ val action = FocusMeteringAction .Builder (afPoint, FocusMeteringAction .FLAG_AF )
733+ .build()
734+
735+ cam.cameraControl.startFocusAndMetering(action)
736+ }
737+
704738 /* *
705739 * Dispose of this scanner instance.
706740 */
0 commit comments