How to Use OpenCV in Unity for Android

If you’ve read my previous articles (especially this one) about how to use OpenCV in Unity and how to pass images between them, this post will feel like a breeze and you can just quickly skim through it. However, if you haven’t, I recommend doing so because in this post I’ll describe how to create an Android library that uses the OpenCV library and then include it in your Unity project, or in other words describe what we did in Unity for Windows this time in Unity for Android.



We’ll be using Visual Studio 2019 for this purpose, however creating libraries for Android should be quite a similar process for Visual Studio 2017 as well.

Start by making sure your Visual Studio installation has “Mobile development with C++”. Just run Visual Studio Installer and check the checkbox next to it as seen below:

After that you can run Visual Studio and try creating a “Dynamic Shared Library (Android)” project. Here’s a screenshot that shows setting a C++ and Android filter and then selecting the project type:



After the project is created, choose Release and ARM Configuration for building the project:

Now you need to open up Project Properties (Property Pages) by right clicking on the project from Solution Explorer and choosing Properties. We’re going to configure our project so that it can be built for Android.

First, under “Configuration Properties” / “General”, you need to set the following parameters:

  • Set “Target API Level” to 21 (Lollipop 5.0 – 5.0.2, android-21) or even higher if you can avoid building your project for really low Android versions.
  • Set “Use of STL” to “GNU STL static library (gnustl_static)” (shared would also work but try to do the same to match my configurations)

After that, switch to “VC++ Directories” / “Include Directories” and then add the OpenCV for Android include path to the list of paths already present.

OpenCV include files are usually located in a path similar to the following:

<opencv_for_android_root_path>/sdk/native/jni/include

If you wish to learn how to build OpenCV from sources for Android, you can follow this guide.

Now, switch to “C/C++”, then “Code Generation” and set “Enable C++ Exceptions” to “Yes (-fexceptions)”.



Next parameter in Properties that we need to change is to enable run-time info. To do this, switch to “C/C++”, then “Language” and set “Enable Run-Time Information” to “Yes (-frtti)”

If you don’t have any Precompiled Headers, switch to “C/C++”, then “Precompiled Headers” and set “Precompiled Header” to “Not using Precompiled Headers”.

The next parameter that needs to be set is related to the OpenCV Android libraries. Switch to “Linker”, then “General” and add OpenCV Android libs folder to “Additional Library Directories”. This folder is usually located in a path similar to this:

<opencv_for_android_root_path>/sdk/native/libs/armeabi-v7a

After adding the path to the libraries, we need to add the libraries as well. Switch to “Linker” and then “Input” and add OpenCV Android libs to “Library Dependencies”. It’s the *.so files in libs folder. Omit the starting “lib” and ending “.so” from the file name. For instance if there’s a file called “libopencv_world.so” in there, you need to add “opencv_world” to the list.

That concludes all required configurations. You can now build the library from the previous tutorial or any other library that uses OpenCV for Android and then use it as a plugin in Unity to process images.

The only thing you need to pay attention to in your source code is that you don’t need the following declspec definition in Android to be able to expose your function to the outside world, so simply remove it or add it using Compiler directives only when you are building for Windows.

__declspec(dllexport)

After building your project, you need to make sure it’s copied into “Plugins/Android” folder in your Unity project. The rest is identical to what we did here.



2 Replies to “How to Use OpenCV in Unity for Android”

  1. Hi

    Thnaks for yout tutorial.
    I have tried your metod but im not able to build your example. I´m always having the same error: undefined reference to ‘cv::imread(std::string const&, int)’
    I have look several times in mi Project properties for any errors but I dont find anything wrong.

    Do you know wat can be wrong?

    Thank you very much

    Joel

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.