All Projects → VlSomers → Native Opencv Android Template

VlSomers / Native Opencv Android Template

Licence: mit
A tutorial for setting up OpenCV 4.5.0 (and other 4.x.y version) for Android in Android Studio with Native Development Kit (NDK) support.

Programming Languages

java
68154 projects - #9 most used programming language
kotlin
9241 projects

Projects that are alternatives of or similar to Native Opencv Android Template

premake-android-studio
premake5 module for android-studio and gradle build.
Stars: ✭ 24 (-81.68%)
Mutual labels:  ndk, jni, android-studio
Jni4android
JNI Generater for Android
Stars: ✭ 261 (+99.24%)
Mutual labels:  ndk, jni
SecurityDemo
ndk进行简单的签名校验,密钥保护demo,android应用签名校验
Stars: ✭ 22 (-83.21%)
Mutual labels:  ndk, jni
Keepalive
Fighting against force-stop kill process on Android with binder ioctl / Android高级保活
Stars: ✭ 376 (+187.02%)
Mutual labels:  ndk, jni
Camera2GLPreview
Android camera preview application using Camera2 API and OpenGL ES/Vulkan
Stars: ✭ 140 (+6.87%)
Mutual labels:  ndk, jni
AndroidDevTools
收集整理Android开发所需的Android SDK、开发中用到的工具、Android开发教程、Android设计规范,免费的设计素材等。
Stars: ✭ 7,284 (+5460.31%)
Mutual labels:  ndk, android-studio
Jni.hpp
A modern, type-safe, header-only, C++14 wrapper for JNI
Stars: ✭ 313 (+138.93%)
Mutual labels:  ndk, jni
Relinker
A robust native library loader for Android.
Stars: ✭ 2,612 (+1893.89%)
Mutual labels:  ndk, jni
Anyndk
🔥 Android native library, make your development faster and easier. Android各种native库,让你的开发更快更简单
Stars: ✭ 35 (-73.28%)
Mutual labels:  ndk, jni
Android Hpe
Android native application to perform head pose estimation using images coming from the front camera.
Stars: ✭ 46 (-64.89%)
Mutual labels:  ndk, opencv
Opencv image comparator
图片相似度计算(直方图、峰值信噪比、结构相似性、感知哈希算法)、轮廓检测、直线检测、圆检测、角点检测、直线交点计算、旋转角度矫正、图像匹配的对应相似处连线、灰度、二值化、直方图均衡化。
Stars: ✭ 87 (-33.59%)
Mutual labels:  opencv, jni
NativeGL demo
此程序使用android native代码实现EGL,并使用SurfaceView作为OpenGL绘图窗口。即可提高图形渲染性能,又可使用java层定义的图形界面
Stars: ✭ 43 (-67.18%)
Mutual labels:  ndk, jni
ChangeVoice
NDK语音消息的变声处理
Stars: ✭ 33 (-74.81%)
Mutual labels:  ndk, jni
Android Disassembler
Disassemble ANY files including .so (NDK, JNI), Windows PE(EXE, DLL, SYS, etc), linux binaries, libraries, and any other files such as pictures, audios, etc(for fun)files on Android. Capstone-based disassembler application on android. 안드로이드 NDK 공유 라이브러리, Windows 바이너리, etc,... 리버싱 앱
Stars: ✭ 250 (+90.84%)
Mutual labels:  ndk, jni
Androiddevwithcpp
Android Develop With C++
Stars: ✭ 106 (-19.08%)
Mutual labels:  ndk, jni
Jnioor
基于C++模板函数与Fluent API设计的JNI反射库,极大的简化JNI反射调用,提高JNI开发效率与稳定性
Stars: ✭ 278 (+112.21%)
Mutual labels:  ndk, jni
Androidsecurity
Android安全实践
Stars: ✭ 150 (+14.5%)
Mutual labels:  ndk, jni
Jpegkit Android
Efficient JPEG operations for Android without the risk of an OutOfMemoryException.
Stars: ✭ 154 (+17.56%)
Mutual labels:  ndk, jni
Android Demo
Hipacc demo project for Renderscript and Filterscript
Stars: ✭ 7 (-94.66%)
Mutual labels:  ndk, android-studio
Android Luajit Launcher
Android NativeActivity based launcher for LuaJIT, implementing the main loop within Lua land via FFI
Stars: ✭ 87 (-33.59%)
Mutual labels:  ndk, jni

License: MIT

Native OpenCV for Android with Android NDK

A tutorial for setting up OpenCV 4.5.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.5.0
Android Studio 4.1.0
Android Build Tool 29.0.3
Android NDK r21d
Kotlin 1.3.72
Gradle 6.5.0
Mac OS 10.15.7

How to use this repository

  1. Download and Install Android Studio

  2. Install NDK, CMake and LLDB

  3. 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.
  4. Install OpenCV Android release :

  5. 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
      
  6. 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 :

  1. Download and Install Android Studio

  2. Install NDK, CMake and LLDB

  3. Create a new Native Android Studio project :

    • Select File -> New -> New Project... from the main menu.
    • Click Phone and Tablet tab, select Native C++ and click next.
    • Choose an Application Name, select your favorite language (Kotlin or Java), choose Minimum API level (28 here) and select next.
    • Choose Toolchain default as C++ standard and click Finish.
  4. Install OpenCV Android release :

  5. 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-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.

  6. 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"
      
  7. 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
      
  8. 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"/>
    
  9. Create your MainActivity :

    • You can copy paste MainActivity Kotlin or Java file. Do not forget to adapt package name.
  10. Create your activity_main.xml :

  11. 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 with Java_<main-activity-package-name-with-underscores>_MainActivity_adaptiveThresholdFromJNI.
  12. Sync Gradle and run the application on your Android Device!

alt text

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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].