How to add OpenCV lib to AS3.1.2 and NDK 17.0

情到浓时终转凉″ 提交于 2019-12-06 10:12:18

1. Make sure you have Android SDK up to date, with NDK installed

2. Download latest OpenCV SDK for Android from OpenCV.org and decompress the zip file.

3. Create a new Android Studio project

  • Check Include C++ Support

  • Choose empty Activity
  • In C++ Support, you can check -fexceptions and -frtti

4. Import OpenCV library module

  • New -> Import Module
  • Choose the YOUR_OPENCV_SDK/sdk/java folder

  • Unckeck replace jar, unckeck replace lib, unckeck create gradle-style

5. Set the OpenCV library module up to fit your SDK

app/build.gradle

opencv/build.gradle

Edit openCVLibrary/build.gradle to match your app/build.gradle e.g:

compileSdkVersion 27
defaultConfig {
minSdkVersion 19
targetSdkVersion 27
}

6. Add OpenCV module dependency in your app module

File -> Project structure -> Module app -> Dependencies tab -> New module dependency -> choose OpenCV library module

7. Make a jni folder by right clicking on app/src/main and click Change Folder Location box after that rename folder fron jni to jniLibs

  • 1st step

  • 2nd step

  • 3rd step

8. Copy all files from your opencv directory YOUR_OPENCV_SDK/sdk/native/libs that you have downloaded and paste them in jniLibs folder

  • 1st step

  • 2nd step

9. Set the app build.gradle

  • Add abiFilters

    externalNativeBuild {
    cmake {
        cppFlags "-frtti -fexceptions"
        abiFilters 'x86', 'x86_64', 'armeabi-v7a', 'arm64-v8a'
       }
    }
    

10. Configure the CMakeLists.txt file

  • Copy these three lines and paste after the cmake_minimum_required

    include_directories(YOUR_OPENCV_SDK/sdk/native/jni/include)
    add_library( lib_opencv SHARED IMPORTED )
    set_target_properties(lib_opencv PROPERTIES IMPORTED_LOCATION ${CMAKE_CURRENT_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}/libopencv_java3.so)
    

  • Go to the end of CMakeLists.txt and write lib_opencv to the target_link_libraries list

All is done now enjoy coding with opencv...

Assuming you are using Android Studio, then the initial NDK integration is much easier now then it was previously.

You can simply create a new project in Android Studio, specify that you want NDK support in it and just add a basic activity.

Adding OpenCV NDK (i.e. C++ support) to this project is still tricky - as you say most of the guides and instructions on the Android OpenCV pages are eclipse based at the moment. I went through many blogs, guides etc fro OpenCv on Android Studio with NDK and the one which worked for me most recently (this week) and which seems to be being kept up to date, is the one in this answer here:

I would encourage you to try this approach and to share any issues you have in the comments so that it is updated and kept current, as changes in Android and in OpenCV mean its important to have well supported reference instructions.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!