.NET OCRing an Image

后端 未结 7 770
挽巷
挽巷 2021-02-06 05:56

I\'m trying to use MODI to OCR a window\'s program. It works fine for screenshots I grab programmatically using win32 interop like this:

public string SaveScreen         


        
7条回答
  •  忘掉有多难
    2021-02-06 06:14

    Looks as though the answer is in giving MODI a bigger canvas. I was also trying to take a screenshot of a control and OCR it and ran into the same problem. In the end I took the image of the control, copied the image into a larger bitmap and OCRed the larger bitmap.

    Another issue I found was that you must have a proper extension for your image file. In other words, .tmp doesn't cut it.

    I kept the work of creating a larger source inside my OCR method, which looks something like this (I deal directly with Image objects):

    public static string ExtractText(this Image image)
    {
        var tmpFile = Path.GetTempFileName();
        string text;
        try
        {
            var bmp = new Bitmap(Math.Max(image.Width, 1024), Math.Max(image.Height, 768));
            var gfxResize = Graphics.FromImage(bmp);
            gfxResize.DrawImage(image, new Rectangle(0, 0, image.Width, image.Height));
            bmp.Save(tmpFile + ".bmp", ImageFormat.Bmp);
            var doc = new MODI.Document();
            doc.Create(tmpFile + ".bmp");
            doc.OCR(MODI.MiLANGUAGES.miLANG_ENGLISH, true, true);
            var img = (MODI.Image)doc.Images[0];
            var layout = img.Layout;
            text = layout.Text;
        }
        finally
        {
            File.Delete(tmpFile);
            File.Delete(tmpFile + ".bmp");
        }
    
        return text;
    }
    

    I'm not sure exactly what the minimum size is, but it appears as though 1024 x 768 does the trick.

提交回复
热议问题