首页 > 解决方案 > Android Studio:在 native-lib.cpp 中包含 opencv2

问题描述

我正在尝试在 AndroidStudio 中使用本机 opencv 方法,但是当我尝试将 opencv 包包含在 native.lib.cpp 中时出现问题。

我已经按照不同的教程尝试解决这个问题。最有用的是这个。但我的问题是我的代码有错误

 #include <opencv2/core.hpp>

“找不到‘opencv2’”

见图片:https ://i.imgur.com/XaIO8mO.png

按照上一个教程,也就是在这个线程中描述的,我得到了这些文件。

应用程序构建.gradle

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')
}

CMakeLists.txt

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} )

本机 lib.cpp

#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());
}

}

请注意,与我发现的教程不同,在 gradle 文件中, abiFilters 'x86', 'x86_64', 'armeabi', 'armeabi-v7a', 'arm64-v8a', 'mips', 'mips64'我不得不删除的那一行,'armeabi' 'mips' 'mips64'因为 AndroidStudio 给了我一些错误,说它们不再受支持。

我还在另一个线程中找到了这个导入问题的“解决方案”,说要从 gradle 文件中删除这些行

externalNativeBuild {
    cmake {
        path "CMakeLists.txt"
    }
}

它实际上解决了这个 #include <opencv2/core.hpp>问题,但是当我尝试调用本机方法时,在我的 mainActivity 上生成了另一个问题。所以它没有太多exaustive。特别看这张图:https ://i.imgur.com/UwIWcMm.png

我的文件有什么问题吗?在 youtube 教程评论中,我注意到其他人也面临同样的问题,但还没有人找到解决方案。

标签: androidopencvnative

解决方案


推荐阅读