Now you can Subscribe using RSS

Submit your Email

Wednesday

Set focus modes in OpenCV using JavaCameraView for Android

Vishal Shrestha
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.

Autofocus using openCV for android
OpenCV for Android


How to set Autofocus and other camera modes using OpenCV for Android :
autofocus using openCV for Android
Focusing a camera
  1. Create a class that extends from JavaCameraView of OpenCV for Android.
  2. Create a setter function that takes a context and focus mode so that we can use this function to set focus from other activities.
  3. Get Camera.Parameters using mCamera variable of JavaCameraView :
  4. Camera.Parameters params = mCamera.getParameters(); 
  5. Cancel the autoFocus and set autoFocus using Camera.AutoFocusCallback :
  6. mCamera.cancelAutoFocus();
            mCamera.autoFocus(new Camera.AutoFocusCallback() {
                @Override
                public void onAutoFocus(boolean b, Camera camera) {
                }
            });
  7. Get the list of supported focus modes from the Camera.Parameters variable that we created in the second step :
  8. List<String> FocusModes = params.getSupportedFocusModes();
  9. Create a switch to find out which focus mode user has selected in the 2nd step and set that focus mode.
  10. Now use this class that we created in XML and set the focus mode using the method that we created in the second step.

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

Vishal Shrestha / Author & Founder

A developer by profession, a born Adventurer. I mainly do Android but like to get my hands dirty with web development and a little bit of Python. I would't rather go on a Trek than a party and you can find me having a few rounds with the heavy bag to let out the steam ;)

For Business info : My Portfolio Site.

0 comments:

Post a Comment

Coprights @ 2017 | The Code City by Vishal Shrestha Vishal Shrestha