How to use opencv in using Gradle?
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.5.+'
}
}
apply plugin: 'android'
repositories {
mavenCentral()
maven {
url 'http://maven2.javacv.googlecode.com/git/'
}
}
dependencies {
compile 'com.android.support:support-v4:13.0.+'
compile 'com.googlecode.javacv:javacv:0.5'
instrumentTestCompile 'junit:junit:4.4'
}
android {
compileSdkVersion 14
buildToolsVersion "17.0.0"
defaultConfig {
minSdkVersion 7
targetSdkVersion 14
}
}
This is worked for me :)
How to import OpenCV 4.5 in Android Studio
This is the complete procedure that currently works for me with OpenCV 4.5.2 on Android Studio 4.1.3.
- In your project click on
File > New > Import Module...
and select the/sdk
directory inside your OpenCV download. Give it a meaningful name and wait for the procedure to finish: the directory you selected should have been copied in the root of your project where the default/app
directory resides; - open the Project Structure (for example by clicking on
File > Project Structure...
), then go toDependencies
(on the left), click onapp
and on the+
icon in theDeclared Dependencies
tab (not the one in theModules
tab); - click on
Module Dependency
and select the checkbox for the OpenCV SDK that you imported earlier. You should now see it in the list with the other dependencies, so click onApply
andOK
to exit from the Project Structure; - open the
build.gradle
file of your app module, copy the values ofcompileSdkVersion
,minSdkVersion
andtargetSdkVersion
, then paste them in thebuild.gradle
file of the OpenCV module replacing the default ones so they match exactly. You can also update thesourceCompatibility
andtargetCompatibility
fields toJavaVersion.VERSION_1_8
; - finally, sync your project with Gradle files.
To check if it works, add this snippet to your code, for example in MainActivity
:
if (OpenCVLoader.initDebug()) {
Log.d("myTag", "OpenCV loaded")
}
OpenCV in Android Studio
The below steps for using Android OpenCV sdk in Android Studio. This is a simplified version of this(1) SO answer.
- Download latest OpenCV sdk for Android from OpenCV.org and decompress the zip file.
- Import OpenCV to Android Studio, From File -> New -> Import Module, choose sdk/java folder in the unzipped opencv archive.
- Update build.gradle under imported OpenCV module to update 4 fields to match your project build.gradle a) compileSdkVersion b) buildToolsVersion c) minSdkVersion and d) targetSdkVersion.
- Add module dependency by Application -> Module Settings, and select the Dependencies tab. Click + icon at bottom, choose Module Dependency and select the imported OpenCV module.
- For Android Studio v1.2.2, to access to Module Settings :
in the project view, right-click the dependent module
-> Open Module Settings
- For Android Studio v1.2.2, to access to Module Settings :
- Copy libs folder under sdk/native to Android Studio under app/src/main.
- In Android Studio, rename the copied libs directory to jniLibs and we are done.
Step (6) is since Android studio expects native libs in app/src/main/jniLibs
instead of older libs
folder. For those new to Android OpenCV, don't miss below steps
- include
static{ System.loadLibrary("opencv_java"); }
(Note: for OpenCV version 3 at this step you should instead load the libraryopencv_java3
.) - For step(5), if you ignore any platform libs like x86, make sure your device/emulator is not on that platform.
OpenCV written is in C/C++. Java wrappers are
- Android OpenCV SDK - OpenCV.org maintained Android Java wrapper. I suggest this one.
- OpenCV Java - OpenCV.org maintained auto generated desktop Java wrapper.
- JavaCV - Popular Java wrapper maintained by independent developer(s). Not Android specific. This library might get out of sync with OpenCV newer versions.
Related Topics
How to Sign an Android APK File
Android - Listview Get Item View by Position
How to Set My App as the Default Sms App
Overriding the Home Button - How to Get Rid of the Choice
Android- Going Back to Previous Activity with Different Intent Value
How to Get an APK File from an Android Device
How to Create Circular Progressbar in Android
Include .So Library in APK in Android Studio
Disabling the Fullscreen Editing View for Soft Keyboard Input in Landscape
Deleting a Gallery Image After Camera Intent Photo Taken
Runtimeexception: Unable to Instantiate Application
How to Start Activity in Another Application
How to Add an Array or Object to Sharedpreferences on Android
Adding Custom Radio Buttons in Android
Activity, Appcompatactivity, Fragmentactivity, and Actionbaractivity: When to Use Which