Man Camera2 APIs are hard to master…
I’ve scanned SO for a way to get touch-to-focus to work on Android and could not find a solution that works.
These were very partial, or provided only scaffold code which I couldn’t use:
- http://stackoverflow.com/questions/33151244/implement-tap-to-focus-in-camera2-api
- http://stackoverflow.com/questions/41649691/android-camera2-api-touch-to-focus-example
- http://stackoverflow.com/questions/34681903/android-camera2-touch-to-focus-implementation-cancelling-on-new-touch
- http://stackoverflow.com/questions/39850590/android-camera2-tap-to-focus
- https://www.reddit.com/r/androiddev/comments/3mpa90/camera2_touch_focus/
Finally I was able to do it by myself this way:
Enjoy
Roy
4 replies on “Android Camera2 Touch-to-Focus”
what is mPolygonDrawerView ?
It’s leftover from other code. I’ll remove. Thanks!
It is not entirely correct to do this:
final int y = (int)((motionEvent.getX() / (float)view.getWidth()) * (float)sensorArraySize.height());
final int x = (int)((motionEvent.getY() / (float)view.getHeight()) * (float)sensorArraySize.width());
The problem is that aspect ratio of the sensor might not be equal to the aspect ratio of the texture view used for camera preview. What you see on the preview may correspond to just a part of the camera sensor, so in general case you need a more accurate mapping from texture view coordinates to active array coordinates.
This link might be helpful: https://source.android.com/devices/camera/camera3_crop_reprocess
if i keep the repeat off then the preview gets stuck
and if i keep it continous then it keeps on focusing and de focusing