This repository has been archived by the owner on Mar 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 150
Setup OpenCV 3.1.0 in Android Studio 2.2
David Miguel Lozano edited this page Oct 4, 2016
·
8 revisions
- Download OpenCV4Android from http://opencv.org/downloads.html. The latest version at the time of writing is 3.1.0.
- Extract the downloaded zip file.
- Open Android Studio and create a new project.
- Then select
File -> New -> Import Module
. - 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. - Open the
build.gradle
file from the OpenCV module. - Change the
compileSdkVersion
,targetSdkVersion
andbuildToolsVersion
value to the latest version you use and sync the project. - Switch back to Android view in Project explorer. Right click on the
app
module and selectOpen Module Settings
. - For the
app
module in the Dependencies tab, selectAdd -> Module Dependency -> openCVLibrary
.
These steps are only for static initialization, ignore them if you are using async initialization with OpenCV Manager.
- Now we need to add native JNI libraries in our project. These libraries should be added in
jniLibs
directory. - Create a new
jniLibs
directory inapp-> src -> main
. - Open the extracted OpenCV SDK directory. Switch to
OpenCV-3.1.0-android-sdk/sdk/native/libs
directory. - You will find directories for many CPU architectures. Copy the required architecture/s directory to the jniLibs directory.
- Delete all files except libopencv_java3.so.
- Add
android.useDeprecatedNdk=true
togradle.properties
file.
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);
}
}
if (!OpenCVLoader.initDebug()) {
Log.e(this.getClass().getSimpleName(), " OpenCVLoader.initDebug(), not working.");
} else {
Log.i(this.getClass().getSimpleName(), " OpenCVLoader.initDebug(), working.");
}