-
Clone this repository.
-
Install OpenCV Android release :
- Download OpenCV 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 :
-
[MacOS|Ubuntu] Open gradle.properties file and edit following line with your own OpenCV Android SDK directory path :
-
[MacOS|Ubuntu]
opencvsdk=/Users/Example/Downloads/OpenCV-android-sdk
-
[Window]
opencvsdk=C\:\\Users\\HELLO\\Downloads\\opencv-4.5.4-android-sdk\\OpenCV-android-sdk
-
-
Sync Gradle and run the application on your Android Device!
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
(28 here) and select next. - Choose
Toolchain default
as C++ standard and click Finish.
- Select
-
Install OpenCV Android release :
- Download OpenCV 4.5.3 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.
-
[MacOS|Ubuntu]
opencvsdk=/Users/Example/Downloads/OpenCV-android-sdk
-
[Window]
opencvsdk=C\:\\Users\\HELLO\\Downloads\\opencv-4.5.4-android-sdk\\OpenCV-android-sdk
-
Open 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 -> cmake
section, put these three lines :cppFlags "-frtti -fexceptions" abiFilters 'x86', 'x86_64', 'armeabi-v7a', 'arm64-v8a' arguments "-DOpenCV_DIR=" + opencvsdk + "/sdk/native"
-
-
Add following config to CMakeLists.txt file :
-
Before
add_library
instruction, 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_libraries
instruction 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 :
- Copy paste MainActivity file.
-
Create your activity_main.xml :
- 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_adaptiveThresholdFromJNI
should 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!