VlSomers/native-opencv-android-template
À propos
A tutorial for setting up OpenCV 4.12.0 (and other 4.x.y version) for Android in Android Studio with Native Development Kit (NDK) support for C++ development.
Apprentissage et éducationProgression mesurée des étoiles
- in 24h
- 0
- in 7 days
- +1
- in 15 days
- +1
- in 1 month
- Historique en cours
Variations nettes sur des périodes qui se chevauchent, pas des totaux quotidiens. Les mesures absentes restent vides. Les étoiles indiquent l’intérêt, pas la qualité.
README.md
OriginalNative OpenCV for Android with Android NDK
A tutorial for setting up OpenCV 4.12.0 (and other 4.x.y versions) for Android in Android Studio with Native Development Kit (NDK) support. Android NDK enables you to implement your OpenCV image processing pipeline in C++ and call that C++ code from Android Kotlin/Java code through JNI (Java Native Interface).
This sample Android application displays a live camera feed with an OpenCV adaptive threshold filter applied on each frame. The OpenCV adaptive threshold call is performed in C++.
Setup
| Tool | Version |
|---|---|
| OpenCV | 4.12.0 |
| Android Studio | 2024.2.1 (Ladybug) |
| Android Build Tool | 34.0 |
| Android NDK | 27.0 |
| Kotlin | 1.8.10 |
| Gradle | 8.13 |
| Target SDK | 34 (Android 14) |
| Mac OS | 12.4 |
Updates
October 2, 2025
- Claude Sonnet Assisted Update: Project updated to OpenCV 4.12.0 and latest Android development tools
How to use this repository
Clone this repository as an Android Studio project :
- In Android Studio, click on
File -> New -> Project from Version Control -> Git - Paste this repository Github URL, choose a project directory and click next.
- In Android Studio, click on
Install OpenCV Android release :
- Download OpenCV 4.12.0 Android release or download latest available Android release on OpenCV website.
- Unzip downloaded file and put OpenCV-android-sdk directory on a path of your choice.
Link your Android Studio project to the OpenCV Android SDK you just downloaded :
Open gradle.properties file and edit following line with your own OpenCV Android SDK directory path :
opencvsdk=/Users/Example/Downloads/OpenCV-android-sdk
Sync Gradle and run the application on your Android Device!
Note: MainActivity is written in Kotlin but you can comment out the Kotlin file and uncomment the Java file to use Java.
Bootstrap a new Android project with Native OpenCV support
Here are the steps to follow to create a new Android Studio project with native OpenCV support :
Create a new Native Android Studio project :
- Select
File -> New -> New Project...from the main menu. - Click
Phone and Tablet tab, selectNative C++and click next. - Choose an
Application Name, select your favoritelanguage(Kotlin or Java), chooseMinimum API level(21 here) and select next. - Choose
Toolchain defaultas C++ standard and click Finish.
- Select
Install OpenCV Android release :
- Download OpenCV 4.12.0 Android release or download latest available Android release on OpenCV website.
- Unzip downloaded file and put OpenCV-android-sdk directory on a path of your choice.
Add OpenCV Android SDK as a module into your project :
Open setting.gradle file and append these two lines.
include ':opencv' project(':opencv').projectDir = new File(opencvsdk + '/sdk')Open gradle.properties file and append following line. Do not forget to use correct OpenCV Android SDK path for your machine.
opencvsdk=/Users/Example/Downloads/OpenCV-android-sdkOpen build.gradle file and add
implementation project(path: ':opencv')to dependencies section :dependencies { ... implementation project(path: ':opencv') }Click on
File -> Sync Project with Gradle Files.
Add following config to app build.gradle file :
In
android -> defaultConfig -> externalNativeBuild -> cmakesection, put these three lines :cppFlags "-frtti -fexceptions" abiFilters 'armeabi-v7a', 'arm64-v8a' arguments "-DOpenCV_DIR=" + opencvsdk + "/sdk/native/jni"
Add following config to CMakeLists.txt file :
Before
add_libraryinstruction, add three following lines :include_directories(${OpenCV_DIR}/jni/include) add_library( lib_opencv SHARED IMPORTED ) set_target_properties(lib_opencv PROPERTIES IMPORTED_LOCATION ${OpenCV_DIR}/libs/${ANDROID_ABI}/libopencv_java4.so)In
target_link_librariesinstruction arguments, add following line :lib_opencv
Add following permissions to your AndroidManifest.xml file :
<uses-permission android:name="android.permission.CAMERA"/> <uses-feature android:name="android.hardware.camera"/> <uses-feature android:name="android.hardware.camera.autofocus"/> <uses-feature android:name="android.hardware.camera.front"/> <uses-feature android:name="android.hardware.camera.front.autofocus"/>Create your MainActivity :
Create your activity_main.xml :
- You can copy paste activity_main.xml file.
Add native code in native-lib.cpp :
- You can copy paste native-lib.cpp file. Do not forget to adapt the method name :
Java_com_example_nativeopencvtemplate_MainActivity_adaptiveThresholdFromJNIshould be replaced withJava_<main-activity-package-name-with-underscores>_MainActivity_adaptiveThresholdFromJNI.
- You can copy paste native-lib.cpp file. Do not forget to adapt the method name :
Sync Gradle and run the application on your Android Device!

Questions and Remarks
If you have any question or remark regarding this tutorial, feel free to open an issue.
Acknowledgments
This tutorial was inspired by this very good Github repository.
Keywords
Tutorial, Template, OpenCV 4, Android, Android Studio, Native, NDK, Native Development Kit, JNI, Java Native Interface, C++, Kotlin, Java
Explorer des projets similaires
Chargement…