How to Extract Text From Part of an Image inside .NET Applications
Join the DZone community and get the full member experience.
Join For FreeTo run OCR on an image using the OcrEngine class:
- Create an instance of OcrEngine and initialize it using the default constructor.
- Set the image file using the OcrEngine.Image property.
- Add language(s) using the OcrEngine.Languages.AddLanguage method.
- Set the start point, width and height of the recognition block using the OcrConfig.AddRecognitionBlock method.
- Set the resource file using the OcrEngine.Resource property.
- Call the OcrEngine.Process method to perform OCR on the whole image.
- If OcrEngine.Process returns true, then get the recognized text with the IRecognitionBlock.Text property.
//The sample code below shows how to use the steps above to run OCR on part of an image.
//[C#]
const string resourceFileName = @"Aspose.OCR.Resources.zip";
try
{
//Create an instance of OcrEngine and assign
//image, language and image settings
OcrEngine ocrEngine = new OcrEngine();
ocrEngine.Image = ImageStream.FromFile("Sample.bmp");
ocrEngine.Languages.AddLanguage(Language.Load("english"));
ocrEngine.Config.UseDefaultDictionaries = true;
//Define the block in which to recognize text
int startX = 0, startY = 0, width = 120, height = 100;
//Clear recognition blocks
ocrEngine.Config.ClearRecognitionBlocks();
//Add 3 rectangle blocks to user defined recognition blocks
ocrEngine.Config.AddRecognitionBlock(RecognitionBlock.CreateTextBlock(startX, startY, width, height));
//Set the resource file name and extract OCR text
using (ocrEngine.Resource = new FileStream(resourceFileName, FileMode.Open))
{
try
{
if (ocrEngine.Process())
{
//Retrieve user defined blocks that determines the paye layout
var blocks = ocrEngine.Config.RecognitionBlocks;
//Loop over the list of blocks
foreach (var block in blocks)
{
//Display if block is set to be recognized
Console.WriteLine(block.ToRecognize);
//Check if block has recognition data
if (block.RecognitionData == null)
{
Console.WriteLine("Null{0}", Environment.NewLine);
continue;
}
//Display dimension & size of rectangle that defines the recognition block
Console.WriteLine("Block: {0}", block.Rectangle);
//Display the recognition results
Console.WriteLine("Text: {0}{1}", block.RecognitionData.Text, Environment.NewLine);
}
}
}
catch (Exception ex)
{
Console.WriteLine("Exception: " + ex.Message);
}
}
ocrEngine = null;
}
catch (Exception ex)
{
Console.WriteLine("Exception: " + ex.Message);
}
//[VB.NET]
Const resourceFileName As String = "Aspose.OCR.Resources.zip"
Try
'Create an instance of OcrEngine and assign
'image, language and image settings
Dim ocrEngine As New OcrEngine()
ocrEngine.Image = ImageStream.FromFile("Sample.bmp")
ocrEngine.Languages.AddLanguage(Language.Load("english"))
ocrEngine.Config.UseDefaultDictionaries = True
'Define the block in which to recognize text
Dim startX As Integer = 0, startY As Integer = 0, width As Integer = 120, height As Integer = 100
'Clear recognition blocks
ocrEngine.Config.ClearRecognitionBlocks()
'Add 3 rectangle blocks to user defined recognition blocks
ocrEngine.Config.AddRecognitionBlock(RecognitionBlock.CreateTextBlock(startX, startY, width, height))
'Set the resource file name and extract OCR text
ocrEngine.Resource = New FileStream(resourceFileName, FileMode.Open)
Using ocrEngine.Resource
Try
If ocrEngine.Process() Then
'Retrieve user defined blocks that determines the paye layout
Dim blocks = ocrEngine.Config.RecognitionBlocks
'Loop over the list of blocks
For Each block In blocks
'Display if block is set to be recognized
Console.WriteLine(block.ToRecognize)
'Check if block has recognition data
If block.RecognitionData Is Nothing Then
Console.WriteLine("Null{0}", Environment.NewLine)
Continue For
End If
'Display dimension & size of rectangle that defines the recognition block
Console.WriteLine("Block: {0}", block.Rectangle)
'Display the recognition results
Console.WriteLine("Text: {0}{1}", block.RecognitionData.Text, Environment.NewLine)
Next block
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
Opinions expressed by DZone contributors are their own.
Comments