我试图在AndroidStudio中使用本机opencv方法,但是当我尝试在native.lib.cpp中包含opencv软件包时遇到问题。
我已经按照不同的教程尝试解决此问题。最有用的是this one。但是我的问题是我的代码有错误
public static Map<String, String> parseMetadata(String metaString) {
Map<String, String> metadata = new HashMap();
String[] metaParts = metaString.split(";");
for (int i = 0; i < metaParts.length; i++) {
String part = metaParts[i].substring(0, metaParts[i].indexOf("="));
String songname = metaParts[i].substring(metaParts[i].indexOf("'")+1, metaParts[i].lastIndexOf("'"));
metadata.put(part, songname);
}
return metadata;
}
“找不到'opencv2'”
查看图片:https://i.imgur.com/XaIO8mO.png
按照之前的教程(在本thread中也对此进行了介绍),我得到了这些文件。
app build.gradle
#include <opencv2/core.hpp>
CMakeLists.txt
apply plugin: 'com.android.application'
android {
compileSdkVersion 25
buildToolsVersion "25.0.3"
defaultConfig {
applicationId "esp1718.android.deblur"
minSdkVersion 16
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
externalNativeBuild {
cmake {
cppFlags "-std=c++11 -frtti -fexceptions"
abiFilters 'x86', 'x86_64', 'armeabi-v7a', 'arm64-v8a'
}
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
sourceSets {
main {
jniLibs.srcDirs = ['C:/Users/erikb_000/AndroidStudioProjects/DeBlurGit/app/src/main/jniLibs']
}
}
externalNativeBuild {
cmake {
path "CMakeLists.txt"
}
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
compile 'com.android.support:design:25.3.1'
testCompile 'junit:junit:4.12'
compile project(':openCVLibrary342')
}
native-lib.cpp
set(pathToProject C:/Users/erikb_000/AndroidStudioProjects/DeBlurGit)
set(pathToOpenCV C:/Users/erikb_000/Desktop/Erik/Università/EmbeddedSystemsProgramming/Project/OpenCV-android-sdk)
# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html
# Sets the minimum version of CMake required to build the native library.
cmake_minimum_required(VERSION 3.4.1)
set(CMAKE_VERBOSE_MAKEFILE on)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++11")
include_directories(${pathToOpenCV}/sdk/native/jni/include)
# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds them for you.
# Gradle automatically packages shared libraries with your APK.
add_library( # Sets the name of the library.
native-lib
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
src/main/cpp/native-lib.cpp )
add_library(lib_opencv SHARED IMPORTED)
set_target_properties(lib_opencv PROPERTIES IMPORTED_LOCATION ${pathToProject}/app/src/main/jniLibs/${ANDROID_ABI}/libopencv_java342.so)
# Searches for a specified prebuilt library and stores the path as a
# variable. Because CMake includes system libraries in the search path by
# default, you only need to specify the name of the public NDK library
# you want to add. CMake verifies that the library exists before
# completing its build.
find_library( # Sets the name of the path variable.
log-lib
# Specifies the name of the NDK library that
# you want CMake to locate.
log )
# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in this
# build script, prebuilt third-party libraries, or system libraries.
target_link_libraries( # Specifies the target library.
native-lib
lib_opencv
# Links the target library to the log library
# included in the NDK.
${log-lib} )
请注意,与我发现的教程不同,在gradle文件中,该行
#include <jni.h>
#include <string>
#include <opencv2/core.hpp>
extern "C" {
JNIEXPORT jstring JNICALL
Java_esp1718_android_deblur_MainActivity_stringFromJNI(
JNIEnv *env,
jobject /* this */) {
std::string hello = "Hello from C++";
return env->NewStringUTF(hello.c_str());
}
jstring
Java_esp1718_android_deblur_MainActivity_validate(JNIEnv *env, jlong addGray, jlong addrRgba) {
cv::Rect();
cv::Mat();
std::string hello2 = "Hello from validate";
return env->NewStringUTF(hello2.c_str());
}
}
我不得不删除abiFilters 'x86', 'x86_64', 'armeabi', 'armeabi-v7a', 'arm64-v8a', 'mips', 'mips64'
,因为AndroidStudio给我一些错误,表明它们不再受支持。
我还在另一个线程中找到了解决此导入问题的“解决方案”,据说可以从gradle文件中删除这些行
'armeabi' 'mips' 'mips64'
它实际上解决了externalNativeBuild {
cmake {
path "CMakeLists.txt"
}
}
问题,但是当我尝试调用本机方法时,又在mainActivity上生成了另一个问题。因此,它没有太大的意义。特别要看这张图片:https://i.imgur.com/UwIWcMm.png
我的文件有什么问题吗?在youtube教程的评论中,我注意到其他人也遇到了同样的问题,但是还没有人找到解决方案。