🌐
OpenCV
opencv.org › home › opencv on android
OpenCV on Android - OpenCV
February 5, 2024 - “Android development with OpenCV” shows you how to add OpenCV functionality into your Android application. For those who want to reuse their C++ code, we’ve created a special section: “Native/C++”. We propose this way for the professional developers, since native development is a bit harder, but gives you larger opportunities. Tutorial 2 – Mixed Processing sample shows an example ...
Library
OpenCV Releases Are Brought To You By Intel Intel is a multinational corporation known for its semiconductor products, including processors that power a wide range of computing devices, from personal computers to servers and embedded systems. Read More Qualcomm Qualcomm is a global leader in ...
Intel, OpenCV’s Platinum Member
OpenCV provides a real-time optimized Computer Vision library, tools, and hardware. It also supports model execution for Machine Learning (ML) and Artificial Intelligence (AI).
OpenCV Bootcamp
Our Free OpenCV Course is perfect for beginners & pros. OpenCV Python Free Course dives into computer vision & Python programming with hands-on projects & tutorials
Free OpenCV Course
Our Free OpenCV Course is perfect for beginners & pros. OpenCV Python Free Course dives into computer vision & Python programming with hands-on projects & tutorials
🌐
OpenCV
opencv.org › home › opencv4android samples
OpenCV4Android Samples - OpenCV
February 28, 2024 - Sample – 15-puzzle – shows how a simple game can be implemented with just a few calls to OpenCV. It is available on Google Play. Sample – face-detection – is the simplest implementation of the face detection functionality on Android.
🌐
LearnOpenCV
learnopencv.com › home › deep learning › image classification with opencv for android
Image Classification with OpenCV for Android | LearnOpenCV #
May 5, 2021 - In the previous post, we've learned how to work with OpenCV Java API with the example of a PyTorch convolutional neural network, integrated into the Java pipeline. Now we are going to transform the obtained experience into a lightweight Android ...
🌐
OpenCV
docs.opencv.org › 4.x › d5 › df8 › tutorial_dev_with_OCV_on_Android.html
OpenCV: Android Development with OpenCV
In this section we're gonna create a simple app that does nothing but OpenCV loading with Maven Central. Open Android Studio and create empty project by choosing Empty Views Activity
🌐
OpenCV
docs.opencv.org › 4.x › d9 › d3f › tutorial_android_dev_intro.html
OpenCV: Introduction into Android Development
Open the project <YOUR_OPENCV_BUILD_FOLDER>/OpenCV-android-sdk/samples in Android Studio. ... Choose a sample from the drop-down menu (for example, 15-puzzle) and run it.
🌐
GitHub
github.com › VlSomers › native-opencv-android-template
GitHub - VlSomers/native-opencv-android-template: 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.
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. Sync Gradle and run the application on your Android Device! If you have any question or remark regarding this tutorial, feel free to open an issue. This tutorial was inspired by this very good Github repository. Tutorial, Template, OpenCV 4, Android, Android Studio, Native, NDK, Native Development Kit, JNI, Java Native Interface, C++, Kotlin, Java
Starred by 305 users
Forked by 73 users
Languages   Java 34.9% | Kotlin 33.9% | CMake 19.9% | C++ 11.3% | Java 34.9% | Kotlin 33.9% | CMake 19.9% | C++ 11.3%
🌐
Medium
medium.com › @sdranju › android-studio-step-by-step-guide-for-setting-up-opencv-sdk-4-9-on-android-740547f3260b
Android Studio: Step-by-Step Guide for Setting up OpenCV SDK 4.9 on Android | by Shamsuddoha Ranju | Medium
January 12, 2024 - Step 4: Add the OpenCV Dependency Open your app-level build.gradle file and add the following line inside the “dependencies” block. Sync your project with Gradle by clicking the “Sync Now” button in Android Studio.
🌐
GitHub
github.com › jlhonora › opencv-android-sample
GitHub - jlhonora/opencv-android-sample: Sample setup for OpenCV + NDK in Android Studio
Sample setup for OpenCV + NDK in Android Studio. Contribute to jlhonora/opencv-android-sample development by creating an account on GitHub.
Starred by 62 users
Forked by 31 users
Languages   Java 71.2% | C++ 28.8% | Java 71.2% | C++ 28.8%
🌐
GitHub
github.com › makbn › opencv_android_setup_tutorial
GitHub - makbn/opencv_android_setup_tutorial: Tutorial on OpenCV for Android with Android Studio · GitHub
Extract the NDK to a directory without spaces in the path, e.g. C:\work\android-ndk-r12b · Extract the OpenCV SDK to a location near the NDK (though its exact location doesn't really matter) Right-click This PC → Properties → click Advanced system settings · In the window that appears, go to the Advanced tab and click Environment Variables · Under User variables, find the PATH variable and click Edit · Click New, then add the path to your Java bin folder Example: C:\Program Files\Java\jdk1.8.0_40\bin\
Starred by 34 users
Forked by 10 users
Languages   Java 91.3% | CMake 8.6%
Find elsewhere
🌐
GitHub
github.com › leolin310148 › opencv-android
GitHub - leolin310148/opencv-android: Example for using opencv with android.
Example for using opencv with android. Contribute to leolin310148/opencv-android development by creating an account on GitHub.
Starred by 4 users
Forked by 3 users
Languages   Java 100.0% | Java 100.0%
🌐
GitHub
github.com › opencv › opencv › tree › master › samples › android
opencv/samples/android at master · opencv/opencv
Open Source Computer Vision Library. Contribute to opencv/opencv development by creating an account on GitHub.
Author   opencv
Top answer
1 of 14
334

The below steps for using Android OpenCV sdk in Android Studio. This is a simplified version of this SO answer.

  1. Download latest OpenCV sdk for Android from OpenCV.org and decompress the zip file.
  2. Import OpenCV to Android Studio, From File -> New -> Import Module, choose sdk/java folder in the unzipped opencv archive.
  3. Update build.gradle under imported OpenCV module to update 4 fields to match your project build.gradle a) compileSdkVersion b) buildToolsVersion c) minSdkVersion and d) targetSdkVersion.
  4. Add module dependency by Application -> Module Settings, and select the Dependencies tab. Click + icon at bottom, choose Module Dependency and select the imported OpenCV module.
    • For Android Studio v1.2.2, to access to Module Settings : in the project view, right-click the dependent module -> Open Module Settings
  5. Copy libs folder under sdk/native to Android Studio under app/src/main.
  6. In Android Studio, rename the copied libs directory to jniLibs and we are done.

