This application is a sample Android Studio project with native OpenCV.
It gets the camera frames, make JNI calls with its gray matrices references as parameters, add some random noise to the images from a C++ method, and render the generated frames.
It works with Android Studio 3+
Last included OpenCV version: 3.4
Here is how to use this project to run native OpenCV code.
jniLibs
in app/src/main
that points to YOUR_OPENCV_SDK/sdk/native/libs
app/CMakeLists.txt
change line 9 to points to YOUR_OPENCV_SDK/sdk/native/jni/include
Here is how I made this project. If you simply want to run openCV with NDK support, use the previous Usage chapter. If you want to build the full project from scratch, you can follow the steps in this chapter.
Make sure you have Android SDK up to date, with NDK installed
Download latest OpenCV SDK for Android from OpenCV.org and decompress the zip file.
Create a new Android Studio project
Import OpenCV library module
YOUR_OPENCV_SDK/sdk/java
folderSet the OpenCV library module up to fit your SDK
Edit openCVLibrary/build.gradle
to fit your SDK:
compileSdkVersion 27
defaultConfig {
minSdkVersion 19
targetSdkVersion 27
}
Add OpenCV module dependency in your app module
File -> Project structure -> Module app -> Dependencies tab -> New module dependency -> choose OpenCV library module
Make a symlink named jniLibs
in app/src/main
that points to YOUR_OPENCV_SDK/sdk/native/libs
Set the app build.gradle
Add abiFilters
externalNativeBuild {
cmake {
cppFlags "-frtti -fexceptions"
abiFilters 'x86', 'x86_64', 'armeabi-v7a', 'arm64-v8a'
}
}
Add openCV jniLibs directory to point to the symlink
sourceSets {
main {
jniLibs.srcDirs = ['src/main/jniLibs']
}
}
See build.gradle
Configure the CMakeLists.txt
file
cmake_minimum_required
, addinclude_directories(YOUR_OPENCV_SDK/sdk/native/jni/include)
add_library( lib_opencv SHARED IMPORTED )
set_target_properties(lib_opencv PROPERTIES IMPORTED_LOCATION ${CMAKE_CURRENT_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}/libopencv_java3.so)
lib_opencv
to the target_link_libraries
listGrant camera permission
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"/>
Add OpenCV code