ML Community Day is November 9! Join us for updates from TensorFlow, JAX, and more Learn more

TaskJniUtils

public class TaskJniUtils

JNI utils for Task API.

Nested Classes

interface TaskJniUtils.EmptyHandleProvider Syntax sugar to get nativeHandle from empty param list. 
interface TaskJniUtils.FdAndOptionsHandleProvider<T> Syntax sugar to get nativeHandle from file descriptor and options. 
interface TaskJniUtils.MultipleBuffersHandleProvider Syntax sugar to get nativeHandle from an array of ByteBuffers. 

Constants

long INVALID_POINTER

Public Methods

static <T> long
createHandleFromFdAndOptions(Context context, FdAndOptionsHandleProvider<T> provider, String libName, String filePath, T options)
Initializes the JNI and returns C++ handle with file descriptor and options for task API.
static long
createHandleFromLibrary(TaskJniUtils.EmptyHandleProvider provider, String libName)
Initializes the JNI and returns C++ handle by first loading the C++ library and then invokes TaskJniUtils.EmptyHandleProvider.createHandle().
static long
createHandleWithMultipleAssetFilesFromLibrary(Context context, TaskJniUtils.MultipleBuffersHandleProvider provider, String libName, String... filePaths)
Initializes the JNI and returns C++ handle by first loading the C++ library and then invokes TaskJniUtils.MultipleBuffersHandleProvider.createHandle(ByteBuffer).
static long
static long
static MappedByteBuffer
loadMappedFile(Context context, String filePath)
Loads a file from the asset folder through memory mapping.
static void
tryLoadLibrary(String libName)
Try loading a native library, if it's already loaded return directly.

Inherited Methods

Constants

public static final long INVALID_POINTER

Constant Value: 0

Public Methods

public static long createHandleFromFdAndOptions (Context context, FdAndOptionsHandleProvider<T> provider, String libName, String filePath, T options)

Initializes the JNI and returns C++ handle with file descriptor and options for task API.

Parameters
context the Android app context
provider provider to get C++ handle, usually returned from native call
libName name of C++ lib to be loaded
filePath path of the file to be loaded
options options to set up the task API, used by the provider
Returns
  • C++ handle as long
Throws
IOException If model file fails to load.

public static long createHandleFromLibrary (TaskJniUtils.EmptyHandleProvider provider, String libName)

Initializes the JNI and returns C++ handle by first loading the C++ library and then invokes TaskJniUtils.EmptyHandleProvider.createHandle().

Parameters
provider provider to get C++ handle, usually returned from native call
libName
Returns
  • C++ handle as long

public static long createHandleWithMultipleAssetFilesFromLibrary (Context context, TaskJniUtils.MultipleBuffersHandleProvider provider, String libName, String... filePaths)

Initializes the JNI and returns C++ handle by first loading the C++ library and then invokes TaskJniUtils.MultipleBuffersHandleProvider.createHandle(ByteBuffer).

Parameters
context app context
provider provider to get C++ pointer, usually returned from native call
libName name of C++ lib to load
filePaths file paths to load
Returns
  • C++ pointer as long
Throws
IOException If model file fails to load.

public static long createProtoBaseOptionsHandle (BaseOptions baseOptions)

Parameters
baseOptions

public static long createProtoBaseOptionsHandleWithLegacyNumThreads (BaseOptions baseOptions, int legacyNumThreads)

Parameters
baseOptions
legacyNumThreads

public static MappedByteBuffer loadMappedFile (Context context, String filePath)

Loads a file from the asset folder through memory mapping.

Parameters
context Application context to access assets.
filePath Asset path of the file.
Returns
  • the loaded memory mapped file.
Throws
IOException If model file fails to load.

public static void tryLoadLibrary (String libName)

Try loading a native library, if it's already loaded return directly.

Parameters
libName name of the lib