adding OpenCV Manager

This commit is contained in:
Andrey Pavlenko
2012-06-21 14:50:05 +00:00
parent a3be73b5cc
commit 2984fa751e
133 changed files with 34065 additions and 84 deletions

View File

@@ -717,12 +717,12 @@ $imports
public class %(jc)s {
""" % { 'm' : self.module, 'jc' : jname } )
self.java_code[class_name]["jn_code"].write("""
//
// native stuff
//
static { System.loadLibrary("opencv_java"); }
""" )
# self.java_code[class_name]["jn_code"].write("""
# //
# // native stuff
# //
# static { System.loadLibrary("opencv_java"); }
#""" )

View File

@@ -248,7 +248,7 @@ if __name__ == "__main__":
print "Parsing documentation..."
parser = rst_parser.RstParser(hdr_parser.CppHeaderParser())
for m in allmodules:
parser.parse(m, os.path.join(selfpath, "../" + m))
parser.parse(m, os.path.join(selfpath, "../../" + m))
parser.printSummary()

View File

@@ -719,14 +719,14 @@ if __name__ == "__main__":
verbose = True
rst_parser_dir = os.path.dirname(os.path.abspath(sys.argv[0]))
hdr_parser_path = os.path.join(rst_parser_dir, "../python/src2")
hdr_parser_path = os.path.join(rst_parser_dir, "../../python/src2")
sys.path.append(hdr_parser_path)
import hdr_parser
module = sys.argv[1]
if module != "all" and not os.path.isdir(os.path.join(rst_parser_dir, "../" + module)):
if module != "all" and not os.path.isdir(os.path.join(rst_parser_dir, "../../" + module)):
print "RST parser error E%03d: module \"%s\" could not be found." % (ERROR_010_NOMODULE, module)
exit(1)
@@ -734,9 +734,9 @@ if __name__ == "__main__":
if module == "all":
for m in allmodules:
parser.parse(m, os.path.join(rst_parser_dir, "../" + m))
parser.parse(m, os.path.join(rst_parser_dir, "../../" + m))
else:
parser.parse(module, os.path.join(rst_parser_dir, "../" + module))
parser.parse(module, os.path.join(rst_parser_dir, "../../" + module))
# summary
parser.printSummary()

View File

@@ -0,0 +1,279 @@
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.net.Uri;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
class AsyncServiceHelper
{
public static boolean initOpenCV(String Version, final Context AppContext,
final 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 true;
}
else
{
AppContext.unbindService(helper.mServiceConnection);
InstallService(AppContext, Callback);
return false;
}
}
protected AsyncServiceHelper(String Version, Context AppContext,
LoaderCallbackInterface Callback)
{
mOpenCVersion = Version;
mUserAppCallback = Callback;
mAppContext = AppContext;
}
protected static final String TAG = "OpenCVManager/Helper";
protected static final int MINIMUM_ENGINE_VERSION = 1;
protected OpenCVEngineInterface mEngineService;
protected LoaderCallbackInterface mUserAppCallback;
protected String mOpenCVersion;
protected Context mAppContext;
protected int mStatus = LoaderCallbackInterface.SUCCESS;
private static void InstallService(final Context AppContext, final LoaderCallbackInterface Callback)
{
InstallCallbackInterface InstallQuery = new InstallCallbackInterface() {
private Context mAppContext = AppContext;
private LoaderCallbackInterface mUserAppCallback = Callback;
public String getPackageName()
{
return "OpenCV Manager Service";
}
public void install() {
Log.d(TAG, "Trying to install OpenCV Manager via Google Play");
boolean result = true;
try
{
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(OPEN_CV_SERVICE_URL));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mAppContext.startActivity(intent);
}
catch(Exception e)
{
result = false;
}
if (result)
{
int Status = LoaderCallbackInterface.RESTART_REQUIRED;
Log.d(TAG, "Init finished with status " + Status);
Log.d(TAG, "Calling using callback");
mUserAppCallback.onManagerConnected(Status);
}
else
{
Log.d(TAG, "OpenCV package was not installed!");
int Status = LoaderCallbackInterface.MARKET_ERROR;
Log.d(TAG, "Init finished with status " + Status);
Log.d(TAG, "Unbind from service");
Log.d(TAG, "Calling using callback");
mUserAppCallback.onManagerConnected(Status);
}
}
public void cancel()
{
Log.d(TAG, "OpenCV library installation was canceled");
int Status = LoaderCallbackInterface.INSTALL_CANCELED;
Log.d(TAG, "Init finished with status " + Status);
Log.d(TAG, "Calling using callback");
mUserAppCallback.onManagerConnected(Status);
}
};
Callback.onPackageInstall(InstallQuery);
}
/**
* URI for OpenCV Manager on Google Play (Android Market)
*/
protected static final String OPEN_CV_SERVICE_URL = "market://details?id=org.opencv.engine";
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, "OpenCV Manager Service connection fails. May be service was not installed?");
InstallService(mAppContext, mUserAppCallback);
}
else
{
try
{
if (mEngineService.getEngineVersion() < MINIMUM_ENGINE_VERSION)
{
mStatus = LoaderCallbackInterface.INCOMPATIBLE_MANAGER_VERSION;
Log.d(TAG, "Init finished with status " + mStatus);
Log.d(TAG, "Unbind from service");
mAppContext.unbindService(mServiceConnection);
Log.d(TAG, "Calling using callback");
mUserAppCallback.onManagerConnected(mStatus);
return;
}
Log.d(TAG, "Trying to get library path");
String path = mEngineService.getLibPathByVersion(mOpenCVersion);
if ((null == path) || (path.length() == 0))
{
InstallCallbackInterface InstallQuery = new InstallCallbackInterface() {
public String getPackageName()
{
return "OpenCV library";
}
public void install() {
Log.d(TAG, "Trying to install OpenCV lib via Google Play");
try
{
if (mEngineService.installVersion(mOpenCVersion))
{
mStatus = LoaderCallbackInterface.RESTART_REQUIRED;
}
else
{
Log.d(TAG, "OpenCV package was not installed!");
mStatus = LoaderCallbackInterface.MARKET_ERROR;
}
} catch (RemoteException e) {
e.printStackTrace();
mStatus = LoaderCallbackInterface.INIT_FAILED;
}
Log.d(TAG, "Init finished with status " + mStatus);
Log.d(TAG, "Unbind from service");
mAppContext.unbindService(mServiceConnection);
Log.d(TAG, "Calling using callback");
mUserAppCallback.onManagerConnected(mStatus);
}
public void cancel() {
Log.d(TAG, "OpenCV library installation was canceled");
mStatus = LoaderCallbackInterface.INSTALL_CANCELED;
Log.d(TAG, "Init finished with status " + mStatus);
Log.d(TAG, "Unbind from service");
mAppContext.unbindService(mServiceConnection);
Log.d(TAG, "Calling using callback");
mUserAppCallback.onManagerConnected(mStatus);
}
};
mUserAppCallback.onPackageInstall(InstallQuery);
return;
}
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 = LoaderCallbackInterface.SUCCESS;
}
else
{
Log.d(TAG, "First attempt to load libs fails");
mStatus = LoaderCallbackInterface.INIT_FAILED;
}
Log.d(TAG, "Init finished with status " + mStatus);
Log.d(TAG, "Unbind from service");
mAppContext.unbindService(mServiceConnection);
Log.d(TAG, "Calling using callback");
mUserAppCallback.onManagerConnected(mStatus);
}
}
catch (RemoteException e)
{
e.printStackTrace();
mStatus = LoaderCallbackInterface.INIT_FAILED;
Log.d(TAG, "Init finished with status " + mStatus);
Log.d(TAG, "Unbind from service");
mAppContext.unbindService(mServiceConnection);
Log.d(TAG, "Calling using callback");
mUserAppCallback.onManagerConnected(mStatus);
}
}
}
public void onServiceDisconnected(ComponentName className)
{
mEngineService = null;
}
};
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(UnsatisfiedLinkError 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;
}
}
}

