前言
最近研究在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++.png
image.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'}

配置完成后开始我们项目之旅项目源码稍后提交

更多相关文章

  1. Android非Kotlin支持项目添加Kotlin代码时的注意点
  2. android模拟器SD卡使用
  3. 推送sdk (类似百度 推送 ) android mina (待优化... )
  4. Android(安卓)NDK移植libiconv和libxml2
  5. android 项目串口通信(serialPort)开发遇到的炕
  6. android 开源 OCR 项目 及手写识别[转]
  7. Android(安卓)DataBinding的简单使用
  8. Android下pm 命令详解
  9. NPM 和webpack 的基础使用

随机推荐

  1. Android源码下载出现的问题
  2. SpannableString的使用方法
  3. 重磅出击,Google Android经典资料!
  4. Android的加速度传感器模拟摇一摇的效果-
  5. Android核心模块
  6. Android之基本样式和布局
  7. android gradle 自动动态构建多平台的包
  8. 二.Android项目结构
  9. Configuration类简介
  10. APK安装路径移动至外部存储设备