QuickBirdEng / opencv-android

Easy way to integrate OpenCv into your Android project via Gradle

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Use in mixed C++/Java projects

critt opened this issue · comments

I have been trying to see if it is possible to use this library in a mixed Java/C++ project using the ndk and cmake. I am under the impression that the ndk and cmake are required in this scenario, even though you guys say you don't need it to use this library. For example, it seems that something like target_link_libraries in CMakeLists.txt is required in order to link opencv libraries with one's own implementation c++ files to provide them opencv dependencies. Or am I missing something?

Example relevant sections of build.grade:

apply plugin: 'com.android.application'

...

android {

    defaultConfig {
        applicationId "x.x.x"
        minSdkVersion 26
        targetSdkVersion 29
        versionCode 1
        versionName "0.0.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        /*externalNativeBuild {
            cmake {
                cppFlags "-lGLESv2 -frtti -fexceptions"
                arguments "-DOpenCV_DIR=" + "~/dev/libs/opencvsdk/410/OpenCV-android-sdk" + "/sdk/native/jni", "-DANDROID_STL=c++_shared" // , "-DANDROID_ARM_NEON=TRUE"
            }
        }*/
    }
    
    ...

    buildTypes {
        
        ...
        
        debug {
            
            ...

            ndk {
                abiFilters 'arm64-v8a', 'armeabi-v7a', 'x86', 'x86_64'
            }
        }
        
    }
    

    ...
    
    externalNativeBuild {
        cmake {
            path file('CMakeLists.txt')
        }
    }


    /*sourceSets {
        main {
            jniLibs.srcDirs = ['libs']
        }
    }*/
}

dependencies {
    implementation fileTree(include: ['*.jar', '.so'], dir: 'libs')
    
    ...

    //opencv
    //implementation project(":opencv")
    implementation 'com.quickbirdstudios:opencv:4.1.0-contrib'
}

Example CMakeLists.txt:

cmake_minimum_required(VERSION 3.4.1)

# Find OpenCV libraries (this is needed for the call to target_link_libraries)
find_package(OpenCV 4.1 REQUIRED core imgproc highgui)

# Make sure we use flags for OpenGL in cpp
set(CMAKE_VERBOSE_MAKEFILE on)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")

# Add implementation cpp files
set(CPP_ROOT src/main/cpp)
set(SRC_FILES ${CPP_ROOT}/common.hpp ${CPP_ROOT}/native-lib.cpp)

set(HUE_ROOT src/main/cpp/hue)
include_directories(${HUE_ROOT}/include)
set(HUE_SRC ${HUE_ROOT}/src)
set(SRC_FILES_HUE ${HUE_SRC}/scanner.cpp ${HUE_SRC}/detector.cpp ${HUE_SRC}/s_locator.cpp ${HUE_SRC}/p_locator.cpp ${HUE_SRC}/u_scanner.cpp ${HUE_SRC}/rd_scanner.cpp ${HUE_SRC}/sc_scanner.cpp)

# Add library using the above files
add_library( native-lib SHARED ${SRC_FILES} ${SRC_FILES_HUE})

# Link the library with OpenCV, android, and log libraries
target_link_libraries( native-lib ${OpenCV_LIBRARIES} android log )

gradle sync output:

ERROR: /path/to/app/CMakeLists.txt : C/C++ localDebug|x86 : CMake Error at /path/to/app/CMakeLists.txt:4 (find_package):
  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" (requested
  version 4.1) 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.
Affected Modules: app

How would something like find_package work in this scenario, or is there a different way this should be done? Wondering if I am missing something. Is this meant to only provide access to the opencv Java classes? Or is there a way to use this library to provide opencv dependencies to c++ files in the project? Thanks for your time!

commented

As far as I understood the question, you want to access your own C++ files that uses OpenCV. You would need to make sure openCV compiles with your C++ files and then you have to write a JNI wrapper for your C++ files so that they can be used in Java.

Yeah I have the JNI wrapper complete, and a bunch of native code with opencv dependencies. From your response, this is the part i'm wondering about:

You would need to make sure openCV compiles with your C++ files

Is it possible do that with this library? The most common way (and the way I am used to) to do this is with CMake, which I described in my question. Is there a way to provide my c++ files their opencv dependencies using this library?

Edit: it doesn't seem to me like there is. And that's fine, because it doesn't seem like this is the intended use of this library. Just hoping beyond hope that somehow it is possible, because adding opencv to a project this way is amazing!

commented

@critt I am afraid you would need to compile opencv yourself for your use case.

@nasirky thought so. Thanks for the library and thanks for the response!

commented

@critt I will close the issue. However, if you need any help when compiling it yourself, fee. free to contact us.