I'm trying to implement tap to focus using CameraX but I can't debug why it is not working, despite the focus result return true.
This is a snippet from inside of my Camera compose.
previewView.setOnTouchListener { view, motionEvent -> when (motionEvent.action) { MotionEvent.ACTION_UP -> { view.performClick() val meteringPoint = previewView.meteringPointFactory .createPoint(motionEvent.x, motionEvent.y) val action = FocusMeteringAction.Builder(meteringPoint).setAutoCancelDuration(3, TimeUnit.SECONDS).build() val result = camera.cameraControl.startFocusAndMetering(action) result.addListener( { try { val isFocusSuccessful = result.get().isFocusSuccessful Timber.tag("CameraXApp").d("Focus result: $isFocusSuccessful and is focused on (x: ${motionEvent.x}, y: ${motionEvent.y})") } catch (e: Exception) { Timber.tag("CameraXApp").d(e, "Focus request was cancelled or failed") } }, ContextCompat.getMainExecutor(previewView.context), ) } } true }
It logs that the isFocusSuccessful is true and the correct x and y coordinate, but from my testing, I couldn't tell any changes at all to the quality. And there are no error or warning in logcat at all.
So, the question is quite vague but I'm just asking for any advice for how to approach this. I'm not sure how to debug this further since there is no error or whatsoever.
isFocusSuccessful only means that the focus/metering request was accepted and the camera reported a successful AF operation. It doesn't guarantee that you'll notice a visible sharpness change.
A few things I'd like you to test:
Try focusing between a nearby object and a distant object. If both are already within the depth of field, the change can be almost impossible to notice.
Make sure you're testing with the rear main camera. Some front/ultrawide cameras are fixed-focus.
Check whether your device is already continuously focusing, because the tap may not cause a noticeable change.
Add a focus indicator at the tap location to confirm your touch coordinates and metering point are correct.
Your startFocusAndMetering() usage looks fine. The lack of a visible difference does not necessarily mean CameraX tap-to-focus is failing.
Chithila Dissanayake