[FIXED] How can I integrate OpenCV 4.0 into a pure C++ Android NDK project?

Issue

What are the steps necessary for me to integrate,

the latest version of OpenCV

into a pure C++(No Java Code) Android NDK project, such as Android NDK Google tutorial:

Endless-Tunnel
?

official documentation for android integration refers to much older version :OpenCV-2.4 and folder structures are no longer the same.

I am using Android Studio on Linux.

All help is highly Appreciated.

Solution

  1. Download opencv Android package (e.g. opencv-4.0.1-android-sdk) and unpack to, say, ~/android.

  2. To the bottom of CMakeLists.txt, add

    set( OpenCV_DIR "~/android/OpenCV-android-sdk/sdk/native/jni" )
    find_package( OpenCV REQUIRED )
    target_link_libraries(game opencv_java)
    

The package will define the following variables:

OpenCV_LIBS : The list of all imported targets for OpenCV modules.

OpenCV_INCLUDE_DIRS : The list of OpenCV include directories. With CMake >= 2.8.11 you don’t even need to write

include_directories(${OpenCV_INCLUDE_DIRS})

This version of prebuilt OpenCV SDK defines also

OpenCV_VERSION : The version of this OpenCV build: "4.0.1"

OpenCV_ANDROID_NATIVE_API_LEVEL : Minimum required level of Android API: "16".

This means that your app manifest needs minSdkVersion 16 or higher (the original sample needs a fix here).

Instead of the shared library that contains all OpenCV functionality, you can use static libraries (opencv_imgcodecs, opencv_stitching, et al). These static libraries assume the default ANDROID_STL=c++_static.

For best results, use NDK r.18 or r.19.

UPDATE: NDK r.21 works well for opencv 4.3.0.

Answered By – Alex Cohn

Answer Checked By – Gilberto Lyons (Easybugfix Admin)

Leave a Reply

(*) Required, Your email will not be published