通过ndk编译opencv

通过ndk编译opencv

1. 环境要求

  • Android studio >= 2.3.3
  • Cmake
  • ndk14
  • opencv2.4.13 or opencv3.3

2. 配置properties

  1. 在local.properties 文件中配置opencv sdk 路径
opencv.dir = ${opencv_sdk_dir}
ndk.dir=/Users/king/Library/Android/ndk14
sdk.dir=/Users/king/Library/Android/sdk
opencv.dir=/Users/king/Libs/OpenCV2413
  1. 在build.gradle中配置cmake参数
externalNativeBuild {
     cmake {
           cppFlags "-std=c++11 -frtti -fexceptions"
           arguments "-DOpenCV_DIR=" + getOpenCVDir().toString()+"/sdk/native/jni", "-DANDROID_ARM_NEON=TRUE"
      }
}
ndk {
      abiFilters  "armeabi-v7a"   //, "arm64-v8a","armeabi",
}
...
def getOpenCVDir() {
    Properties properties = new Properties()
    properties.load(new File(rootDir.absolutePath + "/local.properties").newDataInputStream())
    def externalModuleDir = properties.getProperty('opencv.dir', null)
    if (externalModuleDir == null) {
        throw new GradleException(
                "OpenCV location not found. Define location with opencv.dir in the local.properties file!")
    }
    return externalModuleDir
}

3. 配置CMakeLists.txt

cmake_minimum_required(VERSION 3.4.1)

SET(PROJECT_NAME OpenCVLib)

SET(OpenCV_FOUND true )
FIND_PACKAGE(OpenCV REQUIRED)


# build application's shared lib
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++11")

add_library(
     ${PROJECT_NAME}
     SHARED
     src/main/cpp/OpenCVLib.cpp)

#target_include_directories(OpenCVLib PRIVATE)

target_link_libraries(
      ${PROJECT_NAME}
      ${OpenCV_LIBS}
      ${log-lib})

获取示例代码

github项目地址

你可能感兴趣的:(java,android)