OpenCV4Android支持三種使用方式1.Java層調用2.NDK調用動態庫(方便,但是會包含整個.so庫)3.NDK調用靜態庫(靈活,比如沒有用到機器學習模塊,libopencv_ml.a里的代碼是不會打包到動態庫里的)通過鏈接靜態庫的方式使用OpenCV4Android,CMAKE配置如下 ...
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-linux-androideabi23 --gcc-toolchain=C:/sdk/ndk-bundle/toolchains/arm-linux-androideabi-4.9/prebuilt/windows-x86_64 --sysroot=C:/sdk/ndk-bundle/sysroot -fPIC -isystem C:/sdk/ndk-bundle/sysroot/usr/include/arm-linux-androideabi -g -DANDROID -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -march=armv7-a -mfloat-abi=softfp -mfpu=vfpv3-d16 -mthumb -mfpu=neon -Wa,--noexecstack -Wformat -Werror=format-security -std=c++11 -std=c++11 -O0 -fno-limit-debug-info -Wl,--exclude-libs,libgcc.a -Wl,--exclude-libs,libatomic.a -nostdlib++ --sysroot C:/sdk/ndk-bundle/platforms/android-23/arch-arm -Wl,--build-id -Wl,--warn-shared-textrel -Wl,--fatal-warnings -Wl,--fix-cortex-a8 -Wl,--exclude-libs,libunwind.a -LC:/sdk/ndk-bundle/sources/cxx-stl/llvm-libc++/libs/armeabi-v7a -Wl,--no-undefined -Wl,-z,noexecstack -Qunused-arguments -Wl,-z,relro -Wl,-z,now -shared -Wl,-soname,libnative-lib.so -o ..\..\..\..\build\intermediates\cmake\debug\obj\armeabi-v7a\libnative-lib.so CMakeFiles/native-lib.dir/src/main/cpp/native-lib.cpp.o C:/sdk/opencv-3.4.2-android-sdk/sdk/native/staticlibs/armeabi-v7a/libopencv_highgui.a C:/sdk/opencv-3.4.2-android-sdk/sdk/native/staticlibs/armeabi-v7a/libopencv_features2d.a C:/sdk/opencv-3.4.2-android-sdk/sdk/native/staticlibs/armeabi-v7a/libopencv_shape.a C:/sdk/opencv-3.4.2-android-sdk/sdk/native/staticlibs/armeabi-v7a/libopencv_imgcodecs.a C:/sdk/opencv-3.4.2-android-sdk/sdk/native/staticlibs/armeabi-v7a/libopencv_ml.a C:/sdk/opencv-3.4.2-android-sdk/sdk/native/staticlibs/armeabi-v7a/libopencv_videoio.a C:/sdk/opencv-3.4.2-android-sdk/sdk/native/staticlibs/armeabi-v7a/libopencv_dnn.a C:/sdk/opencv-3.4.2-android-sdk/sdk/native/staticlibs/armeabi-v7a/libopencv_flann.a C:/sdk/opencv-3.4.2-android-sdk/sdk/native/staticlibs/armeabi-v7a/libopencv_objdetect.a C:/sdk/opencv-3.4.2-android-sdk/sdk/native/staticlibs/armeabi-v7a/libopencv_core.a C:/sdk/opencv-3.4.2-android-sdk/sdk/native/staticlibs/armeabi-v7a/libopencv_calib3d.a C:/sdk/opencv-3.4.2-android-sdk/sdk/native/staticlibs/armeabi-v7a/libopencv_video.a C:/sdk/opencv-3.4.2-android-sdk/sdk/native/staticlibs/armeabi-v7a/libopencv_superres.a C:/sdk/opencv-3.4.2-android-sdk/sdk/native/staticlibs/armeabi-v7a/libopencv_photo.a C:/sdk/opencv-3.4.2-android-sdk/sdk/native/staticlibs/armeabi-v7a/libopencv_imgproc.a C:/sdk/opencv-3.4.2-android-sdk/sdk/native/staticlibs/armeabi-v7a/libopencv_stitching.a C:/sdk/opencv-3.4.2-android-sdk/sdk/native/staticlibs/armeabi-v7a/libopencv_videostab.a -llog C:/sdk/opencv-3.4.2-android-sdk/sdk/native/3rdparty/libs/armeabi-v7a/liblibprotobuf.a C:/sdk/opencv-3.4.2-android-sdk/sdk/native/staticlibs/armeabi-v7a/libopencv_calib3d.a C:/sdk/opencv-3.4.2-android-sdk/sdk/native/staticlibs/armeabi-v7a/libopencv_features2d.a C:/sdk/opencv-3.4.2-android-sdk/sdk/native/staticlibs/armeabi-v7a/libopencv_highgui.a C:/sdk/opencv-3.4.2-android-sdk/sdk/native/staticlibs/armeabi-v7a/libopencv_videoio.a C:/sdk/opencv-3.4.2-android-sdk/sdk/native/staticlibs/armeabi-v7a/libopencv_imgcodecs.a C:/sdk/opencv-3.4.2-android-sdk/sdk/native/3rdparty/libs/armeabi-v7a/liblibjpeg-turbo.a C:/sdk/opencv-3.4.2-android-sdk/sdk/native/3rdparty/libs/armeabi-v7a/liblibwebp.a C:/sdk/opencv-3.4.2-android-sdk/sdk/native/3rdparty/libs/armeabi-v7a/liblibpng.a C:/sdk/opencv-3.4.2-android-sdk/sdk/native/3rdparty/libs/armeabi-v7a/liblibtiff.a C:/sdk/opencv-3.4.2-android-sdk/sdk/native/3rdparty/libs/armeabi-v7a/liblibjasper.a C:/sdk/opencv-3.4.2-android-sdk/sdk/native/3rdparty/libs/armeabi-v7a/libIlmImf.a C:/sdk/opencv-3.4.2-android-sdk/sdk/native/staticlibs/armeabi-v7a/libopencv_flann.a C:/sdk/opencv-3.4.2-android-sdk/sdk/native/staticlibs/armeabi-v7a/libopencv_video.a C:/sdk/opencv-3.4.2-android-sdk/sdk/native/staticlibs/armeabi-v7a/libopencv_photo.a C:/sdk/opencv-3.4.2-android-sdk/sdk/native/staticlibs/armeabi-v7a/libopencv_imgproc.a C:/sdk/opencv-3.4.2-android-sdk/sdk/native/staticlibs/armeabi-v7a/libopencv_core.a -lz C:/sdk/opencv-3.4.2-android-sdk/sdk/native/3rdparty/libs/armeabi-v7a/libtbb.a -lc C:/sdk/opencv-3.4.2-android-sdk/sdk/native/3rdparty/libs/armeabi-v7a/libcpufeatures.a -ldl -lm -llog C:/sdk/opencv-3.4.2-android-sdk/sdk/native/3rdparty/libs/armeabi-v7a/libtegra_hal.a -latomic -lm "C:/sdk/ndk-bundle/sources/cxx-stl/llvm-libc++/libs/armeabi-v7a/libc++_static.a" "C:/sdk/ndk-bundle/sources/cxx-stl/llvm-libc++/libs/armeabi-v7a/libc++abi.a" "C:/sdk/ndk-bundle/sources/cxx-stl/llvm-libc++/libs/armeabi-v7a/libunwind.a" "-ldl" && cd ." C:/sdk/opencv-3.4.2-android-sdk/sdk/native/staticlibs/armeabi-v7a/libopencv_core.a(system.cpp.o):system.cpp:function std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*) [clone .part.13]: error: undefined reference to 'std::basic_ios<char, std::char_traits<char> >::clear(std::_Ios_Iostate)' C:/sdk/opencv-3.4.2-android-sdk/sdk/native/staticlibs/armeabi-v7a/libopencv_core.a(system.cpp.o):system.cpp:function cv::Exception::formatMessage(): error: undefined reference to 'std::basic_stringstream<char, std::char_traits<char>, std::allocator<char> >::basic_stringstream(std::_Ios_Openmode)' C:/sdk/opencv-3.4.2-android-sdk/sdk/native/staticlibs/armeabi-v7a/libopencv_core.a(system.cpp.o):system.cpp:function cv::Exception::formatMessage(): error: undefined reference to 'std::basic_ostream<char, std::char_traits<char> >& std::__ostream_insert<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*, int)' C:/sdk/opencv-3.4.2-android-sdk/sdk/native/staticlibs/armeabi-v7a/libopencv_core.a(system.cpp.o):system.cpp:function cv::Exception::formatMessage(): error: undefined reference to 'std::basic_ostream<char, std::char_traits<char> >& std::__ostream_insert<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*, int)' C:/sdk/opencv-3.4.2-android-sdk/sdk/native/staticlibs/armeabi-v7a/libopencv_core.a(system.cpp.o):system.cpp:function cv::Exception::formatMessage(): error: undefined reference to 'std::ostream::put(char)' C:/sdk/opencv-3.4.2-android-sdk/sdk/native/staticlibs/armeabi-v7a/libopencv_core.a(system.cpp.o):system.cpp:function cv::Exception::formatMessage(): error: undefined reference to 'std::ostream::flush()' C:/sdk/opencv-3.4.2-android-sdk/sdk/native/staticlibs/armeabi-v7a/libopencv_core.a(system.cpp.o):system.cpp:function cv::Exception::formatMessage(): error: undefined reference to 'std::basic_ostream<char, std::char_traits<char> >& std::__ostream_insert<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*, int)' C:/sdk/opencv-3.4.2-android-sdk/sdk/native/staticlibs/armeabi-v7a/libopencv_core.a(system.cpp.o):system.cpp:function cv::Exception::formatMessage(): error: undefined reference to 'std::basic_ostream<char, std::char_traits<char> >& std::__ostream_insert<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*, int)' C:/sdk/opencv-3.4.2-android-sdk/sdk/native/staticlibs/armeabi-v7a/libopencv_core.a(system.cpp.o):system.cpp:function cv::Exception::formatMessage(): error: undefined reference to 'std::ostream::put(char)'
.
.
.
.
.
.
.
.
.
C:/sdk/opencv-3.4.2-android-sdk/sdk/native/staticlibs/armeabi-v7a/libopencv_core.a(persistence_json.cpp.o):persistence_json.cpp:function icvJSONParseValue(CvFileStorage*, char*, CvFileNode*): error: undefined reference to 'std::string::at(unsigned int)' C:/sdk/opencv-3.4.2-android-sdk/sdk/native/staticlibs/armeabi-v7a/libopencv_core.a(persistence.cpp.o):persistence.cpp:function check_if_write_struct_is_delayed(CvFileStorage*, bool): error: undefined reference to 'std::string::assign(char const*, unsigned int)' clang++.exe: error: linker command failed with exit code 1 (use -v to see invocation) ninja: build stopped: subcommand failed.
google得知是因為編譯這些靜態庫時所用的編譯器、標準庫等跟NDK當前的版本不同。網上的建議都是stl改為gnustl_static或gnustl_shared之類。可較新的NDK都不支持這些stl了,為了使用OpenCV靜態庫只能重新編譯了。
編譯OpenCV4Android的過程里爬了不少坑,總結一下,希望有助於小白們。
1. 安裝MinGW
這裡不詳細的說了,網上很多教程。選擇如圖三項安裝
安裝好後差不多二三百兆,在環境變數Path里添加
C:\MinGW\bin
C:\MinGW\msys\1.0\bin
這裡C:\MinGW是我的安裝目錄。配置好後需要註銷電腦,再登陸。在命令行里gcc -v看到能輸出版本就算差不多設置好環境了。
2. CMAKE 配置
下載好源代碼解壓,我放在C:\opencv-3.4.2。打開CMAKE圖形界面
如圖一定要選擇MinGW Makefiles,我想這也是為什麼一定要先安裝配置MinGW的原因。選擇指定工具鏈,點擊next
這裡一定要註意,選擇的不是源代碼
\opencv-3.4.2\platforms\android\android.toolchain.cmake
這個工具鏈,這個工具鏈過時了。新版本的NDK帶有自己的工具鏈,這裡選擇NDK
/build/cmake/android.toolchain.cmake這個工具鏈。
這裡感謝
https://github.com/opencv/opencv/issues/8742
點擊Finish。有錯誤別管,點擊configure。開始配置。出現錯誤就再點configure直到遇到一個跳不過去的錯誤,看看是不是讓你配置SDK路徑
Android SDK: specify path to Android SDK via ANDROID_SDK_ROOT /
ANDROID_HOME / ANDROID_SDK variables
點擊Add Entry添加變數
註意類型是FilePath。點擊OK。點擊configure。很快又跳出一個錯誤
CMake Error at cmake/android/android_gradle_projects.cmake:1 (message): Android gradle-based build/projects are not supported in this version of OpenCV. You need to downgrade Android SDK Tools to version 25.2.5. Details: https://github.com/opencv/opencv/issues/8460 Call Stack (most recent call first): cmake/android/OpenCVDetectAndroidSDK.cmake:204 (include) CMakeLists.txt:645 (include)
這是SDK比較新,新的SDK只支持Gradle編譯,而OpenCV需要Ant編譯。這裡也給出瞭解決方法
Details: https://github.com/opencv/opencv/issues/8460
就是下載一個較舊的SDK。正如連接所說,windows選擇這個
https://dl.google.com/android/repository/tools_r25.2.5-windows.zip
解壓後發現裡面只有一個tool目錄,而SDK里也有一個tool目錄。我的做法是先重命名SDK里的tool為tool28。再將下載的tool複製進SDK。
點擊configure。(這裡可能還是這個錯誤,這樣就需要刪除C:\opencv-3.4.2\build里所有內容,重啟CMAKE。再來一次。所以這一步最好在CMAKE前就做好。
接下來會遇到這個錯誤
CMake Error at cmake/android/OpenCVDetectAndroidSDK.cmake:184 (message): Android SDK: Can't build Android projects as requested by BUILD_ANDROID_PROJECTS=ON variable. Use BUILD_ANDROID_PROJECTS=OFF to prepare Android project files without building them Call Stack (most recent call first): CMakeLists.txt:645 (include)
如圖搜索這個變數,取消選擇。點擊configure。
配置成功。
如圖搜做tbb選擇兩項,否則不生成libtbb.a庫,我不知這又什麼用,但是預編譯的SDK里有這個。
還有其它的一些變數可以配置,比如添加ANDROID_ABI,否則僅僅生成armeabi-v7a。修改變數、添加變數後記得configure。
配置完成點擊generate,生成完成。CMAKE可關閉了。
3.編譯
命令行進入C:\opencv-3.4.2\build目錄,這裡有很多配置好的文件,輸入mingw32-make,就開始編譯。等好久就成功了。
C:\opencv-3.4.2\build>mingw32-make Scanning dependencies of target gen-pkgconfig [ 0%] Generate opencv.pc [ 0%] Built target gen-pkgconfig Scanning dependencies of target libcpufeatures [ 0%] Building C object 3rdparty/cpufeatures/CMakeFiles/libcpufeatures.dir/cpu-features.c.o [ 0%] Linking C static library ..\lib\armeabi-v7a\libcpufeatures.a [ 0%] Built target libcpufeatures Scanning dependencies of target libjpeg-turbo [ 0%] Building C object 3rdparty/libjpeg-turbo/CMakeFiles/libjpeg-turbo.dir/src/jcapimin.c.o [ 1%] Building C object 3rdparty/libjpeg-turbo/CMakeFiles/libjpeg-turbo.dir/src/jcapistd.c.o [ 1%] Building C object 3rdparty/libjpeg-turbo/CMakeFiles/libjpeg-turbo.dir/src/jccoefct.c.o [ 1%] Building C object 3rdparty/libjpeg-turbo/CMakeFiles/libjpeg-turbo.dir/src/jccolor.c.o [ 1%] Building C object 3rdparty/libjpeg-turbo/CMakeFiles/libjpeg-turbo.dir/src/jcdctmgr.c.o [ 1%] Building C object 3rdparty/libjpeg-turbo/CMakeFiles/libjpeg-turbo.dir/src/jchuff.c.o [ 1%] Building C object 3rdparty/libjpeg-turbo/CMakeFiles/libjpeg-turbo.dir/src/jcinit.c.o [ 1%] Building C object 3rdparty/libjpeg-turbo/CMakeFiles/libjpeg-turbo.dir/src/jcmainct.c.o [ 1%] Building C object 3rdparty/libjpeg-turbo/CMakeFiles/libjpeg-turbo.dir/src/jcmarker.c.o [ 1%] Building C object 3rdparty/libjpeg-turbo/CMakeFiles/libjpeg-turbo.dir/src/jcmaster.c.o . . . . . . . . . [ 99%] Linking CXX executable ..\..\bin\opencv_perf_stitching [ 99%] Built target opencv_perf_stitching Scanning dependencies of target opencv_videostab [100%] Building CXX object modules/videostab/CMakeFiles/opencv_videostab.dir/src/deblurring.cpp.o [100%] Building CXX object modules/videostab/CMakeFiles/opencv_videostab.dir/src/fast_marching.cpp.o [100%] Building CXX object modules/videostab/CMakeFiles/opencv_videostab.dir/src/frame_source.cpp.o [100%] Building CXX object modules/videostab/CMakeFiles/opencv_videostab.dir/src/global_motion.cpp.o [100%] Building CXX object modules/videostab/CMakeFiles/opencv_videostab.dir/src/inpainting.cpp.o [100%] Building CXX object modules/videostab/CMakeFiles/opencv_videostab.dir/src/log.cpp.o [100%] Building CXX object modules/videostab/CMakeFiles/opencv_videostab.dir/src/motion_stabilizing.cpp.o [100%] Building CXX object modules/videostab/CMakeFiles/opencv_videostab.dir/src/optical_flow.cpp.o [100%] Building CXX object modules/videostab/CMakeFiles/opencv_videostab.dir/src/outlier_rejection.cpp.o [100%] Building CXX object modules/videostab/CMakeFiles/opencv_videostab.dir/src/stabilizer.cpp.o [100%] Building CXX object modules/videostab/CMakeFiles/opencv_videostab.dir/src/wobble_suppression.cpp.o [100%] Linking CXX static library ..\..\lib\armeabi-v7a\libopencv_videostab.a [100%] Built target opencv_videostab Scanning dependencies of target opencv_test_videostab [100%] Building CXX object modules/videostab/CMakeFiles/opencv_test_videostab.dir/test/test_main.cpp.o [100%] Building CXX object modules/videostab/CMakeFiles/opencv_test_videostab.dir/test/test_motion_estimation.cpp.o [100%] Linking CXX executable ..\..\bin\opencv_test_videostab [100%] Built target opencv_test_videostab
4.配置庫
生成的庫在
\opencv-3.4.2\build\lib
第三方庫在
\opencv-3.4.2\build\3rdparty\lib
將其複製到
\opencv-3.4.2-android-sdk
相應目錄下替換掉原來的
現在編譯項目則沒有鏈接錯誤了。
參考文章:
1.https://github.com/opencv/opencv/issues/8742
2.https://github.com/opencv/opencv/issues/8460
3.https://blog.csdn.net/xiao19950820/article/details/77119891