This post was originally published on OpenCV.AI
Easier access to OpenCV for Android through Maven Central
In December, we released OpenCV 4.9.0. As part of that release, we made Android builds available through the Maven Central repository for the first time. This work is supported by Arm company, the authors of the computer architecture on which Android and the world’s smartphones are built.
OpenCV is the open-source library for developing and deploying computer vision (CV) workloads. Its availability for Android will make it easier to develop and maintain CV applications for Android systems, lowering the barrier of entry for developers to build high-quality CV algorithms for supporting AI and image processing algorithms.
Managing your application’s OpenCV dependency through Maven Central is simple, with support built into Android Studio and many other tools. Doing so also enables you to easily keep up to date with bug fixes, new features, and performance improvements as new versions of OpenCV are released. The OpenCV team maintains source compatibility for releases, and the update is safe. Using Maven Central also ensures that you get a build of OpenCV configured correctly for performance and for using the correct dependencies.
We are working with Arm on continuous improvements to OpenCV’s performance across the broad reach of Arm-based devices, from sensors to servers and supercomputers. This includes leveraging improvements from algorithmic changes or using new Arm CPU features.
We will update Maven Central with the latest versions of OpenCV as they are released and continue to update our support for Android, ensuring that developers can integrate OpenCV into their workflows alongside other Android functionality.
We would like to thank Arm for supporting this work.
Using OpenCV in your Android application
To start working with OpenCV, you do not need to download the SDK. Just create a new Application in Android Studio (or open an existing one), open your application’s build.gradle of your application where the “android” section is located, and add OpenCV to the dependencies section of the global scope like this:
apply plugin: 'com.android.application' android { namespace 'org.opencv.samples.puzzle15' compileSdkVersion 31 defaultConfig { applicationId "org.opencv.samples.puzzle15" minSdkVersion 21 targetSdkVersion 31 versionCode 301 versionName "3.01" } // … more options here } dependencies { implementation 'org.opencv:opencv:4.9.0' } }
The OpenCV package will be downloaded and linked automatically by Gradle during the application build. Native libraries are also added to APK automatically. You just need to add a run-time call to load the OpenCV native part before the first library usage like this:
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (OpenCVLoader.initLocal()) { Log.i(TAG, "OpenCV loaded successfully"); } else { Log.e(TAG, "OpenCV initialization failed!"); (Toast.makeText(this, "OpenCV initialization failed!", Toast.LENGTH_LONG)).show(); return; } // application initialization logic }
It is also possible to use OpenCV with your native code libraries in your Android application. The OpenCV AAR package includes a prefab part and allows developers to develop native parts for Android, too. The OpenCV tutorial Tutorial2-mixed processing provides an example of this.
In summary, as well as the normal Gradle steps outlined above, you must update your native code to link against the OpenCV libraries. In CMake, this can be done as follows:
find_package(OpenCV REQUIRED COMPONENTS OpenCV::opencv_java4) file(GLOB srcs *.cpp *.c) file(GLOB hdrs *.hpp *.h) add_library(${target} SHARED …) target_link_libraries(${target} OpenCV::opencv_java4)
We look forward to seeing what you achieve with OpenCV
The OpenCV package is available on Maven Central here: https://central.sonatype.com/artifact/org.opencv/opencv.
OpenCV is an open-source project distributed with Apache 2.0 license. Feel free to check it out and contribute on GitHub!
I’m not a developer; what can I do to help? Donations to OpenCV, a non-profit organization, are welcome!