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:
- SourceImage
- Language
- 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.
- Createan instance of OcrEngine and initialize using default constructor.
- Setthe image file using OcrEngine.Image property on which OCR is to beperformed.
- Addlanguage(s) using OcrEngine.Languages.AddLanguage() method.
- Setstart point, width and height of the recognition block usingRecognitionBlock.FromRectangle method.
- Setthe resource file using OcrEngine.Resource property.
- CallOcrEngine.Process() method to perform OCR on the whole image.
- IfOcrEngine.Process() returns true, then get the recognized text withIRecognitionBlock.Text property.
Sample Code to do OCR on a Specific Block of Image
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);
}
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