10

I just need to know about how can I start with this actually I need to open the Android native camera using opencv.

Where can I find the related docs or any helping material? I have setup my eclipse working with the opencv sample projects!

1 Answer 1

18

Have a look at the shipped opencv samples in opencv/samples/android/, there you should be able to find a few good examples. Here is also a link to the docs that shows how to open the camera. Don't forget to request the permissions to access the camera.

Short version, see the link for full details: Add a layout:

<org.opencv.android.JavaCameraView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/HelloOpenCvView"
    opencv:show_fps="true"
    opencv:camera_id="any" />

Init procedure:

private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) {
    @Override
    public void onManagerConnected(int status) {
        switch (status) {
            case LoaderCallbackInterface.SUCCESS:
            {
                Log.i(TAG, "OpenCV loaded successfully");
                mOpenCvCameraView.enableView();
            } break;
            default:
            {
                super.onManagerConnected(status);
            } break;
        }
    }
};

@Override
public void onResume()
{
    super.onResume();
    OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_3, this, mLoaderCallback);
}

Implement CVFrameListener2 interface:

 private CameraBridgeViewBase mOpenCvCameraView;

 @Override
 public void onCreate(Bundle savedInstanceState) {
     Log.i(TAG, "called onCreate");
     super.onCreate(savedInstanceState);
     getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
     setContentView(R.layout.HelloOpenCvLayout);
     mOpenCvCameraView = (CameraBridgeViewBase) findViewById(R.id.HelloOpenCvView);
     mOpenCvCameraView.setVisibility(SurfaceView.VISIBLE);
     mOpenCvCameraView.setCvCameraViewListener(this);
 }

 @Override
 public void onPause()
 {
     super.onPause();
     if (mOpenCvCameraView != null)
         mOpenCvCameraView.disableView();
 }

 public void onDestroy() {
     super.onDestroy();
     if (mOpenCvCameraView != null)
         mOpenCvCameraView.disableView();
 }

 public void onCameraViewStarted(int width, int height) {
 }

 public void onCameraViewStopped() {
 }

 public Mat onCameraFrame(CvCameraViewFrame inputFrame) {
     return inputFrame.rgba();
 }
Sign up to request clarification or add additional context in comments.

4 Comments

could you also tell how to detect an area from the captured image?
Please add this as a new question, since this is unrelated to this one. Especially since this is a broad topic.
Can i get cameraVersion in openCv?
What is this code supposed to do? JavaCameraView is hidden (gone) so nothing visible. And I don't see where it opens a camera.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.