OpenCV standalone installer opencv

2020-07-14 09:46发布

问题:

I am working on an OpenCV app, and it works fine but when I try to install the app it asks to download the OpenCV manager, and which i dont want. I want to make a stand alone installer for OpenCV app which includes the OpenCV manager inbuit into it ? I did check this link but still it shows pop up to install opencv?

回答1:

in my experience you maybe just need to do these 2(or 3) steps:

1.change the load lib code in java like this:

static {
  Log.i(TAG,"OpenCV library load!");
  if (!OpenCVLoader.initDebug()) {
    Log.i(TAG,"OpenCV load not successfully");
  }
  else {
    System.loadLibrary("opencv_java");// load other libraries
  }
}

2.delete this code in java:

OpenCVLoader.initAsync(OpenCVLoader.OpenCV_VERSION_2_4_3, this, mLoaderCallback);

3.if you have add ndk code to your project, maybe you have to edit Android.mk file like this:

OpenCV_CAMERA_MODULES:=on
OpenCV_INSTALL_MODULES:=on
OpenCV_LIB_TYPE:=SHARED


回答2:

Solution

When you provide libopencv_java3.so with your APK the OpenCV loader doesn't need the OpenCV Manager

Simply copy the OpenCV libs folder (see Context below) into the folder containing your AndroidManifext.xml. Now rename this libs folder into jniLibs. The result should look like the structure below. Props to this post. There's also a bit more details.

android studio project/
├──libs/
|  └── *.jar       <-- if your library has jar files, they go here
├──src/
   └── main/
       ├── AndroidManifest.xml
       ├── java/
       └── jniLibs/ 
           ├── arm64-v8a/                       <-- ARM 64bit
           │   └── libopencv_java3.so
           ├── armeabi-v7a/                     <-- ARM 32bit
           │   └── libopencv_java3.so
           ├── x86/                             <-- Intel 32bit
           │   └── libopencv_java3.so
           └── ...
               └── libopencv_java3.so
  

Now build your APK file. Make sure the libraries are properly provided within your app. Here is your APK after building:

[project name]\[app name]\build\outputs\apk\debug

Just open your APK with a Zip program of your choice. The above listed *.so files should be listed in the APK here:

APK file/
├──lib/
|  ├── arm64-v8a/                       
|  │   └── libopencv_java3.so
|  ├── armeabi-v7a/                     
|  │   └── libopencv_java3.so
|  ├── x86/                             
|  │   └── libopencv_java3.so
|  └── ...
|      └── libopencv_java3.so
├─ ...

Using this APK should result in OpenCV no longer looking for the OpenCV Manager App. Of course your APK grows very large by providing the library for every ABI. Depending on your use case it might become useful to only provide the libraries for the targeted ABIs/platforms. It is possible to create filters for supported ABIs. In case the is relevant start reading here.

ENJOY!


Context

At runtime your application requires the OpenCV library file libopencv_java3.so. This file comes in different versions depending on the target platform where you want to use it. Have a look at your local OpenCV SDK directory at this location:

  • [OpenCV SDK]\sdk\native\libs\

Here you'll find a short list of provided ABIs/platforms via providing the respective OpenCV library file:

  • armeabi
  • armeabi-v7a
  • x86
  • x86_64
  • arm64-v8a

Android OpenCV Manager

From what I understood the OpenCV Manager App does nothing else but provide the required OpenCV library to an app when called from:

if (!OpenCVLoader.initDebug()) {
        Log.d(TAG, "Internal OpenCV library not found. Using OpenCV Manager for initialization");
        OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_3_0_0, this, mLoaderCallback);
    } else {
        Log.d(TAG, "OpenCV library found inside package. Using it!");
        mLoaderCallback.onManagerConnected(LoaderCallbackInterface.SUCCESS);
    }

This makes sense when you have multiple applications requiring the OpenCV libraries. So you would only store it a single time on your mobile device instead of as part of every app.

When you look up what OpenCVLoader.initDebug() does in the OpenCV sources you'll end up at this statement:

loadLibrary("opencv_java3")

So basically the OpenCVLoader tries to find the OpenCV library file within your APK before querying the OpenCV Manager.