简体   繁体   English

使用OpenCV 2.4.2的Android UnsatisfiedLinkError

[英]Android UnsatisfiedLinkError with OpenCV 2.4.2

just trying to do a simple openCV android program. 只是想尝试一个简单的openCV android程序。 Downloaded and installed OpenCV for Android following the instructions here and added the OpenCV Library 2.4.2 as a library project for my own android project like the instructions state. 按照此处说明下载并安装OpenCV for Android,并将OpenCV Library 2.4.2添加为我自己的android项目的库项目,如指令状态。

However when I compile the standard "Hello World Program", as follows, it fails if I include the Mat mat = new Mat(); 但是当我编译标准的“Hello World Program”时,如下所示,如果我包含Mat mat = new Mat(); line, but succeeds otherwise. 线,但否则成功。

package com.example;

import org.opencv.core.Mat;

import android.app.Activity;
import android.os.Bundle;

public class HelloAndroidActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Mat mat = new Mat();
    }
}

Here's the stack trace it prints out: 这是打印出来的堆栈跟踪:

    07-23 09:59:43.835: E/AndroidRuntime(8222): FATAL EXCEPTION: main
07-23 09:59:43.835: E/AndroidRuntime(8222): java.lang.UnsatisfiedLinkError: n_Mat
07-23 09:59:43.835: E/AndroidRuntime(8222):     at org.opencv.core.Mat.n_Mat(Native Method)
07-23 09:59:43.835: E/AndroidRuntime(8222):     at org.opencv.core.Mat.<init>(Mat.java:181)
07-23 09:59:43.835: E/AndroidRuntime(8222):     at com.example.HelloAndroidActivity.onCreate(HelloAndroidActivity.java:15)
07-23 09:59:43.835: E/AndroidRuntime(8222):     at android.app.Activity.performCreate(Activity.java:4538)
07-23 09:59:43.835: E/AndroidRuntime(8222):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1071)
07-23 09:59:43.835: E/AndroidRuntime(8222):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2161)
07-23 09:59:43.835: E/AndroidRuntime(8222):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2240)
07-23 09:59:43.835: E/AndroidRuntime(8222):     at android.app.ActivityThread.access$600(ActivityThread.java:139)
07-23 09:59:43.835: E/AndroidRuntime(8222):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1262)
07-23 09:59:43.835: E/AndroidRuntime(8222):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-23 09:59:43.835: E/AndroidRuntime(8222):     at android.os.Looper.loop(Looper.java:154)
07-23 09:59:43.835: E/AndroidRuntime(8222):     at android.app.ActivityThread.main(ActivityThread.java:4977)
07-23 09:59:43.835: E/AndroidRuntime(8222):     at java.lang.reflect.Method.invokeNative(Native Method)
07-23 09:59:43.835: E/AndroidRuntime(8222):     at java.lang.reflect.Method.invoke(Method.java:511)
07-23 09:59:43.835: E/AndroidRuntime(8222):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-23 09:59:43.835: E/AndroidRuntime(8222):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-23 09:59:43.835: E/AndroidRuntime(8222):     at dalvik.system.NativeStart.main(Native Method)

Two things to note: I am not directly using anything native in this code (like some other questions on here) and the old OpenCV 2.3.x library worked just fine before using the same method. 有两点需要注意:我没有直接使用此代码中的任何原生代码(就像这里的其他一些问题一样),旧的OpenCV 2.3.x库在使用相同的方法之前工作得很好。 Both Android projects have the same target and supported API settings. 两个Android项目都具有相同的目标和支持的API设置。

Figured it out. 弄清楚了。 Wasn't statically linking the library. 没有静态链接库。 If you use this code instead, it works. 如果您使用此代码,它可以工作。

package com.example;

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

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

public class HelloAndroidActivity extends Activity
{

    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");
      // Create and set View
      setContentView(R.layout.main);
       } break;
       default:
       {
      super.onManagerConnected(status);
       } break;
   }
    }
};

