# OCR (C#) : IRIS (iDRS 14.0) Part 2 - Writing some code

August 4, 2011 by

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 (Page page = new Page())
{
using (Doc doc = new Doc())
{
DocPage docPage = doc.ConvertPage(page);
return docPage.GetText();
}
}
}
}

Nothing much to explain here, we pass a language to the OCRContext, which we pass to our Reader class, set a few parameters and pass an image to the page class.

You will notice however that the GetText method on the DocPage class doesn't actually exist, I added this as extension method to simplify retrieving text from a DocPage object, this is what the method looks like:

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();
}
}
}

In the following snippet we pass images to the ImageToText method and save the output to a text file, notice the IDRSLicenses class:

static void Main(string[] args)
{
{
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);
}
}
}
}

The IDRSLicenses class is a class I wrote to simplify the licensing needed to use these classes, observe:

class IDRSLicenses : IDisposable
{

{
SetupKeys();
SetupModules();
}

private void SetupKeys()
{
}

private void SetupModules()
{
}

// We need to unload the IDRSManager once we're done, else we get an ugly exception
public void Dispose()
{
}
}

Just one last rant before I sign off and leave you to fend for yourself and that is about the IDRSException class or should I rather say glorified error code handler?

I really hate the design of this class, if any exception occurs you simply get a message telling you "An error occured in the iDRS" along with an error code (in the IDRSErrorCode property of the exception) which you need to reference in the documentation provided with the SDK.

It would have been a great deal more elegant if the message contained the actual message relating to the error, perhaps consider adding an extension method mapping the error code to an actual useful message like seen in the following snippet:

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).

#### OCR Cloud 2.0 API Information used by OCR-IT LLCSeptember 17, 2011 by OCR API

Yeah message with only error code is not enough.We have to display an user friendly error message.Which you have done in your GetMessage() method.Nice piece of coding.I will definitely try this.Thank for sharing.Although i have used OCR Cloud 2.0 platform which can convert virtually any image (TIF, JPG, PNG, BMP) or PDF to any standard text-based document type (TXT, DOC, RTF, XLS, PPT, XML, HTML) or searchable PDF.For free developer account signup here-http://www.ocr-it.com/ocr-cloud-2-0-api

#### Related Posts

OCR (C#) : IRIS (iDRS 14.0) Part 1 - Getting Started

August 4, 2011