Android: AndroidStudio使用OpenCV-Native
參考文章:https://blog.csdn.net/a390641326/article/details/80495754
1.首先建立一個C++Native工程
參考:https://blog.csdn.net/wishchin/article/details/90205661
在AS中新建一個C++ native工程,默認JNI庫的名稱為native-lib或者其他;
?
2.到opencv官網去下載一個版本的安卓SDK,https://opencv.org/releases/page/3/,解壓到本地文件夾
?
3.點擊File->New->Import Module…把OpenCV-JNI引入到工程中
OpenCV-android-sdk/sdk/native/jni
需要說明的是:在build.gradle(Module: app)的最后添加
在dependencies{}中的最后添加
compile fileTree(dir: "$buildDir/native-libs", include: 'native-libs.jar')4.修改cmake.list文件
修改工程src\app目錄下的CMakeLists.txt文件,如下
# 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)#支持-std=gnu++11 set(CMAKE_VERBOSE_MAKEFILE on) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++11")#工程路徑 set(Project_Path /home/wishchin/Proj/OpenSource/tensorApp/tfMobileNative)# OpenCV Android SDK Path set(OpenCV_Path /home/wishchin/Android/OpenCV-android-sdk/sdk/native)include_directories(${OpenCV_Path}/jni/include) #include_directories(/home/wishchin/Android/OpenCV-android-sdk/sdk/native/jni/include)# 設置include文件夾的地址 #include_directories(${CMAKE_SOURCE_DIR}/src/main/cpp)# 設置opencv的動態庫 #add_library(libopencv_java SHARED IMPORTED) #set_target_properties(libopencv_java PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/libs/${ANDROID_ABI}/libopencv_java.so)# set(OpenCV_STATIC ON)#1 # set(OpenCV_DIR /home/wishchin/Android/OpenCV-android-sdk/sdk/native/jni)#2 # find_package(OpenCV REQUIRED)#3# 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).native-lib.cpp # rec3D.cpp# cameraCalib.cpp# FrameProcess.cpp# markerLocation.cpp# p3pMy.cpp# polynom_solverMy.cppCheckR.cpp# BallTrack.cpp)#動態方式加載 add_library( lib_opencv SHARED IMPORTED )#引入libopencv_java.so文件 #set_target_properties(lib_opencv PROPERTIES IMPORTED_LOCATION ${Project_Path}/app/src/main/jniLibs/${ANDROID_ABI}/libopencv_java.so) set_target_properties(lib_opencv PROPERTIES IMPORTED_LOCATION ${Project_Path}/app/libs/${ANDROID_ABI}/libopencv_java.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 #libopencv_java# 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本人的配置和原文的有一些不一樣,不知道為什么.
Tips:
配置完工程,有一段時間若打開opencv源代碼,發現找不到自身包含.
不用在意這些細節,也不必要在這個問題上浪費時間,AS習慣抽風,過一會就正常了.
?
5.在native.cpp里面寫測試代碼
extern "C" JNIEXPORT jstring JNICALL Java_com_example_tfmobilenative_MainActivity_testCV(JNIEnv *env,jobject /* this */) {std::string hello = "Congratulation,Boy !The native lib is success!";cv::Mat image;//image= cv::Mat::zeros(224,224,CV_8UC1);return env->NewStringUTF(hello.c_str()); }?
6.測試結果
配置OpenCV,測了一天時間,蛋疼.
?
?
總結
以上是生活随笔為你收集整理的Android: AndroidStudio使用OpenCV-Native的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 小班教案《我不哭》
- 下一篇: Java学习之Thymeleaf使用