/** Called when the activity is first created. */
@Override
public 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_2, this, mOpenCVCallBack))
    {
      Log.e(TAG, "Cannot connect to OpenCV Manager");
    }
}
}

However, not too fond of this "OpenCV Manager" idea. 但是,不太喜欢这种“OpenCV Manager”的想法。 Makes it so the user has to install several packages manually before the app will work. 这使得用户必须在应用程序运行之前手动安装多个软件包。

The solution is to either do like in @Jason answer which is about using the OpenCV Manager. 解决方案是在@Jason回答中使用OpenCV Manager。 It is also explained in great detail on the official documentation here . 这里官方文档也详细解释了这一点。

But like @Jason says, "Makes it so the user has to install severally packages manually before the app will work". 但是就像@Jason说的那样,“使用户必须在应用程序工作之前手动安装多个软件包”。 Although this is true, OpenCV Manager has some advantages like for example: 虽然这是事实,但OpenCV Manager具有一些优势,例如:

  • If OpenCV is updated, the user only needs to update the manager/library. 如果更新了OpenCV,则用户只需要更新管理器/库。 The applications which use the manager can remain the same. 使用管理器的应用程序可以保持不变。

  • Your app apk size will be a lot smaller: 你的应用程序apk大小会小很多:

    • A simple opencv app will be about ~400KB for each app + ~800KB for OpenCV Manager + ~12MB for the OpenCV library compiled for your device architecture. 一个简单的opencv应用程序每个应用程序大约约400KB + OpenCV Manager大约800KB +为您的设备架构编译的OpenCV库大约12MB。
    • With the traditional static linking every single opencv app in your device would be at least ~25MB. 使用传统的静态链接,您设备中的每个 opencv应用程序至少约为25MB。
    • These sizes depend, off course, on the amount of stuff you place inside your app ... 当然,这些尺寸取决于您放置在应用程序中的内容量...

Even so, if you wish to deploy your apps the traditional way, static linking , you can read the instructions here . 即便如此,如果您希望以传统的方式部署应用程序, 静态链接您可以在此处阅读说明

 static{System.loadLibrary("opencv_java3"); } //the name of the .so file, without the 'lib' prefix

You can statically load the open cv library everywhere in your activity. 您可以在活动的任何位置静态加载打开的cv库。 Search the .so file in your jniLibs folder and copy/paste it as argument of "loadLibrary" method without the lib prefix. 搜索jniLibs文件夹中的.so文件,并将其复制/粘贴为“loadLibrary”方法的参数,不带lib前缀。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 Tesseract 和 OpenCV 的 Android UnsatisfiedLinkError - Android UnsatisfiedLinkError With Tesseract and OpenCV 在Android上使用opencv 2.4.2合并两个图像 - merge two image using opencv 2.4.2 on android Android的原生OpenCV示例抛出UnsatisfiedLinkError - Native OpenCV Samples for Android throws UnsatisfiedLinkError Android for OpenCV - 错误打开跟踪文件,UnsatisfiedLinkError - Android for OpenCV - error opening trace file, UnsatisfiedLinkError Android OpenCV 2.4.2 Eclipse教程-无法dlopen相机包装器库 - Android OpenCV 2.4.2 Tutorials Eclipse - cannot dlopen camera wrapper library 尝试使用JNI组件启动Android + OpenCV文件时出现困惑的UnsatisfiedLinkError - Perplexing UnsatisfiedLinkError when attempting to launch Android + OpenCV file with JNI component 在Android 2.3.3上对OpenCv使用本机代码时引发UnsatisfiedLinkError - UnsatisfiedLinkError thrown while using native code for OpenCv on Android 2.3.3 找不到Android中的本机OpenCV UnsatisfiedLinkError libopencv_java.so - Native OpenCV in Android UnsatisfiedLinkError libopencv_java.so not found Android中OpenCV的JavaCpp预设。 示例抛出UnsatisfiedLinkError - JavaCpp presets for OpenCV in Android. Example throws UnsatisfiedLinkError android 无实现室 2.4.2 - android no implementation room 2.4.2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM