Friday, April 26, 2013

Converting image files to text in Dynamics NAV


The article given below will help us to retrieve the "Text characters from an image" using Microsoft Dynamics NAV.
We can convert any format of an image(.bmp, .png, .tif, .jpeg)

Here I have taken the scenario of converting an "Error Message" image into a text file. Likewise, any image that has characters can be converted into a text file 

Consider, the below image:
         I captured the error message as screenshot and stored in local machine. For example : I saved in 'D:\Sample Error.tif' (Note: We need to use this file path and name when coding)
 

 

The text characters that are seen in the image can be stored in a document/text file.

Is this possible with NAV?
Yes it is possible with NAV and it can be developed by referencing the automation Server called Microsoft Office Document Imaging (MODI) which works by implementing OCR.

Microsoft Office Document Imaging can be readily available with Microsoft Office Tools package. If not contact your system administrator to include the MODI in office Tools.
How to read the characters?
To read the characters we are now going to develop a new codeunit.

Declare the following variables:
Doc – Automation-Microsoft Office Document Imaging 12.0 TypeLibrary.Document

image – Automation – Microsoft Office Document Imaging 12.0 Type Library.Image

filename – File

I – Integer

n – Integer

outstre – Outstream

 Type the following code:

IF ISCLEAR(doc) THEn

   CREATE(doc);

     //specify the file path and filename with extension as text/document file
    // This enables you to save your image file as text file

    filename.CREATE(<FILE PATH>);  // filepath\filename.txt
    filename.CREATEOUTSTREAM(outstre);  

    //Specify the file path, file name and extension of your image file.

    doc.Create(<File path of the image>);         //D:\Sample Error.tif
 
    doc.OCR(9,TRUE,TRUE);
                        // Where ‘9’ indicates the Language “English”, Boolean variables are optional

    i:=doc.Images.Count;

   FOR n:=0 TO i-1 DO //Loop to convert multiple images stored within one image
        BEGIN
         image:=doc.Images.Item(n);

          outstre.WRITETEXT(image.Layout.Text);

        END;

Now  Save and Run the codeunit.

Want to Check the Output?
 
 
In the above Output Everything looks fine, with some exceptions:
                 There are some Special/Junk character values inserted in between the text (*I,FJ). This is because OCR cannot recognize icon values. So in place of icons, the special characters inserted automatically.

 
Note: The special Characters like Single Quote(‘’), Double Quotes (“”) cannot be recognized by OCR.

The concept written above works not only with NAV but also with any VB.Net application with some modification


No comments:

Post a Comment