Take the following steps to create a project that loads an image, recognizes
its text, and writes it to a textbox.
Start Visual Studio .NET. Create a new Visual C# or Visual Basic Windows Application. In the "Solution Explorer" window, right-click on the "References" folder, and select "Add Reference…" from the context menu. In the "Add Reference" dialog box, select the ".NET" tab and browse to Leadtools For .NET "\LEAD Technologies\LEADTOOLS 15\Bin\DotNet\Win32 " folder and select the following DLLs: Leadtools.dll Leadtools.Codecs.dll Leadtools.Document.dll
Switch to Form1 design view (right-click Form1 in the Solution Explorer then select View Designer). Add a new TextBox from the toolbox and name it txtResults. Switch to Form1 code view (right-click Form1 in the Solution Explorer then select View Code) and add the following lines at the beginning of the file (after the existing Imports / using statements): [Visual Basic]
[C#]Imports Leadtools Imports Leadtools.Codecs Imports Leadtools.Document
using Leadtools; using Leadtools.Codecs; using Leadtools.Document;
Add the following function, RecognizeSample, that loads an image, recognizes it, and returns the text as a String: [Visual Basic]
[C#]Private Function RecognizeExample() As String ' Load an image to OCR RasterCodecs.Startup() Dim codecs As RasterCodecs Dim image As RasterImage codecs = New RasterCodecs() image = codecs.Load("C:\Program Files\LEAD Technologies\LEADTOOLS 15\Images\OCR1.TIF") ' Unlock support for the OCR engine RasterSupport.Unlock(RasterSupportType.Ocr, "TestKey") ' Initialize the OCR engine Dim documentEngine As RasterDocumentEngine documentEngine = RasterDocumentEngine.Instance RasterDocumentEngine.EnginePath = "C:\Program Files\LEAD Technologies\LEADTOOLS 15\Bin\Common\OCR" documentEngine.Startup() ' Recognize the text on the image documentEngine.AddPage(image, 0) documentEngine.RecognitionDataFileName = "C:\test.rdf" documentEngine.Recognize(0, 1, Nothing) ' Write out the recognition results to the console. Dim strResults As String = documentEngine.SaveResultsToMemory() ' Shut down the engine documentEngine.Shutdown() Return strResults End Function
private String RecognizeExample() { // Load an image to OCR RasterCodecs.Startup(); RasterCodecs codecs; RasterImage image; codecs = new RasterCodecs(); image = codecs.Load(@"C:\Program Files\LEAD Technologies\LEADTOOLS 15\Images\OCR1.TIF"); // Unlock support for the OCR engine RasterSupport.Unlock(RasterSupportType.Ocr, "TestKey"); // Initialize the OCR engine RasterDocumentEngine documentEngine; documentEngine = RasterDocumentEngine.Instance; RasterDocumentEngine.EnginePath = @"C:\Program Files\LEAD Technologies\LEADTOOLS 15\Bin\Common\OCR"; documentEngine.Startup(); // Recognize the text on the image documentEngine.AddPage(image, 0); documentEngine.RecognitionDataFileName = @"C:\test.rdf"; documentEngine.Recognize(0, 1, null); // Write out the recognition results to the console. String strResults = documentEngine.SaveResultsToMemory(); // Shut down the engine documentEngine.Shutdown(); return strResults; }
In the code for the Load event for Form1, add the following lines to format the text box and call RecognizeSample(): [Visual Basic]
[C#]Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load txtResults.Dock = DockStyle.Fill txtResults.ScrollBars = ScrollBars.Both txtResults.Multiline = True txtResults.Text = RecognizeExample() End Sub
private void Form1_Load(object sender, EventArgs e) { txtResults.Dock = DockStyle.Fill; txtResults.ScrollBars = ScrollBars.Both; txtResults.Multiline = true; txtResults.Text = RecognizeExample(); }
Build, and Run the program to test it.