不满意的链接错误:n_Mat在使用opencvv2.4.3和android 4.0时



我在android中使用opencv。但当我在代码中添加Mat()时,我的应用程序在启动后意外停止。我的错误日志如下:

FATAL EXCEPTION: main
java.lang.UnsatisfiedLinkError: n_Mat
at org.opencv.core.Mat.n_Mat(Native Method)
at org.opencv.core.Mat.<init>(Mat.java:441)
at com.example.imagepro.MainActivity.onCreate(MainActivity.java:36)
at android.app.Activity.performCreate(Activity.java:4465)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
at android.app.ActivityThread.access$600(ActivityThread.java:123)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)     
at android.app.ActivityThread.main(ActivityThread.java:4424)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)`

我的代码是

import java.io.File;
import org.opencv.android.BaseLoaderCallback;
import org.opencv.android.LoaderCallbackInterface;
import org.opencv.android.OpenCVLoader;
import org.opencv.core.Mat;
import org.opencv.android.Utils;
import org.opencv.imgproc.Imgproc;
import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.ImageView;
public class MainActivity extends Activity {
final String TAG = "Hello World";
Mat imgToProcess;
private BaseLoaderCallback mOpenCVCallBack = new BaseLoaderCallback(this) {
@Override
public void onManagerConnected(int status) {
   switch (status) {
       case LoaderCallbackInterface.SUCCESS:
       {
      Log.i(TAG, "OpenCV loaded successfully");
      // Create and set View
      setContentView(R.layout.activity_main);
       } break;
       default:
       {
      super.onManagerConnected(status);
       } break;
   }
    }
};
@Override
protected void onCreate(Bundle savedInstanceState) {
     Log.i(TAG, "onCreate");
        super.onCreate(savedInstanceState);
        Log.i(TAG, "Trying to load OpenCV library");
        if (!OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_3, this, mOpenCVCallBack))
        {
          Log.e(TAG, "Cannot connect to OpenCV Manager");
        }
        else{ Log.i(TAG, "opencv successfully added"); }
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    String path = Environment.getExternalStorageDirectory()+ "/Koala.jpg"; // get image from sd card
     File imgFile = new File(path);
     View v = null;
          if(imgFile.exists())
 {
          Bitmap myBitmap = BitmapFactory.decodeFile(path);                 
         ImageView myImage = (ImageView) findViewById(R.id.imageView);
         myImage.setImageBitmap(myBitmap);
         Log.i(TAG, "opencv successfull 1");
         Mat imgToProcess = new Mat();
         Mat newmat = new Mat();
         Utils.bitmapToMat(myBitmap, imgToProcess); 
         Imgproc.cvtColor(imgToProcess, newmat, Imgproc.COLOR_RGB2GRAY); 
 Bitmap outImage = Bitmap.createBitmap(newmat.rows(),newmat.cols(),Bitmap.Config.ARGB_8888);
         Utils.matToBitmap(newmat, outImage);
         myImage.setImageBitmap(outImage); 
   }
 }

}

我尝试了stackoverflow上提供的其他解决方案,但都不适用。我已经将libopencv_java.so和libopencv_info.so从F:\OpenCV-2.4.3.2-android-sdk\sdk\native\libs\armeabi-v7a复制到我的/libs文件夹中,但这个错误仍然存在。

请帮忙。

提前感谢:)

很高兴有人提醒我精心发布我的答案。所以我在这里发布我的问题的解决方案:

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

           default:
           {
               super.onManagerConnected(status);
           } break;
         }
      }
 };
    @Override
    protected void onCreate(Bundle savedInstanceState) {

         super.onCreate(savedInstanceState);
         Log.i(TAG, "Trying to load OpenCV library");
         if (!OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_3, this, mOpenCVCallBack))
         {
           Log.e(TAG, "Cannot connect to OpenCV Manager");
         }
         else{ Log.i(TAG, "opencv successfull"); 
         System.out.println(java.lang.Runtime.getRuntime().maxMemory()); }
         setContentView(R.layout.frameview);
    }

这个错误背后的问题是,我们在opencv初始化之前调用了opencv相关函数(例如:Mat()),因此它显示了错误。因此,如果你把opencv函数放在onManagerConnected()中,就可以解决这个问题,如下所示:

Log.i(TAG, "OpenCV loaded successfully");
startDisplay();

这里,startDisplay()包含我的Mat()初始化。问题是,当我们启动一个应用程序时,先执行oncreate()函数,然后加载opencv,所以如果你把你的opencv函数放在onccreate()中,它会显示错误,因为opencv还没有加载。

我希望这能解决你的问题。祝你好运。。。堆叠溢出岩石!!!:)

相关内容

  • 没有找到相关文章

最新更新