Products | Support | Email a link to this topic. | Send comments on this topic. | Back to Introduction | Help Version 19.0.6.21
|
Button | Text |
---|---|
button1 | Change output directory |
button2 | Select the Scanner |
button3 | Scan and OCR |
using
or Imports
section if there are any:
[C#]
using Leadtools;
using Leadtools.Twain;
using Leadtools.ImageProcessing;
using Leadtools.ImageProcessing.Core;
using Leadtools.Forms;
using Leadtools.Forms.DocumentWriters;
using Leadtools.Forms.Ocr;
using Leadtools.ImageProcessing;
[Visual Basic]
Imports Leadtools
Imports Leadtools.Twain
Imports Leadtools.ImageProcessing
Imports Leadtools.ImageProcessing.Core
Imports Leadtools.Forms
Imports Leadtools.Forms.DocumentWriters
Imports Leadtools.Forms.Ocr
Imports Leadtools.ImageProcessing
[C#]
// The OCR engine instance
private IOcrEngine _ocrEngine;
// OCR document instance
private IOcrDocument _ocrDocument;
// The Twain session
private TwainSession _twainSession;
// The output directory for saving PDF files
private string _outputDirectory = @"C:\MyImages";
// The image processing commands we are going to use to clean the scanned image
private List<RasterCommand> _imageProcessingCommands;
private int _scanCount;
[Visual Basic]
' The OCR engine instance
Private _ocrEngine As IOcrEngine
' OCR document instance
Private _ocrDocument As IOcrDocument
' The Twain session
Private _twainSession As TwainSession
' The output directory for saving PDF files
Private _outputDirectory As String = "C:\MyImages"
' The image processing commands we are going to use to clean the scanned image
Private _imageProcessingCommands As List(Of RasterCommand)
Private _scanCount As Integer
OnLoad
and add the following code:
[C#]
protected override void OnLoad(EventArgs e)
{
// Initialize the OCR engine
_ocrEngine = OcrEngineManager.CreateEngine(OcrEngineType.Advantage, false);
// Startup the engine
_ocrEngine.Startup(null, null, null, @"C:\LEADTOOLS 19\Bin\Common\OcrAdvantageRuntime");
// Initialize Twain scanning session
_twainSession = new TwainSession();
_twainSession.Startup(this.Handle, "My Company", "My Product", "My Version", "My Application", TwainStartupFlags.None);
// Subscribe to the TwainSession.Acquire event to get the image
_twainSession.AcquirePage += new EventHandler<TwainAcquirePageEventArgs>(_twainSession_AcquirePage);
// Initialize the image processing commands we are going to use
// Add as many as you like, here we will add Deskew and Despeckle
_imageProcessingCommands = new List<RasterCommand>();
_imageProcessingCommands.Add(new DeskewCommand());
_imageProcessingCommands.Add(new DespeckleCommand());
base.OnLoad(e);
}
[Visual Basic]
Protected Overrides Sub OnLoad(e As EventArgs)
' Initialize the OCR engine
_ocrEngine = OcrEngineManager.CreateEngine(OcrEngineType.Advantage, False)
' Startup the engine
_ocrEngine.Startup(Nothing, Nothing, Nothing, "C:\LEADTOOLS 19\Bin\Common\OcrAdvantageRuntime")
' Initialize Twain scanning session
_twainSession = New TwainSession()
_twainSession.Startup(Me.Handle, "My Company", "My Product", "My Version", "My Application", TwainStartupFlags.None)
' Subscribe to the TwainSession.Acquire event to get the image
AddHandler _twainSession.AcquirePage, AddressOf _twainSession_AcquirePage
' Initialize the image processing commands we are going to use
' Add as many as you like, here we will add Deskew and Despeckle
_imageProcessingCommands = New List(Of RasterCommand)()
_imageProcessingCommands.Add(New DeskewCommand())
_imageProcessingCommands.Add(New DespeckleCommand())
MyBase.OnLoad(e)
End Sub
OnFormClosed
and add the following code:
[C#]
protected override void OnFormClosed(FormClosedEventArgs e)
{
// Shutdown and dispose the OCR engine
_ocrEngine.Dispose();
// And the twain session
_twainSession.Shutdown();
base.OnFormClosed(e);
}
[Visual Basic]
Protected Overrides Sub OnFormClosed(e As FormClosedEventArgs)
' Shutdown and dispose the OCR engine
_ocrEngine.Dispose()
' And the twain session
_twainSession.Shutdown()
MyBase.OnFormClosed(e)
End Sub
Click
handler:
[C#]
private void button1_Click(object sender, EventArgs e)
{
// Change the output directory
using (FolderBrowserDialog dlg = new FolderBrowserDialog())
{
dlg.SelectedPath = _outputDirectory;
dlg.ShowNewFolderButton = true;
if (dlg.ShowDialog(this) == DialogResult.OK)
_outputDirectory = System.IO.Path.GetFullPath(dlg.SelectedPath);
}
}
[Visual Basic]
Private Sub button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button1.Click
' Change the output directory
Using dlg As New FolderBrowserDialog()
dlg.SelectedPath = _outputDirectory
dlg.ShowNewFolderButton = True
If dlg.ShowDialog(Me) = DialogResult.OK Then
_outputDirectory = System.IO.Path.GetFullPath(dlg.SelectedPath)
End If
End Using
End Sub
Click
handler:
[C#]
private void button2_Click(object sender, EventArgs e)
{
// Select the scanner to use
_twainSession.SelectSource(null);
}
[Visual Basic]
Private Sub button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button2.Click
' Select the scanner to use
_twainSession.SelectSource(Nothing)
End Sub
Click
handler:
[C#]
private void button3_Click(object sender, EventArgs e)
{
// Create the output directory if it does not exist
if(!System.IO.Directory.Exists(_outputDirectory))
System.IO.Directory.CreateDirectory(_outputDirectory);
// Build the output PDF file name
string name = "Scanned" + _scanCount;
_scanCount++;
string pdfFileName = System.IO.Path.Combine(_outputDirectory, name + ".pdf");
// Create a new file-based OCR document to add the scanned pages to
_ocrDocument = _ocrEngine.DocumentManager.CreateDocument(null, OcrCreateDocumentOptions.AutoDeleteFile);
// Scan the new page(s)
_twainSession.Acquire(TwainUserInterfaceFlags.Show);
// Save as PDF
_ocrDocument.Save(pdfFileName, DocumentFormat.Pdf, null);
// Delete the document
_ocrDocument.Dispose();
// Show the result PDF file
System.Diagnostics.Process.Start(pdfFileName);
}
[Visual Basic]
Private Sub button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button3.Click
' Create the output directory if it does not exist
If Not System.IO.Directory.Exists(_outputDirectory) Then
System.IO.Directory.CreateDirectory(_outputDirectory)
End If
' Build the output PDF file name
Dim name As String = "Scanned" + _scanCount
_scanCount = _scanCount + 1
Dim pdfFileName As String = System.IO.Path.Combine(_outputDirectory, name + ".pdf")
' Create a new file-based OCR document to add the scanned pages to
_ocrDocument = _ocrEngine.DocumentManager.CreateDocument(Nothing, OcrCreateDocumentOptions.AutoDeleteFile)
' Scan the new page(s)
_twainSession.Acquire(TwainUserInterfaceFlags.Show)
' Save as PDF
_ocrDocument.Save(pdfFileName, DocumentFormat.Pdf, Nothing)
' Delete the document
_ocrDocument.Dispose()
' Show the result PDF file
System.Diagnostics.Process.Start(pdfFileName)
End Sub
[C#]
private void _twainSession_AcquirePage(object sender, TwainAcquirePageEventArgs e)
{
// We have a page
RasterImage image = e.Image;
// First, run the image processing commands on it
foreach (RasterCommand command in _imageProcessingCommands)
{
command.Run(image);
}
// Create an OCR page for it
using (IOcrPage ocrPage = _ocrEngine.CreatePage(image, OcrImageSharingMode.AutoDispose))
{
// Recognize it and add it to the document
ocrPage.Recognize(null);
_ocrDocument.Pages.Add(ocrPage);
}
}
[Visual Basic]
Private Sub _twainSession_AcquirePage(sender As Object, e As TwainAcquirePageEventArgs)
' We have a page
Dim image As RasterImage = e.Image
' First, run the image processing commands on it
For Each command As RasterCommand In _imageProcessingCommands
command.Run(image)
Next
' Create an OCR page for it
Using ocrPage As IOcrPage = _ocrEngine.CreatePage(image, OcrImageSharingMode.AutoDispose)
' Recognize it and add it to the document
ocrPage.Recognize(Nothing)
_ocrDocument.Pages.Add(ocrPage)
End Using
End Sub
OCR Tutorial - Working with Pages
OCR Tutorial - Recognizing Pages
OCR Tutorial - Adding and Painting Zones
OCR Tutorial - Working with Recognition Results
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