无法将图片加载到Mat opencv中

时间:2014-09-03 08:15:43

标签: android opencv mat

我想在Android opencv上使用Mat类加载图像,但我有一些错误。 我在我的项目上添加了库。  我的代码:     私人CameraBridgeViewBase mOpenCvCameraView;
    private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this){         @覆盖         public void onManagerConnected(int status){             开关(状态){                 case LoaderCallbackInterface.SUCCESS:                 {                     Log.i(TAG," OpenCV成功加载");                     mOpenCvCameraView.enableView();                 打破                 默认:                 {                     super.onManagerConnected(状态);                 打破             }         }     };

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



 @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.activity_main);
     mOpenCvCameraView = (CameraBridgeViewBase) findViewById(R.id.HelloOpenCvView);
     mOpenCvCameraView.setVisibility(SurfaceView.VISIBLE);
     mOpenCvCameraView.setCvCameraViewListener(this);
     if (!OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_9, this, mLoaderCallback))
     {
       Log.e(TAG, "Cannot connect to OpenCV Manager");
     }
     else
         Log.i(TAG, "Load it"); 

     Mat Image = Highgui.imread("/mnt/sdcard/1.jpg");

 }

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

日志:

enter image description here

由于

1 个答案:

答案 0 :(得分:2)

你不能在onCreate()中使用任何 opencv代码

如果没有加载opencv的本机代码,则必须等待,直到mLoaderCallback 完成

所以,将Mat上的任何操作移到onCameraViewStarted()