View File

@@ -0,0 +1,126 @@
package org.opencv.android;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.util.Log;
/**
* Basic implementation of LoaderCallbackInterface
*/
public abstract class BaseLoaderCallback implements LoaderCallbackInterface {
public BaseLoaderCallback(Activity AppContext) {
mAppContext = AppContext;
}
public void onManagerConnected(int status)
{
switch (status)
{
/** OpenCV initialization was successful. **/
case LoaderCallbackInterface.SUCCESS:
{
/** Application must override this method to handle successful library initialization **/
} break;
/** OpenCV Manager or library package installation is in progress. Restart of application is required **/
case LoaderCallbackInterface.RESTART_REQUIRED:
{
Log.d(TAG, "OpenCV downloading. App restart is needed!");
AlertDialog RestartMessage = new AlertDialog.Builder(mAppContext).create();
RestartMessage.setTitle("App restart is required");
RestartMessage.setMessage("Application will be closed now. Start it when installation will be finished!");
RestartMessage.setCancelable(false); // This blocks the 'BACK' button
RestartMessage.setButton("OK", new OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
mAppContext.finish();
}
});
RestartMessage.show();
} break;
/** OpenCV loader cannot start Google Play **/
case LoaderCallbackInterface.MARKET_ERROR:
{
Log.d(TAG, "Google Play service is not installed! You can get it here");
AlertDialog MarketErrorMessage = new AlertDialog.Builder(mAppContext).create();
MarketErrorMessage.setTitle("OpenCV Manager");
MarketErrorMessage.setMessage("Package installation failed!");
MarketErrorMessage.setCancelable(false); // This blocks the 'BACK' button
MarketErrorMessage.setButton("OK", new OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
mAppContext.finish();
}
});
MarketErrorMessage.show();
} break;
/** Package installation was canceled **/
case LoaderCallbackInterface.INSTALL_CANCELED:
{
Log.d(TAG, "OpenCV library instalation was canceled by user");
mAppContext.finish();
} break;
/** Application is incompatible with this version of OpenCV Manager. Possible Service update is needed **/
case LoaderCallbackInterface.INCOMPATIBLE_MANAGER_VERSION:
{
Log.d(TAG, "OpenCV Manager Service is uncompatible with this app!");
AlertDialog IncomatibilityMessage = new AlertDialog.Builder(mAppContext).create();
IncomatibilityMessage.setTitle("OpenCV Manager");
IncomatibilityMessage.setMessage("OpenCV Manager service is incompatible with this app. Update it!");
IncomatibilityMessage.setCancelable(false); // This blocks the 'BACK' button
IncomatibilityMessage.setButton("OK", new OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
mAppContext.finish();
}
});
IncomatibilityMessage.show();
}
/** Other status, i.e. INIT_FAILED **/
default:
{
Log.e(TAG, "OpenCV loading failed!");
AlertDialog InitFailedDialog = new AlertDialog.Builder(mAppContext).create();
InitFailedDialog.setTitle("OpenCV error");
InitFailedDialog.setMessage("OpenCV was not initialised correctly. Application will be shut down");
InitFailedDialog.setCancelable(false); // This blocks the 'BACK' button
InitFailedDialog.setButton("OK", new OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
mAppContext.finish();
}
});
InitFailedDialog.show();
} break;
}
}
public void onPackageInstall(final InstallCallbackInterface callback)
{
AlertDialog InstallMessage = new AlertDialog.Builder(mAppContext).create();
InstallMessage.setTitle("Package not found");
InstallMessage.setMessage(callback.getPackageName() + " package was not found! Try to install it?");
InstallMessage.setCancelable(false); // This blocks the 'BACK' button
InstallMessage.setButton("Yes", new OnClickListener()
{
public void onClick(DialogInterface dialog, int which)
{
callback.install();
}
});
InstallMessage.setButton2("No", new OnClickListener() {
public void onClick(DialogInterface dialog, int which)
{
callback.cancel();
}
});
InstallMessage.show();
}
protected Activity mAppContext;
private final static String TAG = "OpenCVLoader/BaseLoaderCallback";
}

