无法在 OpenCV Android 示例中链接本机库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10857301/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Unable to link native library in OpenCV Android sample
提问by ArtemStorozhuk
I have OpenCV code (c++), which I want to use in Android. To do this I have to use Android NDK. I downloaded OpenCV package for Android development(ver. 2.4.0) and did all steps from that manual. Basic samples (Java API only) run without problems. Sample #3 (Tutorial 3 (Advanced) - Add Native OpenCV)builds from ndk-builder correctly. But always got exception when I'm trying to run/debug it on device from eclipse:
我有 OpenCV 代码 (c++),我想在 Android 中使用它。为此,我必须使用Android NDK。我下载了用于 Android 开发的 OpenCV 包(版本2.4.0)并完成了该手册中的所有步骤。基本示例(仅限 Java API)运行没有问题。示例 #3 (教程 3(高级) - 添加原生 OpenCV)从 ndk-builder 正确构建。但是当我尝试从 Eclipse 在设备上运行/调试它时总是出现异常:
Exception Ljava/lang/UnsatisfiedLinkError; thrown while initializing Lorg/opencv/samples/tutorial3/Sample3View;
In this line:
在这一行:
System.loadLibrary("native_sample");
Here's full logcat log:
这是完整的 logcat 日志:
05-31 23:41:45.976: W/ActivityThread(9708): Application org.opencv.samples.tutorial3 is waiting for the debugger on port 8100...
05-31 23:41:45.983: I/System.out(9708): Sending WAIT chunk
05-31 23:41:45.983: I/dalvikvm(9708): Debugger is active
05-31 23:41:46.179: I/System.out(9708): Debugger has connected
05-31 23:41:46.179: I/System.out(9708): waiting for debugger to settle...
05-31 23:41:46.382: I/System.out(9708): waiting for debugger to settle...
05-31 23:41:46.585: I/System.out(9708): waiting for debugger to settle...
05-31 23:41:46.788: I/System.out(9708): waiting for debugger to settle...
05-31 23:41:46.983: I/System.out(9708): waiting for debugger to settle...
05-31 23:41:47.186: I/System.out(9708): waiting for debugger to settle...
05-31 23:41:47.389: I/System.out(9708): waiting for debugger to settle...
05-31 23:41:47.585: I/System.out(9708): waiting for debugger to settle...
05-31 23:41:47.788: I/System.out(9708): debugger has settled (1463)
05-31 23:41:47.819: D/szipinf(9708): Initializing inflate state
05-31 23:41:47.866: I/Sample::Activity(9708): Instantiated new class org.opencv.samples.tutorial3.Sample3Native
05-31 23:41:48.909: D/dalvikvm(9708): threadid=1: still suspended after undo (sc=1 dc=1)
05-31 23:41:51.770: I/Sample::Activity(9708): onCreate
05-31 23:41:59.283: W/dalvikvm(9708): Exception Ljava/lang/UnsatisfiedLinkError; thrown while initializing Lorg/opencv/samples/tutorial3/Sample3View;
05-31 23:42:01.965: D/AndroidRuntime(9708): Shutting down VM
05-31 23:42:01.965: W/dalvikvm(9708): threadid=1: thread exiting with uncaught exception (group=0x40015560)
05-31 23:42:01.999: E/AndroidRuntime(9708): FATAL EXCEPTION: main
05-31 23:42:01.999: E/AndroidRuntime(9708): java.lang.ExceptionInInitializerError
05-31 23:42:01.999: E/AndroidRuntime(9708): at org.opencv.samples.tutorial3.Sample3Native.onCreate(Sample3Native.java:21)
05-31 23:42:01.999: E/AndroidRuntime(9708): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-31 23:42:01.999: E/AndroidRuntime(9708): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1722)
05-31 23:42:01.999: E/AndroidRuntime(9708): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1784)
05-31 23:42:01.999: E/AndroidRuntime(9708): at android.app.ActivityThread.access00(ActivityThread.java:123)
05-31 23:42:01.999: E/AndroidRuntime(9708): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:939)
05-31 23:42:01.999: E/AndroidRuntime(9708): at android.os.Handler.dispatchMessage(Handler.java:99)
05-31 23:42:01.999: E/AndroidRuntime(9708): at android.os.Looper.loop(Looper.java:130)
05-31 23:42:01.999: E/AndroidRuntime(9708): at android.app.ActivityThread.main(ActivityThread.java:3835)
05-31 23:42:01.999: E/AndroidRuntime(9708): at java.lang.reflect.Method.invokeNative(Native Method)
05-31 23:42:01.999: E/AndroidRuntime(9708): at java.lang.reflect.Method.invoke(Method.java:507)
05-31 23:42:01.999: E/AndroidRuntime(9708): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:864)
05-31 23:42:01.999: E/AndroidRuntime(9708): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:622)
05-31 23:42:01.999: E/AndroidRuntime(9708): at dalvik.system.NativeStart.main(Native Method)
05-31 23:42:01.999: E/AndroidRuntime(9708): Caused by: java.lang.UnsatisfiedLinkError: Couldn't load native_sample: findLibrary returned null
05-31 23:42:01.999: E/AndroidRuntime(9708): at java.lang.Runtime.loadLibrary(Runtime.java:429)
05-31 23:42:01.999: E/AndroidRuntime(9708): at java.lang.System.loadLibrary(System.java:554)
05-31 23:42:01.999: E/AndroidRuntime(9708): at org.opencv.samples.tutorial3.Sample3View.<clinit>(Sample3View.java:48)
05-31 23:42:01.999: E/AndroidRuntime(9708): ... 14 more
I find solutions for the same problem but none of them didn't help me:
我找到了相同问题的解决方案,但没有一个对我没有帮助:
- Native OpenCV Samples for Android throws UnsatisfiedLinkError
- java.lang.UnsatisfiedLinkError: Couldn't load opencv_java: findLibrary returned null
- Major OpenCV-Android for Windows Installing and Running Issues(no answer)
- 适用于 Android 的本机 OpenCV 示例引发 UnsatisfiedLinkError
- java.lang.UnsatisfiedLinkError: 无法加载 opencv_java: findLibrary 返回 null
- 适用于 Windows 的主要 OpenCV-Android 安装和运行问题(无答案)
Also I have tested it on different devices and Android API versions.
此外,我已经在不同的设备和 Android API 版本上对其进行了测试。
My system configuration:
我的系统配置:
astor@astor-K42Jv:~$ uname -a
Linux astor-K42Jv 3.2.0-24-generic-pae #39-Ubuntu SMP Mon May 21 18:54:21 UTC 2012 i686 i686 i386 GNU/Linux
I've been tried to fix this problem for 4 nights (it's my free time :) ), but with no luck. I really need this for my thesis, so any help will be appreciated.
我已经尝试解决这个问题 4 个晚上(这是我的空闲时间:)),但没有运气。我的论文真的需要这个,所以任何帮助将不胜感激。
Update:I have tested this sample on Windows 7 (64), but result is the same.
更新:我已经在 Windows 7 (64) 上测试过这个示例,但结果是一样的。
It seems that this is OpenCV bug.
看来这是 OpenCV 的错误。
Update:Build log:
更新:构建日志:
astor@astor-K42Jv:/opt/eclipse-android/workspace/OpenCV-2.4.0-samples/tutorial-3-native$ ndk-build
Install : libnative_camera_r2.2.0.so => libs/armeabi-v7a/libnative_camera_r2.2.0.so
Install : libnative_camera_r2.3.3.so => libs/armeabi-v7a/libnative_camera_r2.3.3.so
Install : libnative_camera_r3.0.1.so => libs/armeabi-v7a/libnative_camera_r3.0.1.so
Install : libnative_camera_r4.0.0.so => libs/armeabi-v7a/libnative_camera_r4.0.0.so
Install : libnative_camera_r4.0.3.so => libs/armeabi-v7a/libnative_camera_r4.0.3.so
Install : libnative_sample.so => libs/armeabi-v7a/libnative_sample.so
回答by ArtemStorozhuk
Whoohoo!
呜呼!
Finally I found solution for this problem by myself!
最后我自己找到了解决这个问题的方法!
I decided to debug line:
我决定调试行:
System.loadLibrary("native_sample");
To do this I downloaded android source code from Android-SDK and then attached source folder (/opt/android-sdk-linux/sources/android-15)to my project. After this I found that error was:
为此,我从 Android-SDK 下载了 android 源代码,然后将源文件夹(/opt/android-sdk-linux/sources/android-15)附加到我的项目中。在此之后我发现错误是:
Cannot load library: link_image[1936]: 37 could not load needed library 'libopencv_java.so' for 'libhello-jni.so' (load_library[1091]: Library 'libopencv_java.so' not found)
And really this library is not in lib
directory. I don't know why but ndk-build
ignored it. So i decided to copy and load it manualy. For this I copied libopencv_java.so
from /opt/OpenCV-2.4.0/libs/armeabi-v7a
and also edited java code:
实际上这个库不在lib
目录中。我不知道为什么,但ndk-build
忽略了它。所以我决定手动复制和加载它。为此,我复制libopencv_java.so
从/opt/OpenCV-2.4.0/libs/armeabi-v7a
和也编辑Java代码:
static {
System.loadLibrary("opencv_java"); //load opencv_java lib
System.loadLibrary("native_sample");
}
Actually similar problems are:
其实类似的问题是:
From second solution I found that I can load libraries using dlopen, but I haven't tried it yet.
从第二个解决方案中,我发现我可以使用dlopen加载库,但我还没有尝试过。
So I will write simple bash-script that will do it (just copy) for myself.
所以我会写一个简单的 bash 脚本来为自己做(只是复制)。
Thanks to all.
谢谢大家。
回答by ahmad raza
Instead of loading your native library as
而不是将您的本机库加载为
static{
System.loadLibrary("YOUR_LIBRARY");
}
load your library after opencv manager is connected in "onManagerConnected" method in you "BaseLoaderCallBack". Following is my code snippet working for me
在您的“BaseLoaderCallBack”中的“onManagerConnected”方法中连接opencv管理器后加载您的库。以下是我的代码片段为我工作
public void onManagerConnected(int status) {
switch(status){
case LoaderCallbackInterface.SUCCESS:
Toast.makeText(getApplicationContext(), "manager connected", Toast.LENGTH_LONG).show();
System.loadLibrary("MYNATIVELIB");
break;
default:
super.onManagerConnected(status);
break;
}
}
回答by user426364
You're at a higher level than the actual problem. See "Getting started with the NDK", when you run
您处于比实际问题更高的级别。请参阅“入门NDK”,当您运行
cd <project>
<ndk>/ndk-build
... what does it say? (Remember to use the cygwin window and not a dos prompt).
... 它说什么?(记住使用 cygwin 窗口而不是 dos 提示)。