Android studio配置Opencv开发环境
16lz
2021-01-23
前言
最近研究在Android移动设备上进行OpenCV开发, 遇到很多坑,终于成功搭建环境,现在跟大家分享一下成果,希望为大家减少不必要的困惑和麻烦。
开发工具配置
Android studio 版本2.3.3
下载地址:http://www.android-studio.org/
OpenCV 3.3
下载地址:https://opencv.org/opencv-3-3.html
开发opencv需要Android NDK 大家可以自行下载NDK库并配置好环境变量
新建一个studio项目 注意一定要勾选支持C++
勾选支持C++.pngimage.png
配置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)#工程路径#OpenCV-android-sdk路径#修改为你的OpenCV-SDK目录set(pathToOpenCv /Users/hzzj/Desktop/SDL/Opencv/OpenCV-android-sdk)#CMake版本信息cmake_minimum_required(VERSION 3.4.1)#支持-std=gnu++11set(CMAKE_VERBOSE_MAKEFILE on)set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++11")#配置加载native依赖->引入OpenCV头文件include_directories(${pathToOpenCv}/sdk/native/jni/include)#动态方式加载#引入libopencv_java3.so文件add_library( opencv_java3 SHARED IMPORTED)set_target_properties(opencv_java3 PROPERTIES IMPORTED_LOCATION ../../../../src/main/jniLibs/armeabi/libopencv_java3.so)# 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 opencv_java3 jnigraphics # Links the target library to the log library # included in the NDK. ${log-lib} )
配置build.gradle
apply plugin: 'com.android.application'android { compileSdkVersion 25 buildToolsVersion "27.0.1" defaultConfig { applicationId "com.hzzj.opencv.demo" minSdkVersion 15 targetSdkVersion 26 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" externalNativeBuild { cmake { cppFlags "-frtti -fexceptions" abiFilters 'armeabi' //添加平台架构类型 } } } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } 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:26.+' compile 'com.android.support.constraint:constraint-layout:1.0.2' testCompile 'junit:junit:4.12'}
配置完成后开始我们项目之旅项目源码稍后提交
更多相关文章
- windows8.1下android开发环境搭建(Eclipse+Android sdk+ADT+Geny
- mac上Android Studio中adb的环境变量配置
- Eclipse 环境下安装PhoneGap开发插件
- android 开源 OCR 项目 及手写识别[转]
- android 项目串口通信(serialPort)开发遇到的炕
- Android非Kotlin支持项目添加Kotlin代码时的注意点
- android 打开预览不同类型文件
- Android中使用PULL方式解析XML文件深入介绍
- Android使用文件管理器打开指定文件夹,浏览里面的内容