Now, in the series about How to install and use OpenCV, the last but not least: Android! (Feel free to check Windows and Linux tutorials).





Getting Started

 

Steps

  • Create a new project.
  • Configure as you like, it’s not that relevant here. For testing purposes, I will leave everything set as default.

 

 

  • Import OpenCV to Android Studio, From File -> New -> Import Module.

 

import module

    • Choose \OpenCV-android-sdk\sdk\java

 

 

  • Leave the default options and then, click Finish.

 

default module addition

 

  • Update
    • a) compileSdkVersion
    • b) buildToolsVersion
    • c) minSdkVersion
    • d) targetSdkVersionof OpenCV’s build.gradle file to match the project build.gradle

 

  • In other words: build.gradle(Module: OpenCV Library)  must have the same values from a), b), c) and d) as build.gradle (module: app)

 

build.gradle

 

  • Add the module dependency by right-clicking on App, then Open Module Settings.

 

Module configuration

 

  • Click app” then choose the Dependencies tab followed by the + icon and choose Module Dependency.

 

 

  • Select the imported OpenCV module.
  • The configuration is over. Now it’s time to add some code to make sure everything is Ok.

 

Testing some Android code

  • Add the code below (and do the required corrections) in your MainActivity to run a simple test.

 

 

  • This code will plot “OpenCV loaded successfully”  and a bunch of status messages about the library if OpenCV initializes correctly.

 

Android studio output

 

 

opencv manager

 

  • The code and configuration files can be found here

 

 

 

Have fun with your projects! 
If this post helped you, please consider buying me a coffee 🙂