美文网首页
Android studio配置Opencv开发环境

Android studio配置Opencv开发环境

作者: 幻梦_ | 来源:发表于2017-12-11 16:17 被阅读0次

    前言
    最近研究在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++11
    set(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'
    }
    

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

    相关文章

      网友评论

          本文标题:Android studio配置Opencv开发环境

          本文链接:https://www.haomeiwen.com/subject/rywiixtx.html