Add OpenCV to native C code through cmake on Android studio

I tried to include opencv in my native C code through cmake. I did some research on the Internet, downloaded the findopencv.cmake file from the Internet and added it to the app directory of my android project. This is also the location of cmakelists.txt. I use this tutorial: https://www.learn2crack.com/2016/03/setup-opencv-sdk-android-studio.html Import opencv as a module into In my android studio project, when I run:

if(!OpenCVLoader.initDebug()){
   System.out.println("Opencv not loaded");
} else {
   System.out.println("Opencv loaded");
}

I know opencv has been loaded

However, because I'm trying to add OpenCV to my native code instead of Java code, I don't think I can use it. This is the cmakelists I now have:

# Sets the minimum version of CMake required to build your native library.
# This ensures that a certain set of CMake features is available to
# your build.

cmake_minimum_required(VERSION 3.4.1)

set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} FindOpenCV.cmake)

# Specifies a library name, specifies whether the library is STATIC or
# SHARED, and provides relative paths to the source code. You can
# define multiple libraries by adding multiple add.library() commands,
# and CMake builds them for you. When you build your app, Gradle
# automatically packages shared libraries with your APK.

add_library(# Specifies the name of the library.
        apriltag

        # Sets the library as a shared library.
        SHARED

        # Provides a relative path to your source file(s).
        src/main/apriltag/apriltag.c
        src/main/apriltag/apriltag_jni.c
        src/main/apriltag/apriltag_quad_thresh.c
        src/main/apriltag/common/g2d.c
        src/main/apriltag/common/getopt.c
        src/main/apriltag/common/homography.c
        src/main/apriltag/common/image_f32.c
        src/main/apriltag/common/image_u8.c
        src/main/apriltag/common/image_u8x3.c
        src/main/apriltag/common/matd.c
        src/main/apriltag/common/pnm.c
        src/main/apriltag/common/string_util.c
        src/main/apriltag/common/svd22.c
        src/main/apriltag/common/time_util.c
        src/main/apriltag/common/unionfind.c
        src/main/apriltag/common/workerpool.c
        src/main/apriltag/common/zarray.c
        src/main/apriltag/common/zhash.c
        src/main/apriltag/common/zmaxheap.c
        src/main/apriltag/tag16h5.c
        src/main/apriltag/tag25h7.c
        src/main/apriltag/tag25h9.c
        src/main/apriltag/tag36artoolkit.c
        src/main/apriltag/tag36h10.c
        src/main/apriltag/tag36h11.c
        )

STRING(REPLACE "-O0" "-O4" CMAKE_C_FLAGS_DEBUG ${CMAKE_C_FLAGS_DEBUG})
STRING(REPLACE "-O2" "-O4" CMAKE_C_FLAGS_RELEASE ${CMAKE_C_FLAGS_RELEASE})

include_directories(src/main/apriltag/)
include_directories(${OpenCV_INCLUDE_DIRS})

find_package(OpenCV required)

find_library(log-lib log)
find_library(jnigraphics-lib jnigraphics)
target_link_libraries(apriltag ${log-lib} ${jnigraphics-lib})

The following are the errors encountered when building gradle:

By not providing "FindOpenCV.cmake" in CMAKE_MODULE_PATH this project has 
asked CMake to find a package configuration file provided by "OpenCV", but 
CMake did not find one. 
Could not find a package configuration file provided by "OpenCV" with any of 
the following names:
OpenCVConfig.cmake
opencv-config.cmake
Add the installation prefix of "OpenCV" to CMAKE_PREFIX_PATH or set 
"OpenCV_DIR" to a directory containing one of the above files. If "OpenCV" 
provides a separate development package or SDK, be sure it has been 
installed.

So my question is:

>Can I use the imported opencv, or do I have to download different opencv and store it elsewhere? > What do I need to change in cmakelists.txt to build my gradle?

Ideally, I want to build and be able to add #include < opencv2 / OpenCV. HPP > and use the namespace CV to my C file and add functions using the opencv function

Thanks for your help!

resolvent:

Update 22 – May 18: added missing step 6

Update October 10 – 17: the new solution uses cmake and Android gradle plug-in 2.3.1 to correctly integrate opencv into the application. Test it with Android studio 2.3.1

Update November 5-17: provides another solution

There are two ways to include OpenCV

Git / Simpler Way

visit https://github.com/ahasbini/Android-OpenCV Learn more

Manual / advanced mode

To include the opencv Library in the Android studio project, it is best to create a new library module in the project and migrate the files in the opencv Android SDK package to it:

>Select File > new module to create a new module. > select "Android library" and enter the details:

>Library name: opencv > module name: opencv > package name: org.opencv

>After creating a new module, the path_ to_ opencv_ Copy the contents of the SDK / SDK / Java / SRC directory to path_ to_ your_ Project / opencv / SRC / main / Java. > under main, create the following direct path: Aidl / org / opencv / engine and move main / Java / org / opencv / engine / opencvengineinterface.aidl into it. > move path_ to_ opencv_ Copy the contents of SDK / SDK / Java / res to path_ to_ your_ Project / opencv / SRC / main / res. > in path_ to_ your_ Create SDK folder in project / opencv / SRC / and add path_ to_ opencv_ Copy the SDK / SDK / native folder to it. > in the opencv module, create the cmakelists.txt file and add the following lines in the following order:

