Opencv4Android擺脫OpenCV manager.apk的安裝
阿新 • • 發佈:2019-02-08
我們知道,在Android手機上利用Opencv來進行相關開發的時候,除了要安裝自己的應用外,還要安裝一個apk-----OpenCV manager.apk。這樣就造成很差的使用者體驗,所以,本文將教會大家怎麼擺脫這個困境。
不知道大家有沒有發現,在這個路徑:./sdk/java/bin/aidl/org/opencv/engine/中有一個aidl檔案 OpenCVEngineInterface.aidl,它裡面是這麼定義的:
package org.opencv.engine; /** * Class provides a Java interface for OpenCV Engine Service. It's synchronous with native OpenCVEngine class. */ interface OpenCVEngineInterface { /** * @return Returns service version. */ int getEngineVersion(); /** * Finds an installed OpenCV library. * @param OpenCV version. * @return Returns path to OpenCV native libs or an empty string if OpenCV can not be found. */ String getLibPathByVersion(String version); /** * Tries to install defined version of OpenCV from Google Play Market. * @param OpenCV version. * @return Returns true if installation was successful or OpenCV package has been already installed. */ boolean installVersion(String version); /** * Returns list of libraries in loading order, separated by semicolon. * @param OpenCV version. * @return Returns names of OpenCV libraries, separated by semicolon. */ String getLibraryList(String version); }
很明顯,Opencv4Android SDK是通過aidl檔案來與OpenCV manager這個應用進行跨程序通訊。
其實,Opencv manager這個應用為我們自己開發的應用提供了庫支援。利用adb命令,進入OpenCV manager應用包的lib路徑檢視一下:
在lib目錄下有檔案:libopencv_java3.so這個動態鏈庫。如下圖,我們以module的方式將Opencv sdk和lib下的庫匯入到我們的工程中:
然後在程式中進行載入呼叫:
private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) { @Override public void onManagerConnected(int status) { switch (status) { case LoaderCallbackInterface.SUCCESS: { Log.i("OpenCV", "OpenCV loaded successfully"); mOpenCvCameraView.enableView(); } break; default: { super.onManagerConnected(status); } break; } } };
@Override protected void onResume() { super.onResume(); if (!OpenCVLoader.initDebug()) { Log.d(TAG, "Internal OpenCV library not found. Using OpenCV Manager for initialization"); OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_3_2_0, this, mLoaderCallback); } else { Log.d(TAG, "OpenCV library found inside package. Using it!"); mLoaderCallback.onManagerConnected(LoaderCallbackInterface.SUCCESS); } }
然後在這個module的build.gradle檔案中加入對庫的引用:
sourceSets {
main {
jniLibs.srcDirs = ['libs']
}
debug.setRoot('build-types/debug')
release.setRoot('build-types/release')
}
BTW:如果在我們自己的應用中加入這些庫,勢必會導致我們自己的應用變得很大很大,也是一種弊端。但畢竟魚與熊掌不可兼得,大家都懂。
希望這篇文章對大家有所啟發,謝謝!