找不到本机方法:org.opencv.core.Mat.n_Mat :()J

时间:2016-10-20 20:18:07

标签: android opencv android-studio

我正在使用原生的Android Opencv 3.1.0库 但总是显示这样的错误

  

java.lang.UnsatisfiedLinkError:找不到本机方法:org.opencv.core.Mat.n_Mat :()J

这是我的代码

package com.example.saya.cameraopencv;

public class Hasil extends AppCompatActivity{

private TextView coba;
private ImageView gambarskrg;
private Mat rgba;

final String TAG = "Hello World";

private BaseLoaderCallback mOpenCVCallBack = 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
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.hasil_activity);
    OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_3_1_0, this, mOpenCVCallBack);
    Bundle i = getIntent().getExtras();

    gambarskrg = (ImageView) findViewById(R.id.gambar);

    String gambar = Environment.getExternalStorageDirectory()+ "/Coba/Coba_1476987074709.jpg";

    Bitmap bmp = BitmapFactory.decodeFile(gambar);

    Log.i(TAG, "Trying to load OpenCV library");
    if (!OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_3_1_0, this, mOpenCVCallBack))
    {
        Log.e(TAG, "Cannot connect to OpenCV Manager");
    }
    else {
        Log.e(TAG, "Berhasil");
        try {
            detectEdges(bmp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

private void detectEdges(Bitmap bmp){
    Mat rgba = new Mat();
    Utils.bitmapToMat(bmp, rgba);

    Mat edges = new Mat(rgba.size(), CvType.CV_8UC4);
    Imgproc.cvtColor(rgba, edges, Imgproc.COLOR_RGBA2GRAY, 4);
    Imgproc.Canny(edges, edges, 80, 100);

    Bitmap resultBitmap = Bitmap.createBitmap(edges.cols(), edges.rows(), Bitmap.Config.ARGB_8888);
    Utils.matToBitmap(edges, resultBitmap);
    int nh = (int) ( resultBitmap.getHeight() * (512.0 / resultBitmap.getWidth()) );
    Bitmap scaled = Bitmap.createScaledBitmap(resultBitmap, 512, nh, true);
    gambarskrg.setImageBitmap(scaled);
}

}

这就是logcat

  

10-21 04:02:32.333 21528-21528 / com.example.saya.cameraopencv E / AndroidRuntime:FATAL EXCEPTION:main                                                                                  处理:com.example.saya.cameraopencv,PID:21528                                                                                  java.lang.UnsatisfiedLinkError:找不到本机方法:org.opencv.core.Mat.n_Mat :()J                                                                                      at org.opencv.core.Mat.n_Mat(Native Method)                                                                                      在org.opencv.core.Mat。(Mat.java:24)                                                                                      在com.example.saya.cameraopencv.Hasil.detectEdges(Hasil.java:88)                                                                                      在com.example.saya.cameraopencv.Hasil.onCreate(Hasil.java:79)                                                                                      在android.app.Activity.performCreate(Activity.java)                                                                                      在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java)                                                                                      在android.app.ActivityThread.performLaunchActivity(ActivityThread.java)                                                                                      在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java)                                                                                      在android.app.ActivityThread.access $ 800(ActivityThread.java)                                                                                      在android.app.ActivityThread $ H.handleMessage(ActivityThread.java)                                                                                      在android.os.Handler.dispatchMessage(Handler.java)                                                                                      在android.os.Looper.loop(Looper.java)                                                                                      在android.app.ActivityThread.main(ActivityThread.java)                                                                                      at java.lang.reflect.Method.invokeNative(Native Method)                                                                                      在java.lang.reflect.Method.invoke(Method.java:515)                                                                                      在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java)                                                                                      在com.android.internal.os.ZygoteInit.main(ZygoteInit.java)                                                                                      at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132)                                                                                      在dalvik.system.NativeStart.main(本地方法)

1 个答案:

答案 0 :(得分:3)

代码存在一些问题。在onCreate方法中,您可以在OpenCVLoader.initAsync后第一次调用setContentView方法,第二次在Log.i(TAG, "Trying to load OpenCV library")消息后的if语句中调用OpenCVLoader.initAsync方法。您应该只调用onResume方法一次,OpenCV4Android SDK附带的许多示例都是使用Mat方法执行此操作。为了更好地了解其原因,请查看描述活动生命周期的this article

这里的第二个问题是您在OpenCV完成初始化之前尝试创建Mat对象。在创建onManagerConnected对象之前,必须先等待调用OpenCV初始化方法的异步任务返回到UIThread。当OpenCV初始化线程完成运行时,它调用onManagerConnected方法。因此,要解决您的问题,必须在private Bitmap bmp; final String TAG = "Hello World"; private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) { @Override public void onManagerConnected(int status) { switch (status) { case LoaderCallbackInterface.SUCCESS: { Log.i(TAG, "OpenCV loaded successfully"); try { detectEdges(bmp); } catch (Exception e) { e.printStackTrace(); } } break; default: { super.onManagerConnected(status); } break; } } }; @Override public void onResume() { super.onResume(); if (!OpenCVLoader.initDebug()) { Log.d(TAG, "Internal OpenCV library not found. Using OpenCV Manager for initialization"); OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_3_1_0, this, mLoaderCallback); } else { Log.d(TAG, "OpenCV library found inside package. Using it!"); mLoaderCallback.onManagerConnected(LoaderCallbackInterface.SUCCESS); } } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.hasil_activity); Bundle i = getIntent().getExtras(); String gambar = Environment.getExternalStorageDirectory()+ "/Coba/Coba_1476987074709.jpg"; bmp = BitmapFactory.decodeFile(gambar); } private void detectEdges(Bitmap bmp){ Mat rgba = new Mat(); Utils.bitmapToMat(bmp, rgba); Mat edges = new Mat(rgba.size(), CvType.CV_8UC4); Imgproc.cvtColor(rgba, edges, Imgproc.COLOR_RGBA2GRAY, 4); Imgproc.Canny(edges, edges, 80, 100); Bitmap resultBitmap = Bitmap.createBitmap(edges.cols(), edges.rows(), Bitmap.Config.ARGB_8888); Utils.matToBitmap(edges, resultBitmap); int nh = (int) ( resultBitmap.getHeight() * (512.0 / resultBitmap.getWidth()) ); Bitmap scaled = Bitmap.createScaledBitmap(resultBitmap, 512, nh, true); ((ImageView) findViewById(R.id.gambar)).setImageBitmap(scaled); } 方法中调用Mat初始化代码,以确保仅在OpenCV初始化代码完成执行后调用它。以下是它应该如何显示的示例:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

此外,由于您从外部存储器中读取图像,因此根据您要定位的Android版本,您可能需要请求用户读取内存的权限。您可以通过在Android清单中添加以下代码来实现此目的:

$galleries =  Gallery::where('user_id', $userId)->paginate(10);