33

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?

Zoe
  • 27,060
  • 21
  • 118
  • 148
Vijay007
  • 329
  • 1
  • 3
  • 7
  • Hey did you find a solution which works for the production version? In the link in the accepted answer, it is stated, "It is designed mostly for development purposes. This approach is deprecated for the production code, release package is recommended to communicate with OpenCV Manager via..." – Solace Dec 12 '15 at 13:27
  • Here is a step by step way, with illustrations that show what you should get: http://stackoverflow.com/a/35135495/5611377 – ssimm May 14 '16 at 11:58

4 Answers4

26

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
Community
  • 1
  • 1
Chintan Rathod
  • 25,864
  • 13
  • 83
  • 93
  • 1
    http://docs.opencv.org/doc/tutorials/introduction/android_binary_package/dev_with_OCV_on_Android.html#application-development-with-static-initialization – Tom A Apr 18 '15 at 14:19
  • 2
    Why this approach is not recommended for production usage (according to docs)? – Vova Rozhkov Sep 21 '15 at 06:57
  • Yeah. I saw that. Actually there was no any method like initAsync() before. As the name suggest guessing that this library is now loading beyond UI thread. – Chintan Rathod Sep 21 '15 at 07:03
  • 2
    Have you tried it? I've tried and it asks me to download OpenCV manager which is not acceptable for me. – Vova Rozhkov Sep 28 '15 at 17:49
  • @rozhok i am sorry. i just edit my answer. Previously I tested with debug mode. But this async load i haven't tested. Let me know if its not working. I will update my answer and will implement the same. – Chintan Rathod Sep 29 '15 at 05:16
  • 4
    @ChintanRathod: this approach (use async init) is devoted to call OpenCV manager if it wasn't found on device. So It is not a solution to 'bundle opencv into app'. I've tried good old deprecated way and it works fine. – Vova Rozhkov Sep 30 '15 at 07:19
  • @rozhok greta.. thanks for your update.. will mention this thing in answer. :) – Chintan Rathod Sep 30 '15 at 07:28
  • The first link in your answer says in the second sentence," It is designed mostly for development purposes. This approach is deprecated for the production code..." - Does this mean for the production version, the OpenCV Manager will still not be integrated into the application and the user will have to install it on their own – Solace Dec 12 '15 at 10:58
  • 1
    @Solace, yes. I think I will simply ignore their advice. – Giuseppe Dini Jan 02 '16 at 09:38
  • I tried this, and it is Not working for me =( I followed these steps and then transferred the .apk from bin directory to my phone and installed it. But it still asks to download OpenCV Manager from Google Play when the app is launched. What should I do? – Solace Jan 15 '16 at 14:48
  • I think I made mistake following [this step 3](http://docs.opencv.org/2.4/doc/tutorials/introduction/android_binary_package/dev_with_OCV_on_Android.html#application-development-with-static-initialization): _"If your application project doesn’t have a JNI part, just copy the corresponding OpenCV native libs from /sdk/native/libs/ to your project directory to folder libs/"._ (I don't think my app has a JNI part. It is a simple basic app.) - I copied **OpenCVv2.4.11\OpenCV-android-sdk\sdk\native\libs\x86** directory Did I make a mistake there? – Solace Jan 15 '16 at 15:31
  • Nope. You are on right track. Do Static initialization of library in your application and you are done. – Chintan Rathod Jan 16 '16 at 14:14
  • 2
    So will this work on production apps with the initDebug method instead of the initAsync? I am finding conflicting information on this. I do not want my users to be prompted to install OpenCV Manager on running the app and would like to ship the app with all needed libraries and stuff. Does the initDebug work on production? – Devsil Feb 09 '16 at 15:12
  • Yes. I did that but its around 2 years back.. So based on OpenCV guideline we can use initAsync. Here is [initAsync Example](http://docs.opencv.org/3.0-beta/doc/tutorials/introduction/android_binary_package/dev_with_OCV_on_Android.html). – Chintan Rathod Feb 10 '16 at 05:34
  • Hello, I wanted to ask, I am using Android Studio. In my project there is no mk file. Where am I supposed to add those two additional lines? – Libathos Dec 07 '16 at 09:21
  • @libathos which lines my friend? – Chintan Rathod Dec 07 '16 at 12:17
  • @ChintanRathod excuse me for being so vague. in the linked provided in your answer it suggest to put 2 additional lines in the android.mk file OPENCV_CAMERA_MODULES:=on OPENCV_INSTALL_MODULES:=on My problem is that while I use JNI libraries I do not have a android.mk file shouldn't this be auto generated? – Libathos Dec 07 '16 at 12:21
  • @libathos That part is in JNI. For your convenience, try to check this post. http://stackoverflow.com/questions/27406303/opencv-in-android-studio. As my answer is little bit old :) – Chintan Rathod Dec 07 '16 at 12:33
  • 1
    @ChintanRathod thank you very much for your guidance. :) – Libathos Dec 07 '16 at 12:38
0
  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
    
                }
                }
            }
        };
    
Zoe
  • 27,060
  • 21
  • 118
  • 148
MIkka Marmik
  • 1,101
  • 11
  • 29
0

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

Community
  • 1
  • 1
Luis Sandoval
  • 75
  • 2
  • 9
-6

Just comment the line in following Code

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

}
Vijay007
  • 329
  • 1
  • 3
  • 7