注意OpenCV 4.0.1 解决了这个问题请直接下载OpenCV 4.0.1
但是OpenCV 4.0.1作为模块导入Android Studio会有找不到R.styleable的问题
OpenCV4Android支持三种使用方式
1.Java层调用
2.NDK调用动态库(方便,但是会包含整个.so库)
3.NDK调用静态库(灵活,比如没有用到机器学习模块,libopencv_ml.a里的代码是不会打包到动态库里的)
通过链接静态库的方式使用OpenCV4Android,CMAKE配置如下
#############################prepare for using of opencv static library#################### #first step to use opencv set(OpenCV_STATIC ON) #second step to use opencv set(OpenCV_DIR C:/sdk/opencv-3.4.2-android-sdk/sdk/native/jni) #third step to use opencv find_package(OpenCV REQUIRED) ########################################################################################### ##################################################the native-lib shared library############ # 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) # 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 #foutth step to use opencv link opencv ${OpenCV_LIBS} # Links the target library to the log library # included in the NDK. ${log-lib}) ######################################################################################
仅仅是新建项目稍稍修改了native-lib.cpp的代码,在函数里用了下opencv的函数
#include <jni.h> #include <string> #include "opencv2/core/core.hpp" #include "opencv2/imgproc/imgproc_c.h" extern "C" JNIEXPORT jstring JNICALL Java_com_example_useprebuiltstaticlibrary_MainActivity_stringFromJNI( JNIEnv *env, jobject /* this */) { std::string hello="hello"; cv::Mat m=cv::Mat::eye(3,3,CV_8UC1); return env->NewStringUTF(hello.c_str()); }
编译时却报错
Build command failed. Error while executing process C:\sdk\cmake\3.6.4111459\bin\cmake.exe with arguments {--build C:\sdk\demo\usePrebuiltStaticLibrary\app\.externalNativeBuild\cmake\debug\armeabi-v7a --target native-lib} [1/2] Building CXX object CMakeFiles/native-lib.dir/src/main/cpp/native-lib.cpp.o [2/2] Linking CXX shared library ..\..\..\..\build\intermediates\cmake\debug\obj\armeabi-v7a\libnative-lib.so FAILED: cmd.exe /C "cd . && C:\sdk\ndk-bundle\toolchains\llvm\prebuilt\windows-x86_64\bin\clang++.exe --target=armv7-none-lin