I'm creating a custom camera app and trying to add focus feature like below
val factory = SurfaceOrientedMeteringPointFactory(
viewFinder.width.toFloat(),
viewFinder.height.toFloat()
)
val point = factory.createPoint(event.x, event.y)
try {
cameraControl?.cancelFocusAndMetering()
val action = FocusMeteringAction.Builder(point, FocusMeteringAction.FLAG_AF)
.setAutoCancelDuration(5, TimeUnit.SECONDS)
.build()
}
The focusing is not accurate, as you can see below the white circle where I tapped to focus the pen and look where it got focused.
The distance between the tap and focus is pretty large. That was not the case with my default camera app.
How to adjust focusing to the point where I Tap on PreviewView
The camera isn't accurately acquiring focus on the tap region because the conversion SurfaceOrientedMeteringPointFactory
is performing from UI coordinates to normalized sensor rect coordinates is probably incorrect. The conversion should also take into consideration things like PreviewView
's layout direction, scale type, the device's rotation and whether the camera is front/back facing. SurfaceOrientedMeteringPointFactory
doesn't do that. If you're curious what the conversion looks like, check out PreviewViewMeteringPointFactory
.
PreviewView
already provides a metering point factory that handles the conversion internally, you can call PreviewView.getMeteringPointFactory()
and pass it the tap coordinates.