【发布时间】:2014-08-12 12:24:03
【问题描述】:
是否有 API 可以使用 Onenote OCR 功能自动识别图像中的文本?
【问题讨论】:
-
我特别指的是 OneNote 2013。
是否有 API 可以使用 Onenote OCR 功能自动识别图像中的文本?
【问题讨论】:
如果您在执行程序的同一台机器上安装了 OneNote 客户端,您可以在 OneNote 中创建一个页面并通过 COM API 插入图像。然后您可以阅读包含 OCR 文本的 XML 格式的页面。
你想用
Application.CreateNewPage创建页面Application.UpdatePageContent 插入图片Application.GetPageContent 读取页面内容并在 XML 中查找 OCRData 和 OCRText 元素。OneNote COM API 记录在这里:http://msdn.microsoft.com/en-us/library/office/jj680120(v=office.15).aspx
【讨论】:
当您通过 API 将图像放在 OneNote 中的页面上时,任何图像都会自动进行 OCR 处理。然后,用户将能够在 OneNote 中搜索图像中的任何文本。但是,此时您无法拉回图像并读取 OCR 文本。
如果您对此功能感兴趣,我邀请您访问我们的 UserVoice 网站并提交此想法:http://onenote.uservoice.com/forums/245490-onenote-developers
-- 詹姆斯
【讨论】:
这里有一个很好的示例来说明如何做到这一点: http://www.journeyofcode.com/free-easy-ocr-c-using-onenote/
代码的主要部分是:
private string RecognizeIntern(Image image)
{
this._page.Reload();
this._page.Clear();
this._page.AddImage(image);
this._page.Save();
int total = 0;
do
{
Thread.Sleep(PollInterval);
this._page.Reload();
string result = this._page.ReadOcrText();
if (result != null)
return result;
} while (total++ < PollAttempts);
return null;
}
【讨论】:
由于我将删除我的博客(在另一篇文章中提到),我想我应该在此处添加内容以供将来参考:
让我们先来看看如何使用该组件:OnenoteOcrEngine 类实现了核心功能,并实现了提供单一方法的接口IOcrEngine: p>
public interface IOcrEngine
{
string Recognize(Image image);
}
排除任何错误处理,使用方式类似如下:
using (var ocrEngine = new OnenoteOcrEngine())
using (var image = Image.FromFile(imagePath))
{
var text = ocrEngine.Recognize(image);
if (text == null)
Console.WriteLine("nothing recognized");
else
Console.WriteLine("Recognized: " + text);
}
实现远没有那么简单。在 Office 2010 之前,Microsoft Office Document Imaging (MODI) 可用于 OCR。不幸的是,情况不再如此。进一步的研究证实,OneNote 的 OCR 功能并未直接以 API 的形式公开,但建议手动解析 OneNote 文档中的文本(请参阅 Is it possible to do OCR on a Tiff image using the OneNote interop API? 或 need a document to extract text from image using onenote Interop?。这正是我所做的:
挑战包括解析 XML 代码,我决定使用 LINQ to XML。例如,插入图像是使用以下代码完成的:
private XElement CreateImageTag(Image image)
{
var img = new XElement(XName.Get("Image", OneNoteNamespace));
var data = new XElement(XName.Get("Data", OneNoteNamespace));
data.Value = this.ToBase64(image);
img.Add(data);
return img;
}
private string ToBase64(Image image)
{
using (var memoryStream = new MemoryStream())
{
image.Save(memoryStream, ImageFormat.Png);
var binary = memoryStream.ToArray();
return Convert.ToBase64String(binary);
}
}
注意使用XName.Get("Image", OneNoteNamespace)(其中OneNoteNamespace 是常量“http://schemas.microsoft.com/office/onenote/2013/onenote”)创建具有正确命名空间的元素以及将GDI 图像从内存序列化到Base64 format 的方法ToBase64。不幸的是,轮询(有关该主题的讨论,请参阅What is wrong with polling?)结合超时来确定检测过程是否已成功完成:
int total = 0;
do
{
Thread.Sleep(PollInterval);
this._page.Reload();
string result = this._page.ReadOcrText();
if (result != null)
return result;
} while (total++ < PollAttempts);
结果并不完美。然而,考虑到图像的质量,我认为它们非常令人满意。我可以在我的项目中成功使用该组件。仍然存在一个非常烦人的问题:有时,OneNote 在此过程中崩溃。大多数情况下,简单的重新启动即可解决此问题,但尝试从某些图像中识别文本会导致 OneNote 崩溃。
在GitHub查看代码
【讨论】:
不确定OCR,但是onenote API的文档站点是这个
http://msdn.microsoft.com/en-us/library/office/dn575425.aspx#sectionSection1
【讨论】: