我正在开发一个Android应用程序,它应该检测一个面部是否在相机前面,然后根据它执行一些操作。我正在使用open cv进行面部检测,但我需要一些服装cpp功能。所以我试图将open cv stuff导入到我自己的cpp文件myLib.cpp中。然后我想在mainActivity中调用myLib中定义的函数。
当我构建项目时,一切正常,但只要我在我的设备上运行它(Oneplus x - Android 22),它会立即崩溃,并显示以下错误消息。
04-12 10:28:38.494 11114-11114/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.lunaticcoding.opencvtest, PID: 11114
java.lang.UnsatisfiedLinkError: dlopen failed: library "libopencv_java3.so" not found
at java.lang.Runtime.loadLibrary(Runtime.java:371)
at java.lang.System.loadLibrary(System.java:988)
at com.lunaticcoding.opencvtest.MainActivity.<clinit>(MainActivity.java:19)
at java.lang.reflect.Constructor.newInstance(Native Method)
at java.lang.Class.newInstance(Class.java:1606)
at android.app.Instrumentation.newActivity(Instrumentation.java:1066)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2246)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2424)
at android.app.ActivityThread.access$900(ActivityThread.java:155)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1323)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:139)
at android.app.ActivityThread.main(ActivityThread.java:5298)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:950)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:745)
我的opencv build gradle
apply plugin: 'com.android.library'
android {
compileSdkVersion 27
defaultConfig {
minSdkVersion 15
targetSdkVersion 27
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
externalNativeBuild {
cmake {
cppFlags "-frtti -fexceptions"
abiFilters 'x86', 'x86_64', 'armeabi', 'armeabi-v7a', 'arm64-v8a', 'mips', 'mips64'
}
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
externalNativeBuild {
cmake {
path "CMakeLists.txt"
}
}
sourceSets {
main {
jni.srcDirs = [jni.srcDirs, 'src/sdk/native/jni/include']
jniLibs.srcDirs = [jniLibs.srcDirs, 'src/sdk/native/3rdparty/libs', 'src/sdk/native/libs']
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:27.1.1'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
}
我的Opencv cMakeLists.txt
cmake_minimum_required(VERSION 3.4.1)
set(OpenCV_DIR "/Users/lunaticcoding/Documents/OpenCV-android-sdk/sdk/native/jni")
find_package(OpenCV REQUIRED java)
message(STATUS "OpenCV libraries: ${OpenCV_LIBS}")
include_directories(${OpenCV_INCLUDE_DIRS})
问题是libopencv_java3.so没有在我的构建中创建。所以问题必须出在opencv项目的cmakelists中。有谁知道如何将libopencv_java3.so文件导出到我的应用程序构建中?
答案 0 :(得分:0)
您必须在设备上安装OpenCV Manager。
并像这样发起经理
OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_6, this, mLoaderCallback);
BaseLoaderCallback是
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;
}
}
};