Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open the Android native Camera using OpenCV

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!

like image 528
user2496503 Avatar asked Jun 18 '13 09:06

user2496503


People also ask

Can we use OpenCV for Android app?

The below steps for using Android OpenCV sdk in Android Studio. This is a simplified version of this SO answer. Download latest OpenCV sdk for Android from OpenCV.org and decompress the zip file. Import OpenCV to Android Studio, From File -> New -> Import Module, choose sdk/java folder in the unzipped opencv archive.

What is Open CV Android?

Basically OpenCV is an open source computer vision and machine learning software library. Based on the documentation this library has more than 2500 optimized algorithm inside of it.


1 Answers

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:visibility="gone"
    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();
 }
like image 88
Nicholas Avatar answered Oct 22 '22 13:10

Nicholas