cmake_minimum_required(VERSION 3.4.1)
set(OpenCV_DIR "src/sdk/native/jni")
find_package(OpenCV required)
message(STATUS "OpenCV libraries: ${OpenCV_LIBS}")
include_directories(${OpenCV_INCLUDE_DIRS})

>In the opencv module, edit the build.gradle file:

...
android {
    ...

    defaultConfig {
        minSdkVersion 8
        targetSdkVersion 25
        versionCode 3200
        versionName "3.2.0"

        ...

        externalNativeBuild {
            cmake {
                cppFlags "-frtti -fexceptions"
            }
        }
    }
    buildTypes {
        ...
    }
    externalNativeBuild {
        cmake {
            path "CMakeLists.txt"
        }
    }
    sourceSets {
        main {
            jni.srcDirs = [jni.srcDirs, 'src/sdk/native/jni/include']
            jniLibs.srcDirs = [jniLibs.srcDirs, 'src/sdk/native/3rdparty/libs', 'src/sdk/native/libs']
        }
    }
}
...

>In the application (Application module, possibly another name) module, create / edit the cmakelists.txt file and add the following lines in the following order (note the different paths set to opencv_dir):

set(OpenCV_DIR "../opencv/src/sdk/native/jni")
find_package(OpenCV required)
message(STATUS "OpenCV libraries: ${OpenCV_LIBS}")
target_link_libraries(YOUR_TARGET_LIB ${OpenCV_LIBS})

>In the application (Application module, possibly another name) module, edit the build.gradle file:

...    
android {
    ...
    defaultConfig {
        ...
        externalNativeBuild {
            cmake {
                cppFlags "-frtti -fexceptions"
        }
    }
    buildTypes {
        ...
    }
    externalNativeBuild {
        cmake {
            path "CMakeLists.txt"
        }
    }
}

dependencies {
    ...
    compile project(':opencv')
}

>Perform gradle synchronization, which now includes opencv native library, header file and Java wrapper class

When you build the project and start APK, you can_ to_ project / path_ to_ app_ Check the packaged APK under module / build / output / (drag APK to the text editor tab of Android studio)

You should see libopencv under each ABI architecture folder_ java3.so.

Initialize opencv SDK in Java class:

public class MyClass {

    static {
        if (BuildConfig.DEBUG) {
            OpenCVLoader.initDebug();
        }
    }

    ...
}

You should see in the logcat message that the specified opencv is loaded (the first error is normal):

05-10 10:42:31.451 D/OpenCV/StaticHelper: Trying to get library list
05-10 10:42:31.452 E/OpenCV/StaticHelper: OpenCV error: Cannot load info library for OpenCV
05-10 10:42:31.452 D/OpenCV/StaticHelper: Library list: ""
05-10 10:42:31.452 D/OpenCV/StaticHelper: First attempt to load libs
05-10 10:42:31.452 D/OpenCV/StaticHelper: Trying to init OpenCV libs
05-10 10:42:31.452 D/OpenCV/StaticHelper: Trying to load library opencv_java3
05-10 10:42:32.031 D/OpenCV/StaticHelper: Library opencv_java3 loaded
05-10 10:42:32.031 D/OpenCV/StaticHelper: First attempt to load libs is OK
05-10 10:42:32.045 I/OpenCV/StaticHelper: General configuration for OpenCV 3.2.0 =====================================
05-10 10:42:32.045 I/OpenCV/StaticHelper:   Version control:               3.2.0
05-10 10:42:32.045 I/OpenCV/StaticHelper:   Platform:
05-10 10:42:32.045 I/OpenCV/StaticHelper:     Timestamp:                   2016-12-23T13:04:49Z
05-10 10:42:32.045 I/OpenCV/StaticHelper:     Host:                        Linux 4.8.0-25-generic x86_64
05-10 10:42:32.045 I/OpenCV/StaticHelper:     Target:                      Linux 1 x86_64
05-10 10:42:32.045 I/OpenCV/StaticHelper:     CMake:                       2.8.12.2
05-10 10:42:32.045 I/OpenCV/StaticHelper:     CMake generator:             Ninja
05-10 10:42:32.045 I/OpenCV/StaticHelper:     CMake build tool:            /usr/bin/ninja
05-10 10:42:32.045 I/OpenCV/StaticHelper:     Configuration:               Release
05-10 10:42:32.045 I/OpenCV/StaticHelper:   C/C++:
05-10 10:42:32.045 I/OpenCV/StaticHelper:     Built as dynamic libs?:      NO
05-10 10:42:32.045 I/OpenCV/StaticHelper:     C++ Compiler:                /usr/bin/ccache /opt/android/android-ndk-r10e/toolchains/x86_64-4.9/prebuilt/linux-x86_64/bin/x86_64-linux-android-g++ (ver 4.9)

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>