1

在我的项目中,我添加了引用的文件 MODI dll:Microsoft Office Document Imaging 11.0 Type Library

在一个新课程中,我做了:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Drawing;
using System.IO;

namespace SearchLiveCameras
{
    class Htpp
    {
        string extractedText = string.Empty;
        string getFileName;

        public Htpp(string F)
        {
            getFileName = @"d:\timessquare1.bmp";
            testing();
            F = extractedText;
        }

        private void testing()
        {

            MODI.Document doc = new MODI.Document();
            doc.Create(getFileName);
            doc.OCR(MODI.MiLANGUAGES.miLANG_ENGLISH, true, true);
            MODI.Image img = (MODI.Image)doc.Images[0];
            MODI.Layout layout = img.Layout;

            for (int i = 0; i < layout.Words.Count; i++)
            {
                MODI.Word word = (MODI.Word)layout.Words[i];

                if (extractedText.Length > 0)
                {
                    extractedText += " ";
                }

                extractedText += word.Text;
            }
        }

    }
}

在Form1中我做了:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SearchLiveCameras
{
    public partial class Form1 : Form
    {
        Htpp htp;
        string f;

        public Form1()
        {
            InitializeComponent();

            htp = new Htpp(f);
            richTextBox1.Text = f;
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}

然后在新类中使用断点。当它到达线路时:

doc.OCR(MODI.MiLANGUAGES.miLANG_ENGLISH, true, true);

我得到异常:

COM异常

OCR 运行错误

System.Runtime.InteropServices.COMException was unhandled
  HResult=-959967087
  Message=OCR running error
  Source=""
  ErrorCode=-959967087
  StackTrace:
       at MODI.IDocument.OCR(MiLANGUAGES LangId, Boolean OCROrientImage, Boolean OCRStraightenImage)
       at SearchLiveCameras.Htpp.testing() in d:\C-Sharp\SearchliveCameras\SearchLiveCameras\SearchLiveCameras\Htpp.cs:line 29
       at SearchLiveCameras.Htpp..ctor(String F) in d:\C-Sharp\SearchliveCameras\SearchLiveCameras\SearchLiveCameras\Htpp.cs:line 20
       at SearchLiveCameras.Form1..ctor() in d:\C-Sharp\SearchliveCameras\SearchLiveCameras\SearchLiveCameras\Form1.cs:line 22
       at SearchLiveCameras.Program.Main() in d:\C-Sharp\SearchliveCameras\SearchLiveCameras\SearchLiveCameras\Program.cs:line 19
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 

可能是什么问题呢 ?我该如何解决?

文件 timessquare1.bmp 是原始的 timessquare.jpg 我在paint中对其进行了编辑并将其保存为 timessquare1.bmp

编辑**

我现在在新类中尝试了这种方法:

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

但是在同一行上得到相同的异常:

doc.OCR(MODI.MiLANGUAGES.miLANG_ENGLISH, true, true);
4

0 回答 0