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.
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 Android Studio module importing issue
This Bug is in Android studio Arctic Fox
This issue is not about only OpenCV
module, This is happening with all library modules.
Many people are posted this issue on issuetracker
but nobody get solution from Android studio
team side.
here is some links of that issues posted on issuetracker
.
Link 1 ,
Link 2 ,
Link 3 ,
Link 4 ,
Link 5 ,
Link 6 ,
Link 7
So here is Temporary Workaround
copy
sdk
folder in your project directoryAdd below line in
settings.gradle
include ':sdk'
click
sync now
Done
If you get below error (I got this error while solving this issue)
Plugin with id 'kotlin-android' not found.
add below dependency in root level build.gradle
and click sync now
dependencies {
classpath "com.android.tools.build:gradle:4.2.2"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.21" // add this line
}
Cant import OpenCV module in android studio
First of all make sure your sdk folder in your downloaded opencv sdk contains a build.gradle file. If your downloaded sdk doesn't have build.gradle try another version of opencv sdk, Unless you have to select java folder in the opencv sdk and after that copy jni files into your project tree.
The easiest way to import OpenCV sdk into your project is as follow:
- Create an Android Project
- File -> New -> Import Module -> path to OpenCV/sdk/ -not java folder! (opencv 4+)
- File -> Project Structure -> add OpenCV as 'Module Dependency' of 'app'
- Clean Project
- Done! no need to copy jni libs and other things.
Trouble using OpenCV in Android
These are the steps that worked for me.
- Go to the OpenCV releases page and click on Android pack link of the current version section (https://opencv.org/releases.html). I used 4.1.1
- Unzip the file named opencv--android-sdk
- In Android Studio, go to File > New > Import Module... and the choose /sdk and wait for the sync to finish.
- Include open-cv as a module dependency. File -> Project Structure.
- Select the project module(not the open-cv module we added in step 3)
- Add the opencv as a module dependency.
- Check in the build.gradle file is it has been added as a dependency.
- Clean and build the project.
NOTEs
- Do not import /sdk/java like most tutorials suggest.
- You do not have to copy the native libraries into your project like most tutorials suggest.
Once you have OpenCV imported, use this tutorial to get the preview to fill the preview window.
And then use this version of the JavaCamera2View. JavaCamera2View uses the camera2 api.
These are the steps that worked for me, happy to be corrected if I have done something wrong.
Good Luck :)
How to integrate Python OpenCV code in android studio
A colleague of mine used https://chaquo.com/chaquopy/, it worked well for him.
You can call your python functions directly from Java / Kotlin on Android.
I don't know the details of exactly how he did it, but it was not too hard I think.
android app asks for OpencvManager installation even after including opencv sdk in project and adding dependency
I just have followed this with the latest(like you) version of everything. And it's working! Enjoy!
I am giving my version of instructions thinking about if the link ever gets broken.
- Download opencv sdk for android at: https://opencv.org/releases.html
Unzip opencv sdk and place it in the same root of sample project folder
-> MyProjects
->OpenCV-android-sdk
->OpenCVSampleIn your project (e.g. OpenCVSample), open settings.gradle file and add config below
def opencvsdk = '<path_to_opencv_android_sdk_rootdir>'
include ':opencv'
project(':opencv').projectDir = new File(opencvsdk + '/sdk')
Example with my settings.gradle file
include ':app'
In my case:
rootProject.name='OpenCVSample'
include ':app'
def opencvsdk = '../OpenCV-android-sdk'
include ':opencv'
project(':opencv').projectDir = new File(opencvsdk + '/sdk')
- In app/build.gradle, add opencv module to dependencies
implementation project(':opencv')
Sync now and the environment is ready. After this I copied the manifest, layout and activity codes from your question and it ran exactly like what you wanted.
OpenCV on Android Studio
1. Make sure you have Android SDK up to date, with NDK installed
2. Download latest OpenCV SDK for Android from OpenCV.org and decompress the zip file.
3. Create a new Android Studio project
- Check Include C++ Support
- Choose empty Activity
- In C++ Support, you can check -fexceptions and -frtti
4. Import OpenCV library module
- New -> Import Module
- Choose the YOUR_OPENCV_SDK/sdk/java folder
- Unckeck replace jar, unckeck replace lib, unckeck create gradle-style
5. Set the OpenCV library module up to fit your SDK
Edit openCVLibrary/build.gradle to match your app/build.gradle e.g:
compileSdkVersion 27
defaultConfig {
minSdkVersion 19
targetSdkVersion 27
}
6. Add OpenCV module dependency in your app module
File -> Project structure -> Module app -> Dependencies tab -> New module dependency -> choose OpenCV library module
7. Make a jni folder by right clicking on app/src/main and click change folder box after that rename folder fron jni to jniLibs
8. Copy all files from your opencv directory YOUR_OPENCV_SDK/sdk/native/libs that you have downloaded and paste them in jniLibs folder
9. Set the app build.gradle
Add abiFilters
externalNativeBuild {
cmake {
cppFlags "-frtti -fexceptions"
abiFilters 'x86', 'x86_64', 'armeabi-v7a', 'arm64-v8a'
}
}
10. Configure the CMakeLists.txt file
After the cmake_minimum_required, add
include_directories(YOUR_OPENCV_SDK/sdk/native/jni/include)
add_library( lib_opencv SHARED IMPORTED )
set_target_properties(lib_opencv PROPERTIES IMPORTED_LOCATION ${CMAKE_CURRENT_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}/libopencv_java3.so)
Every thing is done.
Related Topics
This App Won't Run Unless You Update Google Play Services (Via Bazaar)
Basic Communication Between Two Fragments
What's the Difference Between the Various Methods to Get an Android Context
How to Scale an Image in Imageview to Keep the Aspect Ratio
Filter Logcat to Get Only the Messages from My Application in Android
Background Task, Progress Dialog, Orientation Change - Is There Any 100% Working Solution
Automatically Accept All Sdk Licences
Illegalstateexception: Can Not Perform This Action After Onsaveinstancestate With Viewpager
This Handler Class Should Be Static or Leaks Might Occur: Incominghandler
How to Create a Custom-Shaped Bitmap Marker With Android Map API V2
Get Value of a Edit Text Field
Download Binary File from Okhttp
Firebase Data Desc Sorting in Android
Run/Install/Debug Android Applications Over Wi-Fi
Android - Listen For Incoming Sms Messages
Understanding Fragment'S Setretaininstance(Boolean)