Home » Android » How to integrate OpenCV Manager in Android App

How to integrate OpenCV Manager in Android App

Posted by: admin April 23, 2020 Leave a comment

Questions:

I am using OpenCV2.4.7 Library in my Android app. When app starts its goes to Google Play store for Application called OpenCV Manager. Is there any way to integrate this application in my Android apk because we already using OpenCV library so why app needs OpenCV Engine Again? Is Their any way to integrate this engine?

How to&Answers:

Yes. To integrate OpenCV inside your application, and avoid explicit installation of OpenCV manager, you need to first read following document provided by OpenCV.

First Read -> Static Initialization of OpenCV

After successfully followed steps, you need to write following code to enable OpenCV in your application initialization code before calling OpenCV API. It can be done, for example, in the static section of the Activity class:

static {
    if (!OpenCVLoader.initDebug()) {
        // Handle initialization error
    }
}

References:

  1. http://answers.opencv.org/question/2033/use-opencv-on-android-without-manager/
  2. Static Initialization on OpenCV Android

Edit

As per new scenario in Document and thanks to @rozhok for providing new information, initDebug() method can’t be used for production build

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.

You need to use following method for that

Syntax

static boolean initAsync(String Version, Context AppContext, LoaderCallbackInterface Callback)

Example

public class Sample1Java extends Activity implements CvCameraViewListener {

    private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) {
        @Override
        public void onManagerConnected(int status) {
            switch (status) {
                case LoaderCallbackInterface.SUCCESS:
                {
                    Log.i(TAG, "OpenCV loaded successfully");
                    mOpenCvCameraView.enableView();
                } break;
                default:
                {
                    super.onManagerConnected(status);
                } break;
            }
        }
    };

    @Override
    public void onResume()
    {
        super.onResume();
        OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_6, this, mLoaderCallback);
    }

    ...
}

References

  1. http://docs.opencv.org/doc/tutorials/introduction/android_binary_package/dev_with_OCV_on_Android.html

Answer:

This is what the documentation says about the OpenCV Manager installation:

apk folder contains Android packages that should be installed on the target Android device to enable OpenCV library access via OpenCV Manager API (see details below).

On production devices that have access to Google Play Market (and Internet) these packages will be installed from Market on the first start of an application using OpenCV Manager API.

Note: Installation from Internet is the preferable way since OpenCV team may publish updated versions of this packages on the Market.

You can read more about it here: https://docs.opencv.org/3.0-beta/doc/tutorials/introduction/android_binary_package/O4A_SDK.html#general-info

Answer:

  1. Insert this lines after include $ (CLEAR_VARS) in OpenCV.mk file
    OPENCV_CAMERA_MODULES:=on
    OPENCV_INSTALL_MODULES:=on
    include D:/opencv_with/OpenCV-2.4.10-android-sdk/sdk/native/jni/OpenCV.mk
    
  2. In your current project directory libs folder copy all folder inside OpenCV libs.

  3. Add in your Activity

    if (!OpenCVLoader.initDebug()) {
                Log.d("ERROR", "Unable to load OpenCV");
            } else {
                mLoaderCallback.onManagerConnected(LoaderCallbackInterface.SUCCESS);
            }
    
    private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) {
    
            @Override
            public void onManagerConnected(int status) {
                switch (status) {
                case LoaderCallbackInterface.SUCCESS: {
    
                    //Your opencv Operation code
    
                }
                }
            }
        };
    

Answer:

Just comment the line in following Code

@Override
public void onResume()
{
    super.onResume();
    //OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_3, this, mLoaderCallback);

}