6

是否有 API 可以使用 Onenote OCR 功能自动识别图像中的文本?

4

5 回答 5

6

如果您在执行程序的同一台机器上安装了 OneNote 客户端,您可以在 OneNote 中创建一个页面并通过 COM API 插入图像。然后您可以阅读包含 OCR 文本的 XML 格式的页面。

你想用

  1. Application.CreateNewPage创建一个页面
  2. Application.UpdatePageContent插入图像
  3. Application.GetPageContent读取页面内容并在 XML 中查找OCRData和元素。OCRText

OneNote COM API 记录在这里:http: //msdn.microsoft.com/en-us/library/office/jj680120 (v=office.15).aspx

于 2014-10-10T04:03:27.143 回答
2

当您通过 API 在 OneNote 中的页面上放置图像时,任何图像都会自动进行 OCR 处理。然后,用户将能够在 OneNote 中搜索图像中的任何文本。但是,此时您无法拉回图像并阅读 OCR 文本。

如果这是您感兴趣的功能,我邀请您访问我们的 UserVoice 网站并提交此想法: http: //onenote.uservoice.com/forums/245490-onenote-developers

更新:对这个想法投票:https ://onenote.uservoice.com/forums/245490-onenote-developer-apis/suggestions/10671321-make-ocr-available-in-the-c-api

- 詹姆士

于 2014-08-12T16:26:23.403 回答
1

这里有一个非常好的示例: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;
}
于 2015-06-15T14:04:59.610 回答
1

由于我将删除我的博客(在另一篇文章中提到),我想我应该在此处添加内容以供将来参考:

用法

让我们先来看看如何使用该组件:OnenoteOcrEngine类实现了核心功能,并实现了接口IOcrEngine,它提供了一个单一的方法:

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?需要使用 onenote Interop 从图像中提取文本的文档?这正是我所做的:

  1. 使用COM 互操作连接到 OneNote
  2. 创建一个包含要处理的图像的临时页面
  3. 显示临时页面(重要因为 OneNote 不会执行 OCR,否则)
  4. 在页面的 XML 代码中轮询包含OCRText标记的OCRData标记。
  5. 删除临时页面

挑战包括解析我决定使用LINQ to XML的 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 图像的方法 ToBase64 的用法转换成Base64 格式。不幸的是,轮询(参见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 上的代码

于 2020-05-13T21:50:01.190 回答
0

不确定 OCR,但是 onenote API 的文档站点是这个

http://msdn.microsoft.com/en-us/library/office/dn575425.aspx#sectionSection1

于 2014-08-12T12:27:37.957 回答