{"id":13542373,"url":"https://github.com/VlSomers/native-opencv-android-template","last_synced_at":"2025-04-02T10:30:43.720Z","repository":{"id":37387403,"uuid":"201671755","full_name":"VlSomers/native-opencv-android-template","owner":"VlSomers","description":"A tutorial for setting up OpenCV 4.6.0 (and other 4.x.y version) for Android in Android Studio with Native Development Kit (NDK) support for C++ development.","archived":false,"fork":false,"pushed_at":"2022-07-15T16:06:53.000Z","size":959,"stargazers_count":286,"open_issues_count":1,"forks_count":68,"subscribers_count":14,"default_branch":"master","last_synced_at":"2024-11-03T08:33:32.518Z","etag":null,"topics":["android","android-ndk","android-studio","computer-vision","cpp","java","java-native-interface","jni","kotlin","kotlin-android","ndk","opencv","opencv-android-release","opencv-android-sdk","opencv4android"],"latest_commit_sha":null,"homepage":"","language":"Kotlin","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/VlSomers.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-08-10T19:02:55.000Z","updated_at":"2024-11-03T07:57:23.000Z","dependencies_parsed_at":"2022-07-07T23:09:30.675Z","dependency_job_id":null,"html_url":"https://github.com/VlSomers/native-opencv-android-template","commit_stats":null,"previous_names":[],"tags_count":2,"template":true,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/VlSomers%2Fnative-opencv-android-template","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/VlSomers%2Fnative-opencv-android-template/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/VlSomers%2Fnative-opencv-android-template/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/VlSomers%2Fnative-opencv-android-template/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/VlSomers","download_url":"https://codeload.github.com/VlSomers/native-opencv-android-template/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246796771,"owners_count":20835443,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["android","android-ndk","android-studio","computer-vision","cpp","java","java-native-interface","jni","kotlin","kotlin-android","ndk","opencv","opencv-android-release","opencv-android-sdk","opencv4android"],"created_at":"2024-08-01T10:01:05.776Z","updated_at":"2025-04-02T10:30:43.147Z","avatar_url":"https://github.com/VlSomers.png","language":"Kotlin","funding_links":[],"categories":["Table of Contents"],"sub_categories":["Get start with opencv - Playlists"],"readme":"[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)\n# Native OpenCV for Android with Android NDK\n\nA tutorial for setting up OpenCV 4.6.0 (and other 4.x.y versions) for Android in [Android Studio](https://developer.android.com/studio) with Native Development Kit (NDK) support.\n[Android NDK](https://developer.android.com/ndk) enables you to implement your [OpenCV](https://opencv.org) image processing pipeline in C++ and call that C++ code from Android Kotlin/Java code through JNI ([Java Native Interface](https://en.wikipedia.org/wiki/Java_Native_Interface)).\n\nThis 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++.\n\n## Setup\n\n| Tool      | Version |\n| ---       |  ---    |\n| [OpenCV](https://opencv.org) | 4.6.0\n| [Android Studio](https://developer.android.com/studio) | 2021.2.1\n| [Android Build Tool](https://developer.android.com/about) | 33.0\n| [Android NDK](https://developer.android.com/ndk/guides) | 25.0\n| [Kotlin](https://kotlinlang.org/docs/reference/) | 1.6.10\n| [Gradle](https://gradle.org) | 7.2.1\n| Mac OS | 12.4\n\n## How to use this repository\n\n1. [Download and Install Android Studio](https://developer.android.com/studio)\n\n2. [Install NDK and CMake](https://developer.android.com/studio/projects/install-ndk.md)\n\n3. Clone this repository as an Android Studio project :\n     * In Android Studio, click on `File -\u003e New -\u003e Project from Version Control -\u003e Git`\n     * Paste this repository *Github URL*, choose a *project directory* and click next.\n\n4. Install *OpenCV Android release* :\n    * Download [OpenCV 4.6.0 Android release](https://sourceforge.net/projects/opencvlibrary/files/4.6.0/opencv-4.6.0-android-sdk.zip/download) or download latest available Android release on [OpenCV website](https://opencv.org/releases/).\n    * Unzip downloaded file and put **OpenCV-android-sdk** directory on a path of your choice.\n\n5. Link your *Android Studio* project to the *OpenCV Android SDK* you just downloaded :\n    * Open [gradle.properties](gradle.properties) file and edit following line with your own *OpenCV Android SDK* directory path :\n\n          opencvsdk=/Users/Example/Downloads/OpenCV-android-sdk\n\n6. Sync Gradle and run the application on your Android Device!\n\nNote: MainActivity is written in Kotlin but you can comment out the [Kotlin](/app/src/main/kotlin/com/example/nativeopencvandroidtemplate/MainActivity.kt) file and uncomment the [Java](/app/src/main/java/com/example/nativeopencvandroidtemplate/MainActivity.java) file to use Java.\n\n## Bootstrap a new Android project with Native OpenCV support\n\nHere are the steps to follow to create a new Android Studio project with native OpenCV support :\n\n1. [Download and Install Android Studio](https://developer.android.com/studio)\n\n2. [Install NDK and CMake](https://developer.android.com/studio/projects/install-ndk.md)\n\n3. Create a new *Native Android Studio project* :\n    * Select `File -\u003e New -\u003e New Project...` from the main menu.\n    * Click `Phone and Tablet tab`, select `Native C++` and click next.\n    * Choose an `Application Name`, select your favorite `language` (Kotlin or Java), choose `Minimum API level` (28 here) and select next.\n    * Choose `Toolchain default` as *C++ standard* and click Finish.\n\n4. Install *OpenCV Android release* :\n    * Download [OpenCV 4.6.0 Android release](https://sourceforge.net/projects/opencvlibrary/files/4.6.0/opencv-4.6.0-android-sdk.zip/download) or download latest available Android release on [OpenCV website](https://opencv.org/releases/).\n    * Unzip downloaded file and put **OpenCV-android-sdk** directory on a path of your choice.\n    \n5. Add *OpenCV Android SDK* as a module into your project :\n    * Open [setting.gradle](settings.gradle) file and append these two lines.\n    \n          include ':opencv'\n          project(':opencv').projectDir = new File(opencvsdk + '/sdk')\n        \n    * Open [gradle.properties](gradle.properties) file and append following line. Do not forget to use correct *OpenCV Android SDK* path for your machine. \n    \n          opencvsdk=/Users/Example/Downloads/OpenCV-android-sdk\n          \n    * Open [build.gradle](app/build.gradle) file and add `implementation project(path: ':opencv')` to dependencies section :\n    \n          dependencies {\n              ...\n              implementation project(path: ':opencv')\n          }\n    \n    * Click on `File -\u003e Sync Project with Gradle Files`.\n    \n6. Add following config to app [build.gradle](app/build.gradle) file :\n    * In `android -\u003e defaultConfig -\u003e externalNativeBuild -\u003e cmake` section, put these three lines :\n    \n          cppFlags \"-frtti -fexceptions\"\n          abiFilters 'x86', 'x86_64', 'armeabi-v7a', 'arm64-v8a'\n          arguments \"-DOpenCV_DIR=\" + opencvsdk + \"/sdk/native\"\n        \n7. Add following config to [CMakeLists.txt](app/src/main/cpp/CMakeLists.txt) file :\n    * Before `add_library` instruction, add three following lines :\n    \n          include_directories(${OpenCV_DIR}/jni/include)\n          add_library( lib_opencv SHARED IMPORTED )\n          set_target_properties(lib_opencv PROPERTIES IMPORTED_LOCATION ${OpenCV_DIR}/libs/${ANDROID_ABI}/libopencv_java4.so)\n        \n    * In `target_link_libraries` instruction arguments, add following line :\n    \n          lib_opencv\n        \n8. Add following *permissions* to your [AndroidManifest.xml](app/src/main/AndroidManifest.xml) file :\n\n       \u003cuses-permission android:name=\"android.permission.CAMERA\"/\u003e\n       \u003cuses-feature android:name=\"android.hardware.camera\"/\u003e\n       \u003cuses-feature android:name=\"android.hardware.camera.autofocus\"/\u003e\n       \u003cuses-feature android:name=\"android.hardware.camera.front\"/\u003e\n       \u003cuses-feature android:name=\"android.hardware.camera.front.autofocus\"/\u003e\n    \n9. Create your *MainActivity* :\n    * You can copy paste MainActivity [Kotlin](/app/src/main/kotlin/com/example/nativeopencvandroidtemplate/MainActivity.kt) or [Java](/app/src/main/java/com/example/nativeopencvandroidtemplate/MainActivity.java) file. Do not forget to adapt package name.\n    \n10. Create your *activity_main.xml* :\n    * You can copy paste [activity_main.xml](/app/src/main/res/layout/activity_main.xml) file.\n    \n11. Add native code in *native-lib.cpp* :\n    * You can copy paste [native-lib.cpp](app/src/main/cpp/native-lib.cpp) file. Do not forget to adapt the method name : \n    `Java_com_example_nativeopencvtemplate_MainActivity_adaptiveThresholdFromJNI`\n    should be replaced with \n    `Java_\u003cmain-activity-package-name-with-underscores\u003e_MainActivity_adaptiveThresholdFromJNI`.\n    \n12. Sync Gradle and run the application on your Android Device!\n\n\n\n\n\n![alt text](images/native-opencv-android-template-screenshot.jpg)\n\n\n## Questions and Remarks\n\nIf you have any question or remark regarding this tutorial, feel free to open an issue.\n\n## Acknowledgments\n\nThis tutorial was inspired by this very good [Github repository](https://github.com/leadrien/opencv_native_androidstudio).\n\n## Keywords\n\nTutorial, Template, OpenCV 4, Android, Android Studio, Native, NDK, Native Development Kit, JNI, Java Native Interface, C++, Kotlin, Java\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FVlSomers%2Fnative-opencv-android-template","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FVlSomers%2Fnative-opencv-android-template","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FVlSomers%2Fnative-opencv-android-template/lists"}