Send comments on this topic. | Back to Introduction - All Topics | Help Version 15.12.21
Simple Recognize Tutorial
Take the following steps to create a project that loads an image, recognizes its text, and writes it to a textbox.
  1. Start Visual Studio .NET.
  2. Create a new Visual C# or Visual Basic Windows Application.
  3. 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
    Click the Select button and then press the OK button to add the above DLLs to the application.
  4. 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.
  5. 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]

     
    Imports Leadtools
    Imports Leadtools.Codecs
    Imports Leadtools.Document
    
    [C#]
    
    using Leadtools;
    using Leadtools.Codecs;
    using Leadtools.Document;
    
  6. Add the following function, RecognizeSample, that loads an image, recognizes it, and returns the text as a String:

    [Visual Basic]

    
    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
    
    [C#]
    
    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;
    }
    
  7. In the code for the Load event for Form1, add the following lines to format the text box and call RecognizeSample():

    [Visual Basic]

     
    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
    
    [C#]
     
    private void Form1_Load(object sender, EventArgs e)
    {
        txtResults.Dock = DockStyle.Fill;
        txtResults.ScrollBars = ScrollBars.Both;
        txtResults.Multiline = true;
    
        txtResults.Text = RecognizeExample();
    }
    
  8. Build, and Run the program to test it.