在C#中使用MODI进行OCR。 需要从内存中读取图像,而不是磁盘

我正在尝试使用MODI在内存中已有的位图上执行OCR。 我似乎无法找到解决方案,因为我找到的所有示例都使用create方法从磁盘中获取图像并为OCR做好准备。但是,我已经在内存上有图像并且写入和读取了我往返磁盘会消耗太多时间。

Bitmap bmp = ... //Instantiate the MODI.Document object MODI.Document md = new MODI.Document(); //The Create method grabs the picture from disk snd prepares for OCR. md.Create("C:\\bmp.gif"); //but I don't want to read from disk :( //Do the OCR. md.OCR(MODI.MiLANGUAGES.miLANG_ENGLISH, true, true); //Get the first (and only image) MODI.Image image = (MODI.Image)md.Images[0]; //Get the layout. MODI.Layout layout = image.Layout; 

你不能。 只有一个版本的Create,它需要一个文件。 制作临时文件。 将图像保存到其中。 删除临时文件。 使用Path.GetTempFileName()来做到这一点。

 string file = Path.GetTempFileName(); try { SaveImageToFile(image, file); // you decide how to best do this md.Create(file); // etc. } finally { File.Delete(file); } 

这个MODI.Document类可以从流中读取吗? 像

Image.FromStream(YourStream);

这样你就可以创建一个内存流并从中读取。

您可以在维基百科上查看MODI / OCR信息

en.wikipedia.org/wiki/Microsoft_Office_Document_Imaging

en.wikipedia.org/wiki/List_of_optical_character_recognition_software

使用Microsoft Office映像function对OCR映像进行OCR的最简单代码(需要MS-Office 2007或更高版本,必须安装映像组件,并且必须将MODI添加到引用中)。

 private string OCR ( string fileToOCR) { MODI.Document md = new MODI.Document(); md.Create(fileToOCR); md.OCR(MODI.MiLANGUAGES.miLANG_ENGLISH, true, true); MODI.Image img = (MODI.Image) md.Images[0]; MODI.Layout layout = img.Layout; layout = img.Layout; string result = layout.Text; md.Close (false); return result; } 

调用function可以是:

 private void button6_Click(object sender, EventArgs e) { MessageBox.Show ( OCR ("C:\\temp\\in.tif")); }