August 4, 2011 by Christoff Truter C#
In part 1 we had a look at how to get the OCR "engine" up and running, in this post we're going have a look at how
to use the SDK itself.
Under the samples folder of the SDK supplied by IRIS you'll find some C++/C# and VB.net examples, but I feel these
samples are too bulky, so in this post I am going to show you in just a few lines of code the basics of using this SDK.
Observe the following snippet:
using IDRSNET;
static string ImageToText(String imagePath)
{
using (OCRContext context = new OCRContext(Languages.English))
using (Reader reader = new Reader())
{
reader.SetOCRParameters(context);
reader.SetOrientationDetectionParameters(true, true);
using (Page page = new Page())
{
using (Doc doc = new Doc())
{
page.LoadSourceImage(imagePath);
reader.ReadFmt(page);
DocPage docPage = doc.ConvertPage(page);
return docPage.GetText();
}
}
}
}
static class Extensions
{
public static String GetText(this DocPage docPage)
{
using (StringWriter sw = new StringWriter())
{
foreach (DocZone zone in docPage.Zones)
{
foreach (DocLine line in zone.Lines)
foreach (DocWord word in line.Words)
{
foreach (DocBlob blob in word.Blobs)
sw.Write(blob.Text);
sw.Write(" ");
}
sw.WriteLine();
}
return sw.ToString();
}
}
}
static void Main(string[] args)
{
using (IDRSLicenses licenses = new IDRSLicenses())
{
FileInfo[] files = new DirectoryInfo(@"c:\ocr\temp").GetFiles("*.jpg");
using (StreamWriter sw = new StreamWriter(@"c:\ocr\text.txt"))
{
foreach (FileInfo file in files)
{
string content = ImageToText(file.FullName);
sw.WriteLine(content);
}
}
}
}
class IDRSLicenses : IDisposable
{
LicenseDRS _licenseDRS = new LicenseDRS();
LicensePreprocessing _licensePrepro = new LicensePreprocessing();
LicenseFormatting _licenseFmt = new LicenseFormatting();
LicenseImageFile _licenseImageFile = new LicenseImageFile();
public IDRSLicenses()
{
SetupKeys();
SetupModules();
}
// Retrieve licenses from app.config
private void SetupKeys()
{
_licenseDRS.LicenseType = _licensePrepro.LicenseType = _licenseFmt.LicenseType = _licenseImageFile.LicenseType = LicenseTypes.Software;
_licenseDRS.SoftwareKey = ConfigurationManager.AppSettings["LicenseDRS"];
_licenseDRS.ResourcesPath = ConfigurationManager.AppSettings["ResourcesPath"];
_licensePrepro.SoftwareKey = ConfigurationManager.AppSettings["LicensePreprocessing"]; ;
_licenseFmt.SoftwareKey = ConfigurationManager.AppSettings["LicenseFormatting"];
_licenseImageFile.SoftwareKey = ConfigurationManager.AppSettings["LicenseImageFile"];
_licenseImageFile.ExtensionsToLoad[ImageFileExtensions.JPEG] = true;
_licenseImageFile.ExtensionsSoftwareKeys[ImageFileExtensions.JPEG] = ConfigurationManager.AppSettings["ImageFileExtensions.JPEG"];
}
private void SetupModules()
{
IDRSManager.SetupModule(ModuleTypes.DRS, _licenseDRS);
IDRSManager.SetupModule(ModuleTypes.Preprocessing, _licensePrepro);
IDRSManager.SetupModule(ModuleTypes.Formatting, _licenseFmt);
IDRSManager.SetupModule(ModuleTypes.ImageFile, _licenseImageFile);
}
// We need to unload the IDRSManager once we're done, else we get an ugly exception
public void Dispose()
{
IDRSManager.Unload();
}
}
public static String GetMessage(this IDRSException ex)
{
switch (ex.IDRSErrorCode)
{
case 6005:
return "Cannot talk to the license server on the specified host. The license server is not running.";
case 6092:
return "The given license code is invalid. Hence, it could not be added to this license server.";
}
return ex.Message;
}
September 17, 2011 by Christoff Truter
Thank you for taking the time to share your product with us, will definitely have a look at it some time soon to assess how it measures up with other solutions (like IRIS).