Using MODI to OCR in C#. Need to read images from memory, not disk

只谈情不闲聊 提交于 2019-12-08 09:35:01

问题


I'm trying to use MODI to perfom OCR on bitmaps I already have in memory. I can't seem to find a solution to this as all the examples I find use the create method to grab the image from the disk and prepares it for the OCR., however, I already have the image on memory and writing and reading i to and from the disk consumes too much time.

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;

回答1:


You can't. There is only one version of Create and it takes a file. Make a temp file. Save the image into it. Delete the temp file. Use Path.GetTempFileName() to do that.

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



回答2:


Can this MODI.Document Class read from a stream? Like the

Image.FromStream(YourStream);

That way you can create a memory stream and read from it.




回答3:


You can check MODI / OCR information on wikipedia

en.wikipedia.org/wiki/Microsoft_Office_Document_Imaging

en.wikipedia.org/wiki/List_of_optical_character_recognition_software




回答4:


Simplest code to OCR an image using Microsoft Office's Imaging functionality (requires MS-Office 2007 or later, imaging components must be installed and MODI must be added to references).

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; 

}

Calling function can be:

private void button6_Click(object sender, EventArgs e)

{

MessageBox.Show ( OCR ("C:\\temp\\in.tif")); 

} 


来源:https://stackoverflow.com/questions/4081335/using-modi-to-ocr-in-c-need-to-read-images-from-memory-not-disk

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!