android开发 使用cmake构建jni工程,并添加opencv模块
程序员文章站
2022-06-22 20:32:13
向现有项目添加 C/C++ 代码
1.切换到project试图,在app下,打开build.gradle
在android{}中添加:
externalNat...
向现有项目添加 C/C++ 代码
1.切换到project试图,在app下,打开build.gradle
在android{}中添加: externalNativeBuild { cmake { path "CMakeLists.txt" } }
2.project>>app>>new>>file 创建CMakeLists.txt
cmake_minimum_required(VERSION 3.4.1) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++11") set(ProjectDir /home/cuizhou/codes/AndroidStudioProjects/CuiZhou-JNI-OpenCV-SSD-Mobilenet-Demo) #----------------------------------------------------- # wrapper 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) 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 ) target_link_libraries( # Specifies the target library. native-lib # Links the target library to the log library # included in the NDK. ${log-lib} # lib_opencv)
3.编写java native 方法,并生成头文件
在MainActivity中添加native方法
public native String stringFromJNI();
或者新建类 MyClass.java, 生成头文件
cd project/app/src/main/java javah com.example.MyClass
4.新建cpp,实现jni函数
添加opencv模块从观望上下载android for opencv
把OpenCV-android-sdk/sdk/native/libs拷贝到src/main/目录下,重新命名为jniLibs
在CMakeList中添加
#----------------------------------------------------- # OpenCV set(pathToOpenCv /home/cuizhou/codes/AndroidStudioProjects/OpenCV3.4-android-sdk) include_directories(${pathToOpenCv}/sdk/native/jni/include) add_library(lib_opencv STATIC IMPORTED ) set_target_properties(lib_opencv PROPERTIES IMPORTED_LOCATION ${ProjectDir}/app/src/main/jniLibs/${ANDROID_ABI}/libopencv_java3.so )
在native cpp中可以使用opencv
#include #include #include #include using namespace cv; ... cv::Mat srcImage=cv::Mat::zeros(cv::Size(300,300),CV_8UC1); ...
上一篇: ARP协议的用途
下一篇: iOS开发面试时常问问题大全!