TesseractDotnet Example
2011-07-16 15:45:20 · Author: Wudi · Tagged with: C# & .NET
Download source (TesseractDotnetExample.zip)

The use of TesseractDotnet is quite simple, except for the lack of references (the Tesseract itself does as well). For example, to OCR an image which is treated as a single line text, the code below works well:
But there is one point requiring particular attention. The first parameter of method TesseractProcessor.Init(), dataPath, must be ended with slash "/" or backslash "\", or the initialization will be failed. When it failed, the Init() method returns false. But if you did not deal with the returned value, then when the program runs to the line calling Apply(), an exception will be throwed:

The use of TesseractDotnet is quite simple, except for the lack of references (the Tesseract itself does as well). For example, to OCR an image which is treated as a single line text, the code below works well:
- using System;
- using System.Drawing;
- using tesseract;
- // ...
- TesseractProcessor processor = new TesseractProcessor();
- bool succeed = processor.Init(@"..\tessdata\", "eng", 3); // TesseractEngineMode: DEFAULT
- if (!succeed)
- {
- // Deal with error
- Application.Exit();
- }
- processor.SetVariable("tessedit_pageseg_mode", "3"); // TesseractPageSegMode: PSM_SINGLE_LINE
- Image image = Image.FromFile("...");
- processor.Clear();
- processor.ClearAdaptiveClassifier();
- string result = processor.Apply(image);
- // ...
But there is one point requiring particular attention. The first parameter of method TesseractProcessor.Init(), dataPath, must be ended with slash "/" or backslash "\", or the initialization will be failed. When it failed, the Init() method returns false. But if you did not deal with the returned value, then when the program runs to the line calling Apply(), an exception will be throwed:
AccessViolationException:
Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
Current language: English · also available in: Chinese (Simplified)