Loader classes for OpenCV with Android service added. Library loading in OpenCV API removed.
This commit is contained in:
parent
ac8f6b02de
commit
a9d9eb1ab6
165
modules/java/src/java/AsyncServiceHelper.java
Normal file
165
modules/java/src/java/AsyncServiceHelper.java
Normal file
@ -0,0 +1,165 @@
|
|||||||
|
package org.opencv.android;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.util.StringTokenizer;
|
||||||
|
|
||||||
|
import org.opencv.engine.OpenCVEngineInterface;
|
||||||
|
|
||||||
|
import android.content.ComponentName;
|
||||||
|
import android.content.Context;
|
||||||
|
import android.content.Intent;
|
||||||
|
import android.content.ServiceConnection;
|
||||||
|
import android.os.IBinder;
|
||||||
|
import android.os.RemoteException;
|
||||||
|
import android.util.Log;
|
||||||
|
|
||||||
|
public class AsyncServiceHelper
|
||||||
|
{
|
||||||
|
public static int initOpenCV(String Version, Context AppContext,
|
||||||
|
LoaderCallbackInterface Callback)
|
||||||
|
{
|
||||||
|
AsyncServiceHelper helper = new AsyncServiceHelper(Version, AppContext, Callback);
|
||||||
|
if (AppContext.bindService(new Intent("org.opencv.engine.BIND"),
|
||||||
|
helper.mServiceConnection, Context.BIND_AUTO_CREATE))
|
||||||
|
{
|
||||||
|
return OpenCVLoader.Success;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return OpenCVLoader.NoService;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected AsyncServiceHelper(String Version, Context AppContext,
|
||||||
|
LoaderCallbackInterface Callback)
|
||||||
|
{
|
||||||
|
mOpenCVersion = Version;
|
||||||
|
mUserAppCallback = Callback;
|
||||||
|
mAppContext = AppContext;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected static final String TAG = "OpenCvEngine/Helper";
|
||||||
|
protected OpenCVEngineInterface mEngineService;
|
||||||
|
protected LoaderCallbackInterface mUserAppCallback;
|
||||||
|
protected String mOpenCVersion;
|
||||||
|
protected Context mAppContext;
|
||||||
|
protected int mStatus = OpenCVLoader.Success;
|
||||||
|
|
||||||
|
protected ServiceConnection mServiceConnection = new ServiceConnection()
|
||||||
|
{
|
||||||
|
public void onServiceConnected(ComponentName className, IBinder service)
|
||||||
|
{
|
||||||
|
Log.d(TAG, "Service connection created");
|
||||||
|
mEngineService = OpenCVEngineInterface.Stub.asInterface(service);
|
||||||
|
if (null == mEngineService)
|
||||||
|
{
|
||||||
|
Log.d(TAG, "Engine connection fails. May be service was not installed?");
|
||||||
|
mStatus = OpenCVLoader.NoService;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
Log.d(TAG, "Trying to get library path");
|
||||||
|
String path = mEngineService.getLibPathByVersion(mOpenCVersion);
|
||||||
|
if ((null == path) || (path.length() == 0))
|
||||||
|
{
|
||||||
|
if (mEngineService.installVersion(mOpenCVersion))
|
||||||
|
{
|
||||||
|
mStatus = OpenCVLoader.RestartRequired;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Log.d(TAG, "OpenCV package was not installed!");
|
||||||
|
mStatus = OpenCVLoader.MarketError;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Log.d(TAG, "Trying to get library list");
|
||||||
|
String libs = mEngineService.getLibraryList(mOpenCVersion);
|
||||||
|
Log.d(TAG, "Library list: \"" + libs + "\"");
|
||||||
|
Log.d(TAG, "First attempt to load libs");
|
||||||
|
if (initOpenCVLibs(path, libs))
|
||||||
|
{
|
||||||
|
Log.d(TAG, "First attempt to load libs is OK");
|
||||||
|
mStatus = OpenCVLoader.Success;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Log.d(TAG, "First attempt to load libs fails");
|
||||||
|
mStatus = OpenCVLoader.InitFailed;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (RemoteException e)
|
||||||
|
{
|
||||||
|
e.printStackTrace();
|
||||||
|
mStatus = OpenCVLoader.InitFailed;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Log.d(TAG, "Init finished with status " + mStatus);
|
||||||
|
Log.d(TAG, "Unbind from service");
|
||||||
|
mAppContext.unbindService(mServiceConnection);
|
||||||
|
Log.d(TAG, "Calling using callback");
|
||||||
|
mUserAppCallback.onEngineConnected(mStatus);
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean loadLibrary(String AbsPath)
|
||||||
|
{
|
||||||
|
boolean result = true;
|
||||||
|
|
||||||
|
Log.d(TAG, "Trying to load library " + AbsPath);
|
||||||
|
try
|
||||||
|
{
|
||||||
|
System.load(AbsPath);
|
||||||
|
Log.d(TAG, "OpenCV libs init was ok!");
|
||||||
|
}
|
||||||
|
catch(Exception e)
|
||||||
|
{
|
||||||
|
Log.d(TAG, "Cannot load library \"" + AbsPath + "\"");
|
||||||
|
e.printStackTrace();
|
||||||
|
result &= false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean initOpenCVLibs(String Path, String Libs)
|
||||||
|
{
|
||||||
|
Log.d(TAG, "Trying to init OpenCV libs");
|
||||||
|
if ((null != Path) && (Path.length() != 0))
|
||||||
|
{
|
||||||
|
boolean result = true;
|
||||||
|
if ((null != Libs) && (Libs.length() != 0))
|
||||||
|
{
|
||||||
|
Log.d(TAG, "Trying to load libs by dependency list");
|
||||||
|
StringTokenizer splitter = new StringTokenizer(Libs, ";");
|
||||||
|
while(splitter.hasMoreTokens())
|
||||||
|
{
|
||||||
|
String AbsLibraryPath = Path + File.separator + splitter.nextToken();
|
||||||
|
result &= loadLibrary(AbsLibraryPath);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// If dependencies list is not defined or empty
|
||||||
|
String AbsLibraryPath = Path + File.separator + "libopencv_java.so";
|
||||||
|
result &= loadLibrary(AbsLibraryPath);
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Log.d(TAG, "Library path \"" + Path + "\" is empty");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void onServiceDisconnected(ComponentName className)
|
||||||
|
{
|
||||||
|
mEngineService = null;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
6
modules/java/src/java/LoaderCallbackInterface.java
Normal file
6
modules/java/src/java/LoaderCallbackInterface.java
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
package org.opencv.android;
|
||||||
|
|
||||||
|
public interface LoaderCallbackInterface
|
||||||
|
{
|
||||||
|
public void onEngineConnected(int status);
|
||||||
|
};
|
40
modules/java/src/java/OpenCVLoader.java
Normal file
40
modules/java/src/java/OpenCVLoader.java
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
package org.opencv.android;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
import android.util.Log;
|
||||||
|
|
||||||
|
public class OpenCVLoader
|
||||||
|
{
|
||||||
|
public static final int Success = 0;
|
||||||
|
public static final int NoService = 1;
|
||||||
|
public static final int RestartRequired = 2;
|
||||||
|
public static final int MarketError = 3;
|
||||||
|
public static final int InitFailed = 0xff;
|
||||||
|
|
||||||
|
public static int initStatic()
|
||||||
|
{
|
||||||
|
int result;
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
System.loadLibrary("opencv_java");
|
||||||
|
result = Success;
|
||||||
|
}
|
||||||
|
catch(Exception e)
|
||||||
|
{
|
||||||
|
Log.e(TAG, "OpenCV error: Cannot load native library");
|
||||||
|
e.printStackTrace();
|
||||||
|
result = InitFailed;
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int initAsync(String Version, Context AppContext,
|
||||||
|
LoaderCallbackInterface Callback)
|
||||||
|
{
|
||||||
|
return AsyncServiceHelper.initOpenCV(Version, AppContext, Callback);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static final String TAG = "OpenCV/Helper";
|
||||||
|
}
|
@ -113,11 +113,6 @@ public class Utils {
|
|||||||
nMatToBitmap(m.nativeObj, b);
|
nMatToBitmap(m.nativeObj, b);
|
||||||
}
|
}
|
||||||
|
|
||||||
// native stuff
|
|
||||||
static {
|
|
||||||
System.loadLibrary("opencv_java");
|
|
||||||
}
|
|
||||||
|
|
||||||
private static native void nBitmapToMat(Bitmap b, long m_addr);
|
private static native void nBitmapToMat(Bitmap b, long m_addr);
|
||||||
|
|
||||||
private static native void nMatToBitmap(long m_addr, Bitmap b);
|
private static native void nMatToBitmap(long m_addr, Bitmap b);
|
||||||
|
@ -1069,13 +1069,6 @@ public class Mat {
|
|||||||
return nativeObj;
|
return nativeObj;
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
|
||||||
// native stuff
|
|
||||||
//
|
|
||||||
static {
|
|
||||||
System.loadLibrary("opencv_java");
|
|
||||||
}
|
|
||||||
|
|
||||||
// C++: Mat::Mat()
|
// C++: Mat::Mat()
|
||||||
private static native long n_Mat();
|
private static native long n_Mat();
|
||||||
|
|
||||||
|
@ -194,12 +194,6 @@ public class VideoCapture {
|
|||||||
super.finalize();
|
super.finalize();
|
||||||
}
|
}
|
||||||
|
|
||||||
// native stuff
|
|
||||||
|
|
||||||
static {
|
|
||||||
System.loadLibrary("opencv_java");
|
|
||||||
}
|
|
||||||
|
|
||||||
// C++: VideoCapture::VideoCapture()
|
// C++: VideoCapture::VideoCapture()
|
||||||
private static native long n_VideoCapture();
|
private static native long n_VideoCapture();
|
||||||
|
|
||||||
|
@ -721,10 +721,4 @@ public class Converters {
|
|||||||
llb.add(lb);
|
llb.add(lb);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static {
|
|
||||||
System.loadLibrary("opencv_java");
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user