Important alert: (current site time 7/16/2013 7:32:40 AM EDT)
 

article

Extract Text From a Specific Part of the Image Using Aspose.OCR

Email
Submitted on: 11/25/2011 5:23:32 PM
By: aspose_seo 
Level: Intermediate
User Rating: Unrated
Compatibility: C#, VB.NET
Views: 4560
 
     This technical tip shows how to Extract Text from Specific Part of the Image. Aspose.OCR for .NET provides OcrEngine class to extract text from a specific part of the image document.

 
 
Terms of Agreement:   
By using this article, you agree to the following terms...   
  1. You may use this article in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this article (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this article from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the article or article's description.
				

What’s new in this release?

Aspose.OCR for .NET provides OcrEngine class to extract text from a specific part of the image document. The OcrEngine class requires following three items for character recognition:

  1. SourceImage
  2. Language
  3. Resourcefile

Steps to Extract Text from a Specific Recognition Block

Below are the steps to perform OCR on image using OcrEngine class of Aspose.OCR for .NET component.

  1. Createan instance of OcrEngine and initialize using default constructor.
  2. Setthe image file using OcrEngine.Image property on which OCR is to beperformed.
  3. Addlanguage(s) using OcrEngine.Languages.AddLanguage() method.
  4. Setstart point, width and height of the recognition block usingRecognitionBlock.FromRectangle method.
  5. Setthe resource file using OcrEngine.Resource property.
  6. CallOcrEngine.Process() method to perform OCR on the whole image.
  7. IfOcrEngine.Process() returns true, then get the recognized text withIRecognitionBlock.Text property.

Sample Code to do OCR on a Specific Block of Image

[C#]

const string resourceFileName = @"2011.07.02 v1.0 Aspose.OCR.Resources.zip";

try
{
    //Create OcrEngine instance and assign
    //image, language and image configuration
    OcrEngine ocrEngine = new OcrEngine();
    ocrEngine.Image = ImageStream.FromFile("Sample.bmp");

    ocrEngine.Languages.AddLanguage(Language.Load("english"));
    ocrEngine.Config.NeedRotationCorrection = false;
    ocrEngine.Config.UseDefaultDictionaries = true;

    //Select the block to recognize text
    int startX = 0, startY = 0, width = 120, height = 100;
    IRecognitionBlock rectangleBlock = Aspose.OCR.RecognitionBlock.FromRectangle(startX, startY, width, height);
    ocrEngine.AddRecognitionBlock(rectangleBlock);

    //Set resource file name and extract OCR text
    using (ocrEngine.Resource = new FileStream(resourceFileName, FileMode.Open))
    {
        try
        {
            if (ocrEngine.Process())
            {
                Console.WriteLine(rectangleBlock.Text.ToString());
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine("Exception: " + ex.Message);
        }
    }
    ocrEngine = null;
}
catch (Exception ex)
{
    Console.WriteLine("Exception: " + ex.Message);
}

[VB.NET]

Const resourceFileName As String = "2011.07.02 v1.0 Aspose.OCR.Resources.zip"

Try
              'Create OcrEngine instance and assign
              'image, language and image configuration
              Dim ocrEngine As OcrEngine = New OcrEngine()
              ocrEngine.Image = ImageStream.FromFile("Sample.bmp")

              ocrEngine.Languages.AddLanguage(Language.Load("english"))
              ocrEngine.Config.NeedRotationCorrection = False
              ocrEngine.Config.UseDefaultDictionaries = True

              'Select the block to recognize text
              Dim startX As Integer = 0, startY As Integer = 0, width As Integer = 120, height As Integer = 100
              Dim rectangleBlock As IRecognitionBlock = Aspose.OCR.RecognitionBlock.FromRectangle(startX, startY, width, height)
              ocrEngine.AddRecognitionBlock(rectangleBlock)

              'Set resource file name and extract OCR text
              ocrEngine.Resource = New FileStream(resourceFileName, FileMode.Open)
              Using ocrEngine.Resource
                            Try
                                          If ocrEngine.Process() Then
                                                        Console.WriteLine(rectangleBlock.Text.ToString())
                                          End If
                            Catch ex As Exception
                                          Console.WriteLine("Exception: " & ex.Message)
                            End Try
              End Using
              ocrEngine = Nothing
Catch ex As Exception
              Console.WriteLine("Exception: " & ex.Message)
End Try

More about Aspose.OCR for .NET

Contact Information

Suite 119, 272 Victoria Avenue
Chatswood, NSW, 2067
Australia
Aspose - Your File Format Experts
sales@aspose.com

Phone: 888.277.6734
Fax: 866.810.9465


Other 16 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Intermediate category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)
 

To post feedback, first please login.