Wednesday 13 January 2016

Android OpenCV Static Initialization

This tutorial is sequel to my previous post Android OpenCV Setup  in which i have explained how to setup OpenCV for android in Async Mode (Async Initialization ). If you not gone through it I would recommend you to read it before starting with this one. In this post I'm going to take you through the steps required to set up OpenCV for android in Static Mode (Static Initialization).

 For the first two steps refer to my previous post Android OpenCV Setup

Now assuming you have completed the above mentioned steps lets get to the next.

3. Adding the native jni libs to the project

Create a folder named jniLibs in your project directory next to the java src folder as shown in the following image.

 

make sure you don't miss the path it should be in the main folder of your project source folder.
Now remember the downloaded sdk folder just navigate to it and check for the native->libs folder
as shown in the following image.

 

Now copy the folders in the libs folder and paste them to the jniLibs folder that we just created in our android studio project.

Note : copying all folders is not necessary  unless you targeting all available devices in the market instead you can just copy the specific one that suits your device it will also save you a big memory size.
This are hardware dependent libraries so chose ones which are compatible with your devices.

now your project path must look something like this


well now comes the last step.

4. Starting With the Code.
Before writing any code that invokes the OpenCV functions all you have to use the following piece of code that initializes the library in your android activity class.

public class MainActivity extends Activity {

    static {
        if (!OpenCVLoader.initDebug()) {
            Log.e("OpenCV", "Cannot connect to OpenCV Manager");
        } else {
            Log.e("OpenCV", "Connected Successfully");
        }
    }
....
....
.... 
}

now you are all set to use the OpenCV Library you can start with the samples provided along with the SDK.

Please do leave a comment if you felt it was helpful.