September 28, 2021

How to add opencv in android studio

By lj

OpenCV

OpenCV (Open Source Computer Vision Library) is an open source computer vision and machine learning software library. OpenCV was built to provide a common infrastructure for computer vision applications and to accelerate the use of machine perception in the commercial products. Being a BSD-licensed product, OpenCV makes it easy for businesses to utilize and modify the code.

Why OpenCV

OpenCV have too many use cases, like image processing, face recognition, and other too. But I am using it here for image processing.

Add OpenCV library

OpenCV library can not be imported with build.gradle implementation url. The are providing source code or module project to us, which we need to add in our application.

I have tried regular method to import it like File -> New -> Import Module, but It is not showing Next or Finish button. Before we go further, if you have not downloaded module file then please visit https://opencv.org/releases/  and download it for android.

You will get zip file, extract it and navigate copy sdk folder in your projects root directory.

Make it module

To make sdk folder as module, we need to modify settings.gradle file. Append below line in file and wait for sync

include ':app' , ':opencv'

Now we can see sdk folder as module folder. So we can add module dependency to our app module and use openCV in our module.

To add module dependency we can right-click on project name -> Open Module Settings -> Dependencies