View File

@@ -0,0 +1,21 @@
package org.opencv.android;
/**
* Installation callback interface
*/
public interface InstallCallbackInterface
{
/**
* Target package name
* @return Return target package name
*/
public String getPackageName();
/**
* Installation of package is approved
*/
public void install();
/**
* Installation canceled
*/
public void cancel();
};

View File

@@ -0,0 +1,44 @@
package org.opencv.android;
/**
* Interface for callback object in case of asynchronous initialization of OpenCV
*/
public interface LoaderCallbackInterface
{
/**
* OpenCV initialization finished successfully
*/
static final int SUCCESS = 0;
/**
* OpenCV library installation via Google Play service was initialized. Application restart is required
*/
static final int RESTART_REQUIRED = 1;
/**
* Google Play (Android Market) cannot be invoked
*/
static final int MARKET_ERROR = 2;
/**
* OpenCV library installation was canceled by user
*/
static final int INSTALL_CANCELED = 3;
/**
* Version of OpenCV Manager Service is incompatible with this app. Service update is needed
*/
static final int INCOMPATIBLE_MANAGER_VERSION = 4;
/**
* OpenCV library initialization failed
*/
static final int INIT_FAILED = 0xff;
/**
* Callback method that is called after OpenCV library initialization
* @param status Status of initialization. See Initialization status constants
*/
public void onManagerConnected(int status);
/**
* Callback method that is called in case when package installation is needed
* @param callback Answer object with approve and cancel methods and package description
*/
public void onPackageInstall(InstallCallbackInterface callback);
};

View File

