5

I am using OpenCV for Android in my app using static initialization. The code to initialize is -

static {
    if(!OpenCVLoader.initDebug()) {
        Log.d("My App", "Unable to load OpenCV");
    } else {
        Log.d("My App", "OpenCV loaded");
    }
}

Well this works fine. But in the OpenCV documentation it is specified that initDebug() is deprecated -

Note This method is deprecated for production code. It is designed for experimental and local development purposes only. If you want to publish your app use approach with async initialization.

Now if I use async initialization, it uses OpenCV manager app which should be installed on the device. This is not what I want.

My question is - If initDebug() is deprecated to use in release mode, is there any other way to load openCV which does not use OpenCV Manager? Or is it safe to load OpenCV using initDebug()?

user1930106
  • 779
  • 1
  • 6
  • 19

4 Answers4

3

My answer will be derived from my experience using that library!

OpenCVLoader.initDebug() iterates over some options to load the library into memory using System.loadLibrary("lib_name") at the end and trying to cover you with some logs!

from the OpenCVLoader.initDebug() method documentation

Loads and initializes OpenCV library from current application package. Roughly, it's an analog of System.loadLibrary("opencv_java").

So in the end, if you are going to use the statically loaded library which is more convenient in most cases because you don't want the user to install another application in order to use yours, you shall load the library into memory using System.loadLibrary("lib_name") to be able to use them in your java code and it won't really matter if you loaded it directly or using the initDebug helper method.

Note: New versions of the library have the version appended to the end of the library name so you shall use System.loadLibrary("opencv_java3") or whatever name you have as the name of the library in the jniLibs folder!

Ahmed Hegazy
  • 12,395
  • 5
  • 41
  • 64
2

It's save to use static initialization. I used it and tested a lot in my project.

Derek K
  • 2,756
  • 1
  • 21
  • 37
2

Don't worry about initDebug(), it's safe also in release mode. Should be a problem for future versions of OpenCV for Android. If you don't change the version don't worry.

SimoBrazz
  • 81
  • 7
1

In my attempt to answer this question I quoted:

OpenCVLoader.initDebug() must be used for debugging purposes only as when you are developing locally on your machine. But for production purposes where you need to release the app on Play Store, etc. you must use OpenCVLoader.initAsync(). Actually initializing the OpenCVLoader takes some time depending upon the phone. So if you load it uisng initDebug(), then it would be executed in the main thread, which may block the UI for a fractional time. So it is advised to load the OpenCV in background which can be achieved using initAsync()

ZdaR
  • 22,343
  • 7
  • 66
  • 87
  • The referenced answer is written authoritatively, but I question it's correctness. For what reason must one use initAsync() for prod? Other sources say that initDebug() is fine for production. – steve May 09 '23 at 16:33