mirror of https://github.com/opencv/opencv.git
pull/13383/head
parent
ac8f6b02de
commit
a9d9eb1ab6
7 changed files with 2219 additions and 2032 deletions
@ -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; |
||||
} |
||||
}; |
||||
} |
@ -0,0 +1,6 @@ |
||||
package org.opencv.android; |
||||
|
||||
public interface LoaderCallbackInterface |
||||
{ |
||||
public void onEngineConnected(int status); |
||||
}; |
@ -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"; |
||||
} |
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
Loading…
Reference in new issue