我正在尝试做一些简单的OCR-Tasks,我仍然在寻找一个免费的库。由于每个人似乎都使用tesseract,有人可以给我一个简单但有效的例子,使用tesseractengine3.dll与C#或VB.NET,好吗?搜索几个小时后,我无法找到任何文档或在VS2010和.Net 4下编译的示例。
答案 0 :(得分:1)
试试这个
Ocr ocr = new Ocr();
private void button1_Click(object sender, EventArgs e)
{
using (Bitmap bmp = new Bitmap(@"C:\OCR\ocr-test.jpg"))
{
tessnet2.Tesseract tessocr = new tessnet2.Tesseract();
tessocr.Init(null, "eng", false);
tessocr.GetThresholdedImage(bmp, Rectangle.Empty).Save("c:\\temp\\" + Guid.NewGuid().ToString() + ".bmp");
// Tessdata directory must be in the directory than this exe
Console.WriteLine("Multithread version");
ocr.DoOCRMultiThred(bmp, "eng");
Console.WriteLine("Normal version");
ocr.DoOCRNormal(bmp, "eng");
}
}
public class Ocr
{
public void DumpResult(List<tessnet2.Word> result)
{
foreach (tessnet2.Word word in result)
Console.WriteLine("{0} : {1}", word.Confidence, word.Text);
}
public List<tessnet2.Word> DoOCRNormal(Bitmap image, string lang)
{
tessnet2.Tesseract ocr = new tessnet2.Tesseract();
ocr.Init(null, lang, false);
List<tessnet2.Word> result = ocr.DoOCR(image, Rectangle.Empty);
DumpResult(result);
return result;
}
ManualResetEvent m_event;
public void DoOCRMultiThred(Bitmap image, string lang)
{
tessnet2.Tesseract ocr = new tessnet2.Tesseract();
ocr.Init(null, lang, false);
// If the OcrDone delegate is not null then this'll be the multithreaded version
ocr.OcrDone = new tessnet2.Tesseract.OcrDoneHandler(Finished);
// For event to work, must use the multithreaded version
ocr.ProgressEvent += new tessnet2.Tesseract.ProgressHandler(ocr_ProgressEvent);
m_event = new ManualResetEvent(false);
ocr.DoOCR(image, Rectangle.Empty);
// Wait here it's finished
m_event.WaitOne();
}
public void Finished(List<tessnet2.Word> result)
{
DumpResult(result);
m_event.Set();
}
void ocr_ProgressEvent(int percent)
{
Console.WriteLine("{0}% progression", percent);
}
}
答案 1 :(得分:1)
尝试使用https://github.com/charlesw/tesseract令人敬畏的开源OCR中使用的http://vietocr.sourceforge.net/库,并在简单的示例中查看库源代码中的BaseApiTester项目。
答案 2 :(得分:0)
Tesseract 3.01有一个.NET包装器:tesseract-ocr-dotnet