Get the App
SLTechnology News&Howtos  ›  Development  › 

How to integrate OpenCV with Android Studio

Shulou Source: shulou.com Published: 2022-06-02 05:07:57 10月14日 Update

This article is about how Android Studio integrates OpenCV. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

My environment:

Android Studio Arctic Fox + JDK11 + Gradle 7.0.2 + NDK21 + opencv-4.5.1-android-sdk

Code link:

Build Demo in OpenCV environment

1. Create a Native project

two。 Download OpenCV SDK

OpenCV SDK

3. Create a new libs folder under the cpp directory of Android Studio

Copy the whole OpenCV SDK under the folder "OpenCV-android-sdk\ sdk\ native\ libs" we downloaded to the libs folder just now.

4. Copy the entire include folder under the "OpenCV-android-sdk\ sdk\ native\ jni" path to the cpp folder.

5. Configure build.gradle

6. Configure CMakelists.txt

# the purpose is to let CMake find out where our header file is. Here is include_directories (include) # set the system variable under the cpp/include folder. The goal is to let CMake know where to find our libopencv_java4.so library set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}-L ${CMAKE_SOURCE_DIR} / libs/$ {ANDROID_ABI}") target_link_libraries (myopencv opencv_java4 # link libopencv_java4.so, so that this so ${log-lib} will be found in the generated APK)

7. Click build and find the library we introduced in the generated apk

The next thing is that the runtime will report an error.

Java.lang.UnsatisfiedLinkError: dlopen failed: library "libc++_shared.so" not found

The reason is that before NDK18 was compiled by gcc, NDK18 was later compiled by clang, and NDK18 no longer has the library libc++_shared.so, so an error will be reported at run time.

Solution:

Add a sentence to build.gradle

Arguments'- DANDROID_STL=c++_shared'

In this way, the library libc++_shared.so can be obtained remotely

After recompiling, we found that there is libc++_shared.so in apk, and apk can run normally.

Thank you for reading! This is the end of the article on "how to integrate Android Studio with OpenCV". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

Tags: Files folders runs content is in more environment directory purpose article link will report generate compile configure good practical next code method Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia Xiaomi Redmi Linux macOS