UnforgivenD
UnforgivenD

Reputation: 35

Android Studio - App dependency on module, (OpenCv module)

I'm struggling to add OpenCV to my project. I've added the module to my project (File -> New -> Import-Module) and added the java folder, renaming it to OpenCV (https://sourceforge.net/projects/opencvlibrary/files/4.5.2/)

I then synced the gradel folder for the Module but when I try and make my app dependent on opencv module I cannot find it.

enter image description here

I'd appreciate some input, clearly, I've missed a step. I'm new to android development so any help would be appreciated.

Upvotes: 0

Views: 187

Answers (1)

JOE
JOE

Reputation: 373

OK. You have downloaded the sdk from https://opencv.org/releases/. Then you just have to import it and link it with CMake.

First, you have to create a JNI project. As you know, OpenCV is a C++ library.

Then, import the SDK, File > New > Import Module…

And choose the “sdk” folder in the OpenCV SDK.

Open setting.gradle, add this code to tell the project you have an opencv module.

include "opencv"
project(":opencv").projectDir = file("sdk")

Edit build.gradle which is under the “app” folder to add an OpenCV_DIR parameter in CMake where is your OpenCV native code.

arguments "-DOpenCV_DIR=" + file('../sdk').absolutePath + "/native/jni",
        "-DANDROID_TOOLCHAIN=clang",
        "-DANDROID_STL=c++_shared"
dependences {
...
implementation project(':opencv')
}

Add code in app/src/main/cpp/CMakeLists.txt to tell CMake install the OpenCV module

set(ANDROID_OPENCV_COMPONENTS "opencv_java" CACHE STRING "")
message(STATUS "ANDROID_ABI=${ANDROID_ABI}")
find_package(OpenCV REQUIRED COMPONENTS ${ANDROID_OPENCV_COMPONENTS})

target_link_libraries(${PROJECT_NAME} ${ANDROID_OPENCV_COMPONENTS})

Finish. You can use it now.

Upvotes: 2

Related Questions