Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Manual Focus using android camera2 API

I want to develop an Android Camera App for myself (can share it if there are interested people) that has a manual focus while video recording.

I've added a SeekBar to the google sample Camera2 app but I can't find the way to implement the manual focus.

I found Manual focus in camera2, android but it doesn't work on my LG G4. The stock camera app is almost perfect since it doesn't allow the manual focus in video mode.

Does anyone of you have an idea ?

EDIT: here's the code of the SeekBar listener:

@Override
public void onStopTrackingTouch(SeekBar seekBar) {}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {
    mPreviewBuilder.set(CaptureRequest.CONTROL_AF_MODE, CameraMetadata.CONTROL_AF_MODE_OFF);
}

@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
    float minimumLens = characteristics.get(CameraCharacteristics.LENS_INFO_MINIMUM_FOCUS_DISTANCE);
    float num = (((float)progress) * minimumLens / 100);
    mPreviewBuilder.set(CaptureRequest.LENS_FOCUS_DISTANCE, num);
}
like image 895
Raphaël Avatar asked Mar 20 '17 10:03

Raphaël


People also ask

What is the use of Camera2 API?

Camera2 is the latest low-level Android camera package and replaces the deprecated Camera class. Camera2 provides in-depth controls for complex use cases, but requires you to manage device-specific configurations. You can read about specific Camera2 classes and functions in the reference documentation.

What is Level 3 Camera2 API?

Level_3: These devices support YUV reprocessing and RAW image capture, along with additional output stream configurations on top of full Camera2 API support. External: Similar to LIMITED devices with some exceptions (e.g. some sensor or lens information may not be reported or have less stable frame rates).


2 Answers

You need to check if the device you're running on actually supports manual controls in camera2.

The key is whether the available capabilities of the camera device lists MANUAL_SENSOR. If so, then you can control the lens by setting the autofocus mode to OFF, and then setting the lens focus distance to your desired value. If MANUAL_SENSOR is not listed, then the device very likely doesn't support manual focus control (Some manufacturers use private interfaces for their default camera app to implement manual focus control, unfortunately).

These should all go into the repeating request you're using to control preview.

like image 66
Eddy Talvala Avatar answered Sep 18 '22 00:09

Eddy Talvala


Eddy Talvala described everything correctly in his answer, but if you still feel complicated, here is a code snippet that sets focus to infinity (which has value 0f):

CameraCharacteristics characteristics = manager.getCameraCharacteristics(cameraId);
int[] capabilities = characteristics
    .get(CameraCharacteristics.REQUEST_AVAILABLE_CAPABILITIES);

boolean isManualFocusSupported = IntStream.of(capabilities)
    .anyMatch(x -> x == CameraCharacteristics.REQUEST_AVAILABLE_CAPABILITIES_MANUAL_SENSOR);

if (isManualFocusSupported) {
    previewBuilder.set(CaptureRequest.CONTROL_AF_MODE, CaptureRequest.CONTROL_AF_MODE_OFF);
    previewBuilder.set(CaptureRequest.LENS_FOCUS_DISTANCE, 0f);
}
like image 44
Alexander Ponomarev Avatar answered Sep 18 '22 00:09

Alexander Ponomarev