Skip to content
This repository has been archived by the owner on Mar 26, 2024. It is now read-only.

Setup OpenCV 3.1.0 in Android Studio 2.2

David Miguel Lozano edited this page Oct 4, 2016 · 8 revisions

Download OpenCV Android SDK

  1. Download OpenCV4Android from http://opencv.org/downloads.html. The latest version at the time of writing is 3.1.0.
  2. Extract the downloaded zip file.

Setup Android Studio

  1. Open Android Studio and create a new project.
  2. Then select File -> New -> Import Module.
  3. You need to select the OpenCV SDK location. Select OpenCV-3.1.0-android-sdk/sdk/java. Then select Next and Finish. OpenCV sdk is now imported as a module.
  4. Open the build.gradle file from the OpenCV module.
  5. Change the compileSdkVersion, targetSdkVersion and buildToolsVersion value to the latest version you use and sync the project.
  6. Switch back to Android view in Project explorer. Right click on the app module and select Open Module Settings.
  7. For the app module in the Dependencies tab, select Add -> Module Dependency -> openCVLibrary.

Add Native libraries

These steps are only for static initialization, ignore them if you are using async initialization with OpenCV Manager.

  1. Now we need to add native JNI libraries in our project. These libraries should be added in jniLibs directory.
  2. Create a new jniLibs directory in app-> src -> main.
  3. Open the extracted OpenCV SDK directory. Switch to OpenCV-3.1.0-android-sdk/sdk/native/libs directory.
  4. You will find directories for many CPU architectures. Copy the required architecture/s directory to the jniLibs directory.
  5. Delete all files except libopencv_java3.so.
  6. Add android.useDeprecatedNdk=true to gradle.properties file.

Testing OpenCV

With async initialization

public class Sample1Java extends Activity {

    private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) {
        @Override
        public void onManagerConnected(int status) {
            switch(status) {
                case LoaderCallbackInterface.SUCCESS:
                    Log.i(TAG,"OpenCV Manager Connected");
                    //from now onwards, you can use OpenCV API
                    break;
                case LoaderCallbackInterface.INIT_FAILED:
                    Log.i(TAG,"Init Failed");
                    break;
                case LoaderCallbackInterface.INSTALL_CANCELED:
                    Log.i(TAG,"Install Cancelled");
                    break;
                case LoaderCallbackInterface.INCOMPATIBLE_MANAGER_VERSION:
                    Log.i(TAG,"Incompatible Version");
                    break;
                case LoaderCallbackInterface.MARKET_ERROR:
                    Log.i(TAG,"Market Error");
                    break;
                default:
                    Log.i(TAG,"OpenCV Manager Install");
                    super.onManagerConnected(status);
                    break;
            }
        }
    };

    @Override
    public void onResume() {
        super.onResume();
        OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_3_1_0, this, mLoaderCallback);
    }
}

With static initialization

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

References