Opencv in Android Studio

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.

  1. Download latest OpenCV sdk for Android from OpenCV.org and decompress the zip file.
  2. Import OpenCV to Android Studio, From File -> New -> Import Module, choose sdk/java folder in the unzipped opencv archive.
  3. 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.
  4. 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
  5. Copy libs folder under sdk/native to Android Studio under app/src/main.
  6. 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 library opencv_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

  1. Android OpenCV SDK - OpenCV.org maintained Android Java wrapper. I suggest this one.
  2. OpenCV Java - OpenCV.org maintained auto generated desktop Java wrapper.
  3. 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.

  1. 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;
  2. open the Project Structure (for example by clicking on File > Project Structure...), then go to Dependencies (on the left), click on app and on the + icon in the Declared Dependencies tab (not the one in the Modules tab);
  3. 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 on Apply and OK to exit from the Project Structure;
  4. open the build.gradle file of your app module, copy the values of compileSdkVersion, minSdkVersion and targetSdkVersion, then paste them in the build.gradle file of the OpenCV module replacing the default ones so they match exactly. You can also update the sourceCompatibility and targetCompatibility fields to JavaVersion.VERSION_1_8;
  5. 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

  1. copy sdk folder in your project directory

  2. Add below line in settings.gradle

    include ':sdk'

  3. click sync now

  4. 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:

  1. Create an Android Project
  2. File -> New -> Import Module -> path to OpenCV/sdk/ -not java folder! (opencv 4+)
  3. File -> Project Structure -> add OpenCV as 'Module Dependency' of 'app'
  4. Clean Project
  5. Done! no need to copy jni libs and other things.

Trouble using OpenCV in Android

These are the steps that worked for me.

  1. 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
  2. Unzip the file named opencv--android-sdk
  3. In Android Studio, go to File > New > Import Module... and the choose /sdk and wait for the sync to finish.
  4. Include open-cv as a module dependency. File -> Project Structure.
  5. Select the project module(not the open-cv module we added in step 3)
  6. Add the opencv as a module dependency.
  7. Check in the build.gradle file is it has been added as a dependency.
  8. 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.

  1. Download opencv sdk for android at: https://opencv.org/releases.html
  2. Unzip opencv sdk and place it in the same root of sample project folder

    -> MyProjects

    ->OpenCV-android-sdk
    ->OpenCVSample
  3. In 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')

  1. 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



Leave a reply



Submit