Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenCv crashes when calling from android activity

i have this code in which i use openCV to launch a camera. The code is given below. If this class "MainActivity" is used as the main class then every thing works fine but if i use it from another activity then the application crashes.

Here's the code of openCV android

public class MainActivity extends Activity implements CvCameraViewListener2 {

private CameraBridgeViewBase mOpenCvCameraView;

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);
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
     setContentView(R.layout.helloopencvlayout);
     mOpenCvCameraView = (CameraBridgeViewBase) findViewById(R.id.HelloOpenCvView);
     mOpenCvCameraView.setVisibility(SurfaceView.VISIBLE);
     mOpenCvCameraView.setCvCameraViewListener((CvCameraViewListener2) 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();

 }

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

Now if i try to call this from another activity using intent in which i just have a button and onclick method is implemented and when the button is pressed , this activity is called , the application crashes.

It's been more the 4 hours , i am struggling with it. Please help and thanks in advance.

What i believe is that there is a callback method implemented , so we can't call this activity as normal activity call , or may be some other concept. I search it on the internet , but nothing helps. Some where i also found the concept of Async call , but i didn't get it.

Please help. Thanks.

like image 368
Umair Khalid Avatar asked Mar 20 '26 11:03

Umair Khalid


1 Answers

If you will use try catch block on the oncreate function then you will come to know that the problem is of typecasting exception that is application can't typecast the org.opencv activity class to your activity.

So Instead if typecasting this , if i type cast MainActivity.this , the problem is solved.

Simple but took me hell of time to figure out.

like image 184
Umair Khalid Avatar answered Mar 22 '26 02:03

Umair Khalid