Issue
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.
I'd appreciate some input, clearly, I've missed a step. I'm new to android development so any help would be appreciated.
Solution
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.
You can find a detailed tutorial with pictures at this link: https://kcwong-joe.medium.com/how-to-import-opencv-4-5-2-in-android-studio-d9114179628f
Answered By - JOE
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.