Products | Support | Email a link to this topic. | Send comments on this topic. | Back to Introduction | Help Version 19.0.6.21
|
using
or Imports
section if there are any:
[C#]
using Leadtools;
using Leadtools.Codecs;
using Leadtools.Drawing;
using Leadtools.Controls;
using Leadtools.Forms;
using Leadtools.Forms.Ocr;
using Leadtools.Forms.DocumentWriters;
[Visual Basic]
Imports Leadtools
Imports Leadtools.Codecs
Imports Leadtools.Drawing
Imports Leadtools.Controls
Imports Leadtools.Forms
Imports Leadtools.Forms.Ocr
Imports Leadtools.Forms.DocumentWriters
[C#]
// Image viewer instance
private ImageViewer _imageViewer;
// The OCR engine instance
private IOcrEngine _ocrEngine;
// The OCR page
private IOcrPage _ocrPage;
// Recognition results (characters with bounds information)
private IOcrZoneCharacters _zoneCharacters;
[Visual Basic]
' Image viewer instance
Private _imageViewer As ImageViewer
' The OCR engine instance
Private _ocrEngine As IOcrEngine
' The OCR page
Private _ocrPage As IOcrPage
' Recognition results (characters with bounds information)
Private _zoneCharacters As IOcrZoneCharacters
OnLoad
and add the following code:
[C#]
protected override void OnLoad(EventArgs e)
{
// Add the image viewer to the form
_imageViewer = new ImageViewer();
_imageViewer.Dock = DockStyle.Fill;
Controls.Add(_imageViewer);
_imageViewer.BringToFront();
// Show images using their true size
_imageViewer.UseDpi = true;
// Add ability to pan/zoom using the left mouse button
ImageViewerPanZoomInteractiveMode panZoomMode = new ImageViewerPanZoomInteractiveMode();
panZoomMode.MouseButtons = MouseButtons.Left;
_imageViewer.InteractiveModes.Add(panZoomMode);
// Add a rubber band mode to the right mouse button
ImageViewerRubberBandInteractiveMode rubbBandMode = new ImageViewerRubberBandInteractiveMode();
rubbBandMode.MouseButtons = MouseButtons.Right;
// Hook to the rubber-band completed event so we can recognize that area
rubbBandMode.RubberBandCompleted += rubbBandMode_RubberBandCompleted;
_imageViewer.InteractiveModes.Add(rubbBandMode);
// Initialize the OCR engine
_ocrEngine = OcrEngineManager.CreateEngine(OcrEngineType.Advantage, false);
// Startup the engine
_ocrEngine.Startup(null, null, null, @"C:\LEADTOOLS 19\Bin\Common\OcrAdvantageRuntime");
// Create the OCR page from an image
string fileName = @"C:\Users\Public\Documents\LEADTOOLS Images\Ocr1.tif";
RasterImage rasterImage = _ocrEngine.RasterCodecsInstance.Load(fileName, 1);
_ocrPage = _ocrEngine.CreatePage(rasterImage, OcrImageSharingMode.AutoDispose);
// Show this image in the viewer
_imageViewer.Image = _ocrPage.GetRasterImage();
Text = "Left click and drag to pan, CTRL-Left and drag click to zoom, right click and draw to draw the selection rectangle";
// Hook to the post render event of the viewer so we can render the recognition result on the page
_imageViewer.PostRender += _imageViewer_PostRender;
base.OnLoad(e);
}
[Visual Basic]
Protected Overrides Sub OnLoad(e As EventArgs)
' Add the image viewer to the form
_imageViewer = New ImageViewer()
_imageViewer.Dock = DockStyle.Fill
Controls.Add(_imageViewer)
_imageViewer.BringToFront()
' Show images using their true size
_imageViewer.UseDpi = True
' Add ability to pan/zoom using the left mouse button
Dim panZoomMode As New ImageViewerPanZoomInteractiveMode()
panZoomMode.MouseButtons = MouseButtons.Left
_imageViewer.InteractiveModes.Add(panZoomMode)
' Add a rubber band mode to the right mouse button
Dim rubbBandMode As New ImageViewerRubberBandInteractiveMode()
rubbBandMode.MouseButtons = MouseButtons.Right
' Hook to the rubber-band completed event so we can recognize that area
AddHandler rubbBandMode.RubberBandCompleted, AddressOf rubbBandMode_RubberBandCompleted
_imageViewer.InteractiveModes.Add(rubbBandMode)
' Initialize the OCR engine
_ocrEngine = OcrEngineManager.CreateEngine(OcrEngineType.Advantage, False)
' Startup the engine
_ocrEngine.Startup(Nothing, Nothing, Nothing, "C:\LEADTOOLS 19\Bin\Common\OcrAdvantageRuntime")
' Create the OCR page from an image
Dim fileName As String = "C:\Users\Public\Documents\LEADTOOLS Images\Ocr1.tif"
Dim rasterImage As RasterImage = _ocrEngine.RasterCodecsInstance.Load(fileName, 1)
_ocrPage = _ocrEngine.CreatePage(rasterImage, OcrImageSharingMode.AutoDispose)
' Show this image in the viewer
_imageViewer.Image = _ocrPage.GetRasterImage()
Text = "Left click and drag to pan, CTRL-Left and drag click to zoom, right click and draw to draw the selection rectangle"
' Hook to the post render event of the viewer so we can render the recognition result on the page
AddHandler _imageViewer.PostRender, AddressOf _imageViewer_PostRender
MyBase.OnLoad(e)
End Sub
OnFormClosed
and add the following code:
[C#]
protected override void OnFormClosed(FormClosedEventArgs e)
{
// Destroy the page
_ocrPage.Dispose();
// And the engine
_ocrEngine.Dispose();
base.OnFormClosed(e);
}
[Visual Basic]
Protected Overrides Sub OnFormClosed(e As FormClosedEventArgs)
' Destroy the page
_ocrPage.Dispose()
' And the engine
_ocrEngine.Dispose()
MyBase.OnFormClosed(e)
End Sub
[C#]
private void rubbBandMode_RubberBandCompleted(object sender, ImageViewerRubberBandEventArgs e)
{
// Use has drawn a selection rectangle
// Remove current zone from the page
_ocrPage.Zones.Clear();
// Add the new zone. The points from rubber band is in control coordinates, need to convert them to image
LeadRect bounds = LeadRect.FromLTRB(e.Points[0].X, e.Points[0].Y, e.Points[1].X, e.Points[1].Y);
bounds = _imageViewer.ConvertRect(null, ImageViewerCoordinateType.Control, ImageViewerCoordinateType.Image, bounds);
// Add a new text zone from these coordinates
OcrZone ocrZone = new OcrZone();
ocrZone.ZoneType = OcrZoneType.Text;
ocrZone.Bounds = new LogicalRectangle(bounds);
_ocrPage.Zones.Add(ocrZone);
// Recognize the page to get the text of the zone
_ocrPage.Recognize(null);
// Get the recognized characters
IOcrPageCharacters pageCharacters = _ocrPage.GetRecognizedCharacters();
_zoneCharacters = pageCharacters[0];
// Refresh the viewer so we render the character bounds and positions
_imageViewer.Refresh();
// Show the result in a message box
string text = _ocrPage.GetText(0);
if (string.IsNullOrEmpty(text))
text = "[Nothing found]";
MessageBox.Show(this, text);
}
[Visual Basic]
Private Sub rubbBandMode_RubberBandCompleted(sender As Object, e As ImageViewerRubberBandEventArgs)
' Use has drawn a selection rectangle
' Remove the current zone from the page
_ocrPage.Zones.Clear()
' Add the new zone. The points from rubber band is in control coordinates, need to convert them to image
Dim bounds As LeadRect = LeadRect.FromLTRB(e.Points(0).X, e.Points(0).Y, e.Points(1).X, e.Points(1).Y)
bounds = _imageViewer.ConvertRect(Nothing, ImageViewerCoordinateType.Control, ImageViewerCoordinateType.Image, bounds)
' Add a new text zone from these coordinates
Dim ocrZone As New OcrZone()
ocrZone.ZoneType = OcrZoneType.Text
ocrZone.Bounds = New LogicalRectangle(bounds)
_ocrPage.Zones.Add(ocrZone)
' Recognize the page to get the text of the zone
_ocrPage.Recognize(Nothing)
' Get the recognized characters
Dim pageCharacters As IOcrPageCharacters = _ocrPage.GetRecognizedCharacters()
_zoneCharacters = pageCharacters(0)
' Refresh the viewer so we render the character bounds and positions
_imageViewer.Refresh()
' Show the result in a message box
Dim text As String = _ocrPage.GetText(0)
If String.IsNullOrEmpty(text) Then
text = "[Nothing found]"
End If
MessageBox.Show(Me, text)
End Sub
[C#]
private void _imageViewer_PostRender(object sender, ImageViewerRenderEventArgs e)
{
// Draw the recognition results (if we have any)
if (_zoneCharacters == null)
return;
Graphics graphics = e.PaintEventArgs.Graphics;
using (Brush characterBrush = new SolidBrush(Color.FromArgb(128, Color.Black)))
{
foreach (OcrCharacter character in _zoneCharacters)
{
// Get the character boundary
LogicalRectangle characterBounds = character.Bounds;
// The character bounds is a logical rectangle, it may be in units other than pixels. Convert to pixels
LeadRect bounds = characterBounds.ToRectangle(_ocrPage.DpiX, _ocrPage.DpiY);
// Convert the bound to what we see in the viewer
// Note that this demo does not have rotation; otherwise, you need to use the four corner points
bounds = _imageViewer.ConvertRect(null, ImageViewerCoordinateType.Image, ImageViewerCoordinateType.Control, bounds);
// Highlight the character area on the page
graphics.FillRectangle(characterBrush, bounds.X, bounds.Y, bounds.Width, bounds.Height);
graphics.DrawRectangle(Pens.Black, bounds.X, bounds.Y, bounds.Width - 1, bounds.Height - 1);
// Finally draw the character code it self
graphics.DrawString(new string(new char[] { character.Code }), this.Font, Brushes.White, bounds.X, bounds.Y);
}
}
}
[Visual Basic]
Private Sub _imageViewer_PostRender(sender As Object, e As ImageViewerRenderEventArgs)
' Draw the recognition results (if we have any)
If IsNothing(_zoneCharacters) Then Return
Dim graphics As Graphics = e.PaintEventArgs.Graphics
Using characterBrush As New SolidBrush(Color.FromArgb(128, Color.Black))
For Each character As OcrCharacter In _zoneCharacters
' Get the character boundary
Dim characterBounds As LogicalRectangle = character.Bounds
' The character bounds is a logical rectangle, it may be in units other than pixels. Convert to pixels
Dim bounds As LeadRect = characterBounds.ToRectangle(_ocrPage.DpiX, _ocrPage.DpiY)
' Convert the bound to what we see in the viewer
' Note that this demo does not have rotation; otherwise, you need to use the four corner points
bounds = _imageViewer.ConvertRect(Nothing, ImageViewerCoordinateType.Image, ImageViewerCoordinateType.Control, bounds)
' Highlight the character area on the page
graphics.FillRectangle(characterBrush, bounds.X, bounds.Y, bounds.Width, bounds.Height)
graphics.DrawRectangle(Pens.Black, bounds.X, bounds.Y, bounds.Width - 1, bounds.Height - 1)
' Finally draw the character code it self
graphics.DrawString(New String(New Char() {character.Code}), Me.Font, Brushes.White, bounds.X, bounds.Y)
Next
End Using
End Sub
OCR Tutorial - Working with Pages
OCR Tutorial - Recognizing Pages
OCR Tutorial - Adding and Painting Zones
OCR Tutorial - Scanning to Searchable PDF
Introduction
Getting Started (Guide to Example Programs)
LEADTOOLS OCR .NET Assemblies
Programming with LEADTOOLS .NET OCR
An Overview of OCR Recognition Modules
Creating an OCR Engine Instance
Starting and Shutting Down the OCR Engine
Multi-Threading with LEADTOOLS OCR
OCR Spell Language Dictionaries
Working with OCR Languages
Working with OCR Pages
Working with OCR Zones
Recognizing OCR Pages
OCR Confidence Reporting
Using OMR in LEADTOOLS .NET OCR
OCR Languages and Spell Checking
OCR Engine-Specific Settings