1

这个问题让我发疯了好几天。我正在尝试使用 opencv android 最新的软件包。一切都已插入并且我没有任何错误,直到我在 android 上运行该项目并且那是它崩溃的时候。当我调用一个简单的代码时, Mat m = new Mat(); 应用程序崩溃了,我看到其他人也有同样的问题,但他们设法修复了它,这是我的代码,也许这是我看不到的愚蠢的东西!我真正需要的是 Matrix 库,我尝试了 Jama 和 jblas,但它们在某种程度上可以工作,但随后它们也崩溃了,而且它们非常慢。

 package com.ece.facerecog;
 import java.io.File;
 import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;'
 import java.util.Arrays;

 import org.opencv.android.BaseLoaderCallback;
 import org.opencv.android.LoaderCallbackInterface;
 import org.opencv.android.OpenCVLoader;
     import org.opencv.core.Mat;

import com.ece.facerecog.utils.UIHelper;


//import org.jblas.DoubleMatrix;


  //import Jama.Matrix;
     import android.app.Activity;
 import android.content.Context;
    import android.graphics.Bitmap;
  import android.graphics.BitmapFactory;
 import android.graphics.Color;
 //import android.graphics.Matrix;

  import android.os.Bundle;
 import android.os.Environment;
 import android.util.Log;
 import android.widget.GridView;
 import android.widget.ImageView;
  import android.widget.TextView;

  public class Face extends Activity {
private Bitmap bitmap;
private int f = Crop.k;
private ImageView tv;

   private static final String    TAG                 = "OCVSample::Activity";

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

            } break;
            default:
            {
                super.onManagerConnected(status);
            } break;
        }
    }
};
  //    @Override
   //   public void onResume()
    //  {
  //        super.onResume();
  //        OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_5, this,                           k                  mLoaderCallback);
    //    }

private Bitmap ReadImage1(String fBitmap) {
    String root = Environment.getExternalStorageDirectory().toString();
    File myDir = new File(root + "/preprocessed");
    File file = new File(myDir , fBitmap); //or any other format supported
    UIHelper.displayText(this, R.id.textView1, file.toString());

    try {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inPreferredConfig = Bitmap.Config.ARGB_8888;        
        Bitmap bitmap =            `BitmapFactory.decodeFile(file.getAbsolutePath(),options); //This gets the image `       
        return bitmap;

        } catch (Exception e) {
               e.printStackTrace();
               UIHelper.displayText(this, R.id.textView1, "Doesn't exist");
        }

        return bitmap;
    }



   @Override
   public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.face);

Log.i(TAG, "Trying to load OpenCV library");
if (!OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_5, this, mLoaderCallback))
{
  Log.e(TAG, "Cannot connect to OpenCV Manager");
}


ImageView tv = (ImageView) findViewById(R.id.imageView1);
Bitmap bmp = ReadImage1("/Image-" + f+ ".jpg");
tv.setImageBitmap(bmp);
Mat m = new Mat();

} }

`

这是日志错误,

04-13 23:14:17.412: E/AndroidRuntime(12111): FATAL EXCEPTION: main
04-13 23:14:17.412: E/AndroidRuntime(12111): java.lang.UnsatisfiedLinkError: Native method not found: org.opencv.core.Mat.n_Mat:()J
04-13 23:14:17.412: E/AndroidRuntime(12111):    at org.opencv.core.Mat.n_Mat(Native Method)
04-13 23:14:17.412: E/AndroidRuntime(12111):    at org.opencv.core.Mat.<init>(Mat.java:441)
04-13 23:14:17.412: E/AndroidRuntime(12111):    at com.ece.facerecog.Face.onCreate(Face.java:147)
04-13 23:14:17.412: E/AndroidRuntime(12111):    at android.app.Activity.performCreate(Activity.java:5104)
04-13 23:14:17.412: E/AndroidRuntime(12111):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
04-13 23:14:17.412: E/AndroidRuntime(12111):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
04-13 23:14:17.412: E/AndroidRuntime(12111):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
04-13 23:14:17.412: E/AndroidRuntime(12111):    at android.app.ActivityThread.access$600(ActivityThread.java:141)
04-13 23:14:17.412: E/AndroidRuntime(12111):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
04-13 23:14:17.412: E/AndroidRuntime(12111):    at android.os.Handler.dispatchMessage(Handler.java:99)
04-13 23:14:17.412: E/AndroidRuntime(12111):    at android.os.Looper.loop(Looper.java:137)
04-13 23:14:17.412: E/AndroidRuntime(12111):    at android.app.ActivityThread.main(ActivityThread.java:5039)
04-13 23:14:17.412: E/AndroidRuntime(12111):    at java.lang.reflect.Method.invokeNative(Native Method)
04-13 23:14:17.412: E/AndroidRuntime(12111):    at java.lang.reflect.Method.invoke(Method.java:511)
04-13 23:14:17.412: E/AndroidRuntime(12111):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-13 23:14:17.412: E/AndroidRuntime(12111):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-13 23:14:17.412: E/AndroidRuntime(12111):    at dalvik.system.NativeStart.main(Native Method)

谢谢你的协助!

4

2 回答 2

4

我认为必须在 a或Mat中调用OpenCV 函数。在没有帮助的情况下,我遇到了 AGES 的相同问题。threadAsyncTask

为了避免崩溃,您可以声明Mat m为全局变量并在AsyncInitializationOpenCV 块中对其进行初始化。

像这样的东西:

 public class Face extends Activity {
 private Bitmap bitmap;
 private int f = Crop.k;
 private ImageView tv;
 Mat m;
 private static final String    TAG                 = "OCVSample::Activity";

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

如果它仍然崩溃,我建议您执行您AsyncTask在我的项目中尝试的任何功能。Mat然而,函数可以在没有UnsatisfiedLinkErrorOpenCV 函数异常的风险的情况下声明。然而,在 UI 中调用的函数会遇到这种情况。至少这是我所看到的。

于 2013-04-14T07:07:13.850 回答
4

我刚刚遇到同样的问题并找到了另一个解决方案:
您可以使用另一个活动来调用您的活动(“Face”),例如一些带有启动活动的按钮的介绍屏幕。如果您在此介绍活动中初始化 OpenCV(与您在活动中所做的相同),您可以在下一个活动中使用 OpenCV 函数而不会出现任何问题...

这样您就不必将 OpenCV 变量声明为全局变量并在 OpenCV 的 AsyncInitialization 块中对其进行初始化。

于 2013-05-16T19:34:02.050 回答