Categories
Android Java video

Android Camera2 Touch-to-Focus

Touch to focus on Android using Camera2 APIs

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:

Finally I was able to do it by myself this way:

Enjoy
Roy

4 replies on “Android Camera2 Touch-to-Focus”

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

Leave a Reply

Your email address will not be published. Required fields are marked *