VintaSoft Imaging .NET SDK 14.0: Documentation for .NET developer
Vintasoft.Imaging.Ocr.Tesseract Namespace / TesseractOcr Class / Recognize Methods / Recognize() Method
Syntax Exceptions Example Requirements SeeAlso
In This Topic
    Recognize() Method (TesseractOcr)
    In This Topic
    Recognizes text from image.
    Syntax
    'Declaration
    
    Public Overloads Overrides Function Recognize() As Vintasoft.Imaging.Ocr.Results.OcrPage
    
    
    public override Vintasoft.Imaging.Ocr.Results.OcrPage Recognize()
    
    
    public: Vintasoft.Imaging.Ocr.Results.OcrPage* Recognize(); override
    
    
    public:
    Vintasoft.Imaging.Ocr.Results.OcrPage^ Recognize(); override

    Return Value

    Result of recognition as OcrPage.
    Exceptions
    ExceptionDescription
    Thrown if this object is disposed.
    Thrown if Tesseract OCR engine does not have image for processing.
    Example

    This C#/VB.NET code shows how to recognize text from image using Tesseract OCR engine.

    
    ''' <summary>
    ''' Recognizes text in image using Tesseract OCR engine.
    ''' </summary>
    ''' <param name="filename">The name of file, which stores images with text.</param>
    ''' <param name="ocrLanguage">The language of recognizing text.</param>
    Public Shared Sub RecognizeTextInImageUsingTesseractOcrEngine(filename As String, ocrLanguage As Vintasoft.Imaging.Ocr.OcrLanguage)
        ' create an image collection
        Using images As New Vintasoft.Imaging.ImageCollection()
            ' add images from file to the image collection
            images.Add(filename)
    
            System.Console.WriteLine("Create Tesseract OCR engine...")
            ' create the Tesseract OCR engine
            Using tesseractOcr As New Vintasoft.Imaging.Ocr.Tesseract.TesseractOcr()
                System.Console.WriteLine("Initialize OCR engine...")
                ' init the Tesseract OCR engine
                tesseractOcr.Init(New Vintasoft.Imaging.Ocr.OcrEngineSettings(ocrLanguage))
    
                ' for each image in image collection
                For Each image As Vintasoft.Imaging.VintasoftImage In images
                    System.Console.WriteLine("Recognize the image...")
    
                    ' recognize text in image
                    Dim ocrResult As Vintasoft.Imaging.Ocr.Results.OcrPage = tesseractOcr.Recognize(image)
    
                    ' output the recognized text
    
                    System.Console.WriteLine("Page Text:")
                    System.Console.WriteLine(ocrResult.GetText())
                    System.Console.WriteLine()
                Next
    
                ' shutdown the Tesseract OCR engine
                tesseractOcr.Shutdown()
            End Using
    
            ' free images
            images.ClearAndDisposeItems()
        End Using
    End Sub
    
    
    
    /// <summary>
    /// Recognizes text in image using Tesseract OCR engine.
    /// </summary>
    /// <param name="filename">The name of file, which stores images with text.</param>
    /// <param name="ocrLanguage">The language of recognizing text.</param>
    public static void RecognizeTextInImageUsingTesseractOcrEngine(
        string filename,
        Vintasoft.Imaging.Ocr.OcrLanguage ocrLanguage)
    {
        // create an image collection
        using (Vintasoft.Imaging.ImageCollection images = 
            new Vintasoft.Imaging.ImageCollection())
        {
            // add images from file to the image collection
            images.Add(filename);
    
            System.Console.WriteLine("Create Tesseract OCR engine...");
            // create the Tesseract OCR engine
            using (Vintasoft.Imaging.Ocr.Tesseract.TesseractOcr tesseractOcr = 
                new Vintasoft.Imaging.Ocr.Tesseract.TesseractOcr())
            {
                System.Console.WriteLine("Initialize OCR engine...");
                // init the Tesseract OCR engine
                tesseractOcr.Init(new Vintasoft.Imaging.Ocr.OcrEngineSettings(ocrLanguage));
    
                // for each image in image collection
                foreach (Vintasoft.Imaging.VintasoftImage image in images)
                {
                    System.Console.WriteLine("Recognize the image...");
                    
                    // recognize text in image
                    Vintasoft.Imaging.Ocr.Results.OcrPage ocrResult = tesseractOcr.Recognize(image);
    
                    // output the recognized text
    
                    System.Console.WriteLine("Page Text:");
                    System.Console.WriteLine(ocrResult.GetText());
                    System.Console.WriteLine();
                }
    
                // shutdown the Tesseract OCR engine
                tesseractOcr.Shutdown();
            }
    
            // free images
            images.ClearAndDisposeItems();
        }
    }
    
    

    Requirements

    Target Platforms: .NET9; .NET 8; .NET 7; .NET 6; .NET Framework 4.8, 4.7, 4.6, 4.5, 4.0, 3.5

    See Also