Question

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;
Was it helpful?

Solution

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);
}

OTHER TIPS

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.

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

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")); 

} 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top