Setting focus mode in Android using JavaCameraView class of OpenCV for Android is pretty easy. You just need to create a class that extends from JavaCameraView, get the camera parameters and set the focus mode in the camera parameter and set those parameters in mCamera variable.
OpenCV for Android |
How to set Autofocus and other camera modes using OpenCV for Android :
Focusing a camera |
- Create a class that extends from JavaCameraView of OpenCV for Android.
- Create a setter function that takes a context and focus mode so that we can use this function to set focus from other activities.
- Get Camera.Parameters using mCamera variable of JavaCameraView :
- Cancel the autoFocus and set autoFocus using Camera.AutoFocusCallback :
- Get the list of supported focus modes from the Camera.Parameters variable that we created in the second step :
- Create a switch to find out which focus mode user has selected in the 2nd step and set that focus mode.
- Now use this class that we created in XML and set the focus mode using the method that we created in the second step.
Camera.Parameters params = mCamera.getParameters();
mCamera.cancelAutoFocus();
mCamera.autoFocus(new Camera.AutoFocusCallback() {
@Override
public void onAutoFocus(boolean b, Camera camera) {
}
});
List<String> FocusModes = params.getSupportedFocusModes();
Here’s the full code for a class with method that can be used to set autofocus and other focus modes in Android using JavaCameraView of OpenCV :
public class MyCameraView extends JavaCameraView{
public MyCameraView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public void setFocusMode(Context item, int type) {
Camera.Parameters params = mCamera.getParameters();
mCamera.cancelAutoFocus();
mCamera.autoFocus(new Camera.AutoFocusCallback() {
@Override
public void onAutoFocus(boolean b, Camera camera) {
}
});
List<String> FocusModes = params.getSupportedFocusModes();
switch (type) {
case 0:
if (FocusModes.contains(Camera.Parameters.FOCUS_MODE_AUTO))
params.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
else
Toast.makeText(item, "Auto Mode is not supported", Toast.LENGTH_SHORT).show();
break;
case 1:
if (FocusModes.contains(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO))
params.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO);
else
Toast.makeText(item, "Continuous Mode is not supported", Toast.LENGTH_SHORT).show();
break;
case 2:
if (FocusModes.contains(Camera.Parameters.FOCUS_MODE_EDOF))
params.setFocusMode(Camera.Parameters.FOCUS_MODE_EDOF);
else
Toast.makeText(item, "EDOF Mode is not supported", Toast.LENGTH_SHORT).show();
break;
case 3:
if (FocusModes.contains(Camera.Parameters.FOCUS_MODE_FIXED))
params.setFocusMode(Camera.Parameters.FOCUS_MODE_FIXED);
else
Toast.makeText(item, "Fixed Mode is not supported", Toast.LENGTH_SHORT).show();
break;
case 4:
if (FocusModes.contains(Camera.Parameters.FOCUS_MODE_INFINITY))
params.setFocusMode(Camera.Parameters.FOCUS_MODE_INFINITY);
else
Toast.makeText(item, "Infinity Mode is not supported", Toast.LENGTH_SHORT).show();
break;
case 5:
if (FocusModes.contains(Camera.Parameters.FOCUS_MODE_MACRO))
params.setFocusMode(Camera.Parameters.FOCUS_MODE_MACRO);
else
Toast.makeText(item, "Macro Mode is not supported", Toast.LENGTH_SHORT).show();
break;
}
mCamera.setParameters(params);
}
}
Now use this class as cameraView in XML:
<company.yourapp.MyCameraView
android:id="@+id/camera_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
Now in your Activity that uses this layout, just create an object of MyCameraView and call the setFocusMode Method:
public MyCameraActivity extends AppCompatActivity implements CameraBridgeViewBase.CvCameraViewListener2 {
MyCameraView mOpenCvCameraView;
private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) {
@Override
public void onManagerConnected(int status) {
switch (status) {
case LoaderCallbackInterface.SUCCESS: {
mOpenCvCameraView.enableView();
}
break;
default: {
super.onManagerConnected(status);
}
break;
}
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mOpenCvCameraView = (MTCameraView) findViewById(R.id.camera_view);
mOpenCvCameraView.setVisibility(SurfaceView.VISIBLE);
mOpenCvCameraView.setCvCameraViewListener(this);
mOpenCvCameraView.setFocusMode(this, 0);
}
public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) {
return inputFrame.rgba();
}
}
That’s it! That’s how you can set up focus mode or any other mode in Android Using JavaCameraView and OpenCV. Setting Flash On or Off and Resolution is coming in the next post, stay tuned. If you have any problem, feel free to comment below or contact me. Happy coding!
In the next part we will learn –
1. How to set camera resolution using openCV
2. How to use LED flash of the camera using OpenCV