Step (6) is since Android studio expects native libs in app/src/main/jniLibs instead of older libs folder. For those new to Android OpenCV, don't miss below steps

  • include static{ System.loadLibrary("opencv_java"); } (Note: for OpenCV version 3 at this step you should instead load the library opencv_java3.)
  • For step(5), if you ignore any platform libs like x86, make sure your device/emulator is not on that platform.

OpenCV written is in C/C++. Java wrappers are

  1. Android OpenCV SDK - OpenCV.org maintained Android Java wrapper. I suggest this one.
  2. OpenCV Java - OpenCV.org maintained auto generated desktop Java wrapper.
  3. JavaCV - Popular Java wrapper maintained by independent developer(s). Not Android specific. This library might get out of sync with OpenCV newer versions.
2 of 14
147

Integrating OpenCV v3.1.0 into Android Studio v1.4.1, instructions with additional detail and this-is-what-you-should-get type screenshots.

Most of the credit goes to Kiran, Kool, 1", and SteveLiles over at opencv.org for their explanations. I'm adding this answer because I believe that Android Studio's interface is now stable enough to work with on this type of integration stuff. Also I have to write these instructions anyway for our project.

Experienced A.S. developers will find some of this pedantic. This answer is targeted at people with limited experience in Android Studio.

  1. Create a new Android Studio project using the project wizard (Menu:/File/New Project):

    • Call it "cvtest1"
    • Form factor: API 19, Android 4.4 (KitKat)
    • Blank Activity named MainActivity

      You should have a cvtest1 directory where this project is stored. (the title bar of Android studio shows you where cvtest1 is when you open the project)

  2. Verify that your app runs correctly. Try changing something like the "Hello World" text to confirm that the build/test cycle is OK for you. (I'm testing with an emulator of an API 19 device).

  3. Download the OpenCV package for Android v3.1.0 and unzip it in some temporary directory somewhere. (Make sure it is the package specifically for Android and not just the OpenCV for Java package.) I'll call this directory "unzip-dir" Below unzip-dir you should have a sdk/native/libs directory with subdirectories that start with things like arm..., mips... and x86... (one for each type of "architecture" Android runs on)

  4. From Android Studio import OpenCV into your project as a module: Menu:/File/New/Import_Module:

    • Source-directory: {unzip-dir}/sdk/java
    • Module name: Android studio automatically fills in this field with openCVLibrary310 (the exact name probably doesn't matter but we'll go with this).
    • Click on next. You get a screen with three checkboxes and questions about jars, libraries and import options. All three should be checked. Click on Finish.

      Android Studio starts to import the module and you are shown an import-summary.txt file that has a list of what was not imported (mostly javadoc files) and other pieces of information.

      But you also get an error message saying failed to find target with hash string 'android-14'.... This happens because the build.gradle file in the OpenCV zip file you downloaded says to compile using android API version 14, which by default you don't have with Android Studio v1.4.1.

  5. Open the project structure dialogue (Menu:/File/Project_Structure). Select the "app" module, click on the Dependencies tab and add :openCVLibrary310 as a Module Dependency. When you select Add/Module_Dependency it should appear in the list of modules you can add. It will now show up as a dependency but you will get a few more cannot-find-android-14 errors in the event log.

  6. Look in the build.gradle file for your app module. There are multiple build.gradle files in an Android project. The one you want is in the cvtest1/app directory and from the project view it looks like build.gradle (Module: app). Note the values of these four fields:

    • compileSDKVersion (mine says 23)
    • buildToolsVersion (mine says 23.0.2)
    • minSdkVersion (mine says 19)
    • targetSdkVersion (mine says 23)
  7. Your project now has a cvtest1/OpenCVLibrary310 directory but it is not visible from the project view:

Use some other tool, such as any file manager, and go to this directory. You can also switch the project view from Android to Project Files and you can find this directory as shown in this screenshot:

Inside there is another build.gradle file (it's highlighted in the above screenshot). Update this file with the four values from step 6.

  1. Resynch your project and then clean/rebuild it. (Menu:/Build/Clean_Project) It should clean and build without errors and you should see many references to :openCVLibrary310 in the 0:Messages screen.

    At this point the module should appear in the project hierarchy as openCVLibrary310, just like app. (Note that in that little drop-down menu I switched back from Project View to Android View ). You should also see an additional build.gradle file under "Gradle Scripts" but I find the Android Studio interface a little bit glitchy and sometimes it does not do this right away. So try resynching, cleaning, even restarting Android Studio.

    You should see the openCVLibrary310 module with all the OpenCV functions under java like in this screenshot:

  2. Copy the {unzip-dir}/sdk/native/libs directory (and everything under it) to your Android project, to cvtest1/OpenCVLibrary310/src/main/, and then rename your copy from libs to jniLibs. You should now have a cvtest1/OpenCVLibrary310/src/main/jniLibs directory. Resynch your project and this directory should now appear in the project view under openCVLibrary310.

  3. Go to the onCreate method of MainActivity.java and append this code:

    Copyif (!OpenCVLoader.initDebug()) {
        Log.e(this.getClass().getSimpleName(), "  OpenCVLoader.initDebug(), not working.");
    } else {
        Log.d(this.getClass().getSimpleName(), "  OpenCVLoader.initDebug(), working.");
    }
    

    Then run your application. You should see lines like this in the Android Monitor: (I don't know why that line with the error message is there)

  4. Now try to actually use some openCV code. In the example below I copied a .jpg file to the cache directory of the cvtest1 application on the android emulator. The code below loads this image, runs the canny edge detection algorithm and then writes the results back to a .png file in the same directory.

    Put this code just below the code from the previous step and alter it to match your own files/directories.

    CopyString inputFileName="simm_01";
    String inputExtension = "jpg";
    String inputDir = getCacheDir().getAbsolutePath();  // use the cache directory for i/o
    String outputDir = getCacheDir().getAbsolutePath();
    String outputExtension = "png";
    String inputFilePath = inputDir + File.separator + inputFileName + "." + inputExtension;
    
    
    Log.d (this.getClass().getSimpleName(), "loading " + inputFilePath + "...");
    Mat image = Imgcodecs.imread(inputFilePath);  
    Log.d (this.getClass().getSimpleName(), "width of " + inputFileName + ": " + image.width());
    // if width is 0 then it did not read your image.
    
    
    // for the canny edge detection algorithm, play with these to see different results
    int threshold1 = 70;
    int threshold2 = 100;
    
    Mat im_canny = new Mat();  // you have to initialize output image before giving it to the Canny method
    Imgproc.Canny(image, im_canny, threshold1, threshold2);
    String cannyFilename = outputDir + File.separator + inputFileName + "_canny-" + threshold1 + "-" + threshold2 + "." + outputExtension;
    Log.d (this.getClass().getSimpleName(), "Writing " + cannyFilename);
    Imgcodecs.imwrite(cannyFilename, im_canny);
    
  5. Run your application. Your emulator should create a black and white "edge" image. You can use the Android Device Monitor to retrieve the output or write an activity to show it.

The Gotchas:

  • If you lower your target platform below KitKat some of the OpenCV libraries will no longer function, specifically the classes related to org.opencv.android.Camera2Renderer and other related classes. You can probably get around this by simply removing the apprpriate OpenCV .java files.
  • If you raise your target platform to Lollipop or above my example of loading a file might not work because use of absolute file paths is frowned upon. So you might have to change the example to load a file from the gallery or somewhere else. There are numerous examples floating around.
🌐
KDnuggets
kdnuggets.com › 2020 › 10 › guide-preparing-opencv-android.html
A Guide to Preparing OpenCV for Android - KDnuggets
October 6, 2020 - This tutorial guides Android developers in preparing the popular library OpenCV for use. Using a step-by-step guide, the library will be imported into Android Studio and then can be used for performing any of the operations it supports, such as object detection, segmentation, tracking, and more.
🌐
OpenCV
opencv.org › home › opencv4android usage models
OpenCV4Android usage models - OpenCV
January 30, 2024 - The option is available since OpenCV 4.9.0 release and will be maintained by the OpenCV Team. We highly recommend this way, if you do not require advanced build options. All usage steps are described in the tutorial “Android Development with OpenCV”. The option is also applicable for all Android samples and available in sample’s build.gradle.
🌐
Fritz ai
fritz.ai › home › blog › a guide to preparing opencv for android
A Guide to Preparing OpenCV for Android - Fritz ai
September 21, 2023 - This tutorial guides Android developers in preparing the popular library OpenCV (Open Computer Vision) for use. Through a step-by-step guide, the library will be imported into Android Studio (the official IDE for Android). Upon installation and setup, OpenCV can be…
🌐
GeeksforGeeks
geeksforgeeks.org › how-to-add-opencv-library-into-android-application-using-android-studio
How to Add OpenCV library into Android Application using Android Studio? | GeeksforGeeks
February 18, 2021 - Now open Android Studio and create a new project. After creating a new project go to File > New > Import-Module · Now click on Import-Module as you will see a dialog box like below: Now, import the module of OpenCV. So open the location where you extracted the OpenCV library so go to that location.
🌐
Medium
medium.com › android-news › a-beginners-guide-to-setting-up-opencv-android-library-on-android-studio-19794e220f3c
A Beginner’s Guide to Setting up OpenCV Android Library on Android Studio | by Elvis Chidera | AndroidPub | Medium
August 21, 2018 - To easily do this on Android Studio, click on File -> Project Structure. ... When the project structure dialog opens, click on the app module or any other module that you want to use OpenCV library in.
🌐
OpenCV
docs.opencv.org › 3.4 › d0 › d6c › tutorial_dnn_android.html
OpenCV: How to run deep networks on Android device
In this tutorial you'll know how to run deep learning networks on Android device using OpenCV deep learning module.
🌐
Amazon
amazon.com › OpenCV-Android-Programming-Example-Muhammad › dp › 1783550597
OpenCV Android Programming By Example: Mohammad, Amgad: 9781783550593: Amazon.com: Books
You will discover that, though computer vision is a challenging subject, the ideas and algorithms used are simple and intuitive, and you will appreciate the abstraction layer that OpenCV uses to do the heavy lifting for you. Packed with many examples, the book will help you understand the main data structures used within OpenCV, and how you can use them to gain performance boosts.