LEADTOOLS Support
General
LEADTOOLS SDK Examples
HOW TO: Package OCR Runtime Files with a Xamarin.Android Project
#1
Posted
:
Thursday, July 11, 2019 4:08:23 PM(UTC)
Groups: Registered, Tech Support
Posts: 6
The LEADTOOLS OCR Engine requires a path to a directory containing certain runtime files before it begins recognizing documents. On some platforms, this directory can be directly packaged with your application. On Android, however, the files must be listed as individual assets independent of one another—the building process for assets does not preserve directory structure.
We resolve this issue by including runtime files as Android assets and reconstructing the directory structure ourselves at runtime. This need only be done once per install.
First, navigate to your Android project’s Assets directory and create a subdirectory called “OcrRuntime.” Then, take any relevant runtime files (such as language helper files or dictionaries for the spellchecking engine) and place them into the subdirectory. In the end, your project’s Assets folder should look something like this (the files themselves may vary, but the structure should be identical):
The following snippet will find any files in the OcrRuntime assets directory and copy them to the device's internal storage. Make sure to do this before calling IOcrEngine.Startup. Since this snippet only copies files which are missing from the device's internal storage, it is safe to call each time the OCR engine is started.
Code:
public static void CopyAssets()
{
Context context = Android.App.Application.Context;
string[] assets = context.Assets.List("OcrRuntime"); // list assets in OcrRuntime subdirectory
var folder = context.GetDir("OcrRuntime", 0).ToString(); // get path to internal storage directory
foreach (string asset in assets)
{
string assetPath = Path.Combine("OcrRuntime/", asset); // get path to asset itself
Stream assetStream = context.Assets.Open(assetPath); // open up the asset
string save = Path.Combine(folder, Path.GetFileName(assetPath));
if (File.Exists(save)) continue; // no need to copy if the file already exists
FileStream outfile = File.Open(save, FileMode.Create);
assetStream.CopyTo(outfile); // copy the asset over
}
}
Later, when you want to start the OCR engine, simply provide a path to the directory the runtime files have been copied into.
Code:
CopyAssets(); // make sure we copy runtime files before we begin
string runtimeDir = Application.Context.GetDir("OcrRuntime", 0).ToString(); // get the folder path as a string
IOcrEngine engine = OcrEngineManager.CreateEngine(OcrEngineType.LEAD, false);
engine.Startup(null, null, null, runtimeDir); // hand the folder path to the engine when calling startup
Attached is a sample Xamarin.Android project that copies the runtime files from Android assets into the device's internal storage and starts up the OCR engine. The app performs no actual OCR functionality and is intended only as an example of how the snippets above can be used in practice.
Joe Kerrigan
Intern
LEAD Technologies, Inc.
LEADTOOLS Support
General
LEADTOOLS SDK Examples
HOW TO: Package OCR Runtime Files with a Xamarin.Android Project
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.