私のプロジェクトでは、ファイル 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 で、ペイントで編集して 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);