@@ -0,0 +1,41 @@
package org.opencv.android;
import android.content.Context;
/**
* Helper class provides common initialization methods for OpenCV library
*/
public class OpenCVLoader
{
/**
* OpenCV Library version 2.4.0
*/
public static final String OPEN_CV_VERSION_2_4_0 = "2.4.0";
/**
* OpenCV Library version 2.4.0
*/
public static final String OPEN_CV_VERSION_2_4_2 = "2.4.2";
/**
* Load and initialize OpenCV library from current application package. Roughly it is analog of system.loadLibrary("opencv_java")
* @return Return true is initialization of OpenCV was successful
*/
public static boolean initDebug()
{
return StaticHelper.initOpenCV();
}
/**
* Load and initialize OpenCV library using OpenCV Engine service.
* @param Version OpenCV Library version
* @param AppContext Application context for connecting to service
* @param Callback Object, that implements LoaderCallbackInterface for handling Connection status
* @return Return true if initialization of OpenCV starts successfully
*/
public static boolean initAsync(String Version, Context AppContext,
LoaderCallbackInterface Callback)
{
return AsyncServiceHelper.initOpenCV(Version, AppContext, Callback);
}
}

View File

@@ -0,0 +1,89 @@
package org.opencv.android;
import java.util.StringTokenizer;
import android.util.Log;
class StaticHelper {
public static boolean initOpenCV()
{
boolean result;
String libs = "";
Log.d(TAG, "Trying to get library list");
try
{
System.loadLibrary("opencvinfo");
libs = getLibraryList();
}
catch(UnsatisfiedLinkError e)
{
Log.e(TAG, "OpenCV error: Cannot load info library for OpenCV");
}
Log.d(TAG, "Library list: \"" + libs + "\"");
Log.d(TAG, "First attempt to load libs");
if (initOpenCVLibs(libs))
{
Log.d(TAG, "First attempt to load libs is OK");
result = true;
}
else
{
Log.d(TAG, "First attempt to load libs fails");
result = false;
}
return result;
}
private static boolean loadLibrary(String Name)
{
boolean result = true;
Log.d(TAG, "Trying to load library " + Name);
try
{
System.loadLibrary(Name);
Log.d(TAG, "OpenCV libs init was ok!");
}
catch(UnsatisfiedLinkError e)
{
Log.d(TAG, "Cannot load library \"" + Name + "\"");
e.printStackTrace();
result &= false;
}
return result;
}
private static boolean initOpenCVLibs(String Libs)
{
Log.d(TAG, "Trying to init OpenCV libs");
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())
{
result &= loadLibrary(splitter.nextToken());
}
}
else
{
// If dependencies list is not defined or empty
result &= loadLibrary("opencv_java");
}
return result;
}
private static final String TAG = "OpenCV/StaticHelper";
private static native String getLibraryList();
}

View File

@@ -113,11 +113,6 @@ public class Utils {
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 nMatToBitmap(long m_addr, Bitmap b);

View File

@@ -0,0 +1,7 @@
#!/usr/bin/python
import os
import shutil
for f in os.listdir("."):
shutil.copyfile(f, os.path.join("../../../../../../modules/java/generator/src/java/", "android+" + f));

View File

@@ -1082,13 +1082,6 @@ public class Mat {
return nativeObj;
}
//
// native stuff
//
static {
System.loadLibrary("opencv_java");
}
// C++: Mat::Mat()
private static native long n_Mat();

View File

@@ -0,0 +1,33 @@
package org.opencv.engine;
/**
* Class provides Java interface to OpenCV Engine Service. Is synchronous with native OpenCVEngine class.
*/
interface OpenCVEngineInterface
{
/**
* @return Return service version
*/
int getEngineVersion();
/**
* Find installed OpenCV library
* @param OpenCV version
* @return Return path to OpenCV native libs or empty string if OpenCV was not found
*/
String getLibPathByVersion(String version);
/**
* Try to install defined version of OpenCV from Google Play (Android Market).
* @param OpenCV version
* @return Return true if installation was successful or OpenCV package has been already installed
*/
boolean installVersion(String version);
/**
* Return list of libraries in loading order separated by ";" symbol
* @param OpenCV version
* @return Return OpenCV libraries names separated by symbol ";" in loading order
*/
String getLibraryList(String version);
}

View File

@@ -194,12 +194,6 @@ public class VideoCapture {
super.finalize();
}
// native stuff
static {
System.loadLibrary("opencv_java");
}
// C++: VideoCapture::VideoCapture()
private static native long n_VideoCapture();

View File

@@ -721,10 +721,4 @@ public class Converters {
llb.add(lb);
}
}
static {
System.loadLibrary("opencv_java");
}
}