文字识别文字识别

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Xml.Linq;


namespace WindowsFormsApplication1
{
    public class MyOcr
    {
        private string m_OcrPageID = "";


        /// <summary>
        /// https://msdn.microsoft.com/zh-cn/ff966472  error code
        /// https://msdn.microsoft.com/zh-cn/library/office/ff796230.aspx
        /// </summary>
        /// <param name="s_ImgPath">路径 可以是网络路径  </param>
        /// <param name="i_DelaySecond">延迟时间</param>
        /// <param name="s_Message"></param>
        /// <returns></returns>
        public bool fnOCR(string s_ImgPath, int i_DelaySecond, out string s_Message)
        {
            s_Message = "";
            //WebClient webClient = new WebClient();
            //webClient.DownloadFile(s_ImgPath, "download");

            //using (Image im = Image.FromFile("download"))
            //{
            using (Image im = Image.FromFile(s_ImgPath))
            {
                using (MemoryStream ms = new MemoryStream())
                {
                    //Image Type 只支持这些类型:auto|png|emf|jpg
                    string ImgExtension = "";
                    if (im.RawFormat.Guid == ImageFormat.Jpeg.Guid)
                    {
                        im.Save(ms, ImageFormat.Jpeg);
                        ImgExtension = "jpg";
                    }
                    else if (im.RawFormat.Guid == ImageFormat.Gif.Guid)
                    {
                        im.Save(ms, ImageFormat.Jpeg);
                        ImgExtension = "jpg";
                    }
                    else if (im.RawFormat.Guid == ImageFormat.Bmp.Guid)
                    {
                        im.Save(ms, ImageFormat.Bmp);
                        ImgExtension = "bmp";
                    }
                    else if (im.RawFormat.Guid == ImageFormat.Tiff.Guid)
                    {
                        im.Save(ms, ImageFormat.Tiff);
                        ImgExtension = "tiff";
                    }
                    else if (im.RawFormat.Guid == ImageFormat.Png.Guid)
                    {
                        im.Save(ms, ImageFormat.Png);
                        ImgExtension = "png";
                    }
                    else if (im.RawFormat.Guid == ImageFormat.Emf.Guid)
                    {
                        im.Save(ms, ImageFormat.Emf);
                        ImgExtension = "emf";
                    }
                    else
                    {
                        s_Message = "不支持的图片格式。";
                        return false;
                    }


                    byte[] buffer = ms.GetBuffer();
                    string _Base64 = Convert.ToBase64String(buffer);


                    var onenoteApp = new Microsoft.Office.Interop.OneNote.Application();
                    string notebookXml;


                    //先检查 是否已 建立过 page
                    if (m_OcrPageID == "")
                    {
                        //hsSections
                        onenoteApp.GetHierarchy(null, Microsoft.Office.Interop.OneNote.HierarchyScope.hsSections, out notebookXml, Microsoft.Office.Interop.OneNote.XMLSchema.xsCurrent);
                        var doc = XDocument.Parse(notebookXml);
                        var ns = doc.Root.Name.Namespace;
                        var sectionNode = doc.Descendants(ns + "Section").FirstOrDefault();
                        var sectionID = sectionNode.Attribute("ID").Value;
                        onenoteApp.CreateNewPage(sectionID, out m_OcrPageID);
                    }


                    //hsPages
                    onenoteApp.GetHierarchy(null, Microsoft.Office.Interop.OneNote.HierarchyScope.hsPages, out notebookXml, Microsoft.Office.Interop.OneNote.XMLSchema.xsCurrent);


                    var _pdoc = XDocument.Parse(notebookXml);
                    var _pns = _pdoc.Root.Name.Namespace;
                    var _page = new XDocument(new XElement(_pns + "Page", new XAttribute("ID", m_OcrPageID),
                                               new XElement(_pns + "Outline",
                                                 new XElement(_pns + "OEChildren",
                                                   new XElement(_pns + "OE",
                                                     new XElement(_pns + "Image", new XAttribute("format", ImgExtension), new XAttribute("originalPageNumber", "0"),
                                                       new XElement(_pns + "Position", new XAttribute("x", "0"), new XAttribute("y", "0"), new XAttribute("z", "0")),
                                                         new XElement(_pns + "Size", new XAttribute("width", im.Width.ToString()), new XAttribute("height", im.Height.ToString())),
                                                           new XElement(_pns + "Data", _Base64)))))));


                    onenoteApp.UpdatePageContent(_page.ToString());


                    //线程休眠时间,单位毫秒,若图片很大,则延长休眠时间,保证Onenote OCR完毕
                    System.Threading.Thread.Sleep(i_DelaySecond);


                    string _pageXml = "";


                    onenoteApp.GetPageContent(m_OcrPageID, out _pageXml, Microsoft.Office.Interop.OneNote.PageInfo.piBasic);


                    XDocument _OCRText = XDocument.Parse(_pageXml);
                    //抓取识别文字
                    IEnumerable<XElement> i_OCRText = _OCRText.Descendants(_pns + "OCRText");
                    if (i_OCRText.Count() > 0)
                    {
                        s_Message = i_OCRText.FirstOrDefault().Value;
                    }


                    onenoteApp.DeleteHierarchy(m_OcrPageID);
                }
            }
            return true;
        }
    }
}

安装 office2013
System.InvalidCastException:“无法将类型为“Microsoft.Office.Interop.OneNote.ApplicationClass”的 COM 对象强制转换为接口类型“Microsoft.Office.Interop.OneNote.IApplicationCOM”。此操作失败的原因是对 IID 为“{452AC71A-B655-4967-A208-A4CC39DD7949}”的接口的 COM 组件调用 QueryInterface 因以下错误而失败: 加载类型库/DLL 时出错。 (异常来自 HRESULT:0x80029C4A (TYPE_E_CANTLOADLIBRARY))。”

没有配置好office
异常来自 HRESULT:0x80042030


office 卸载不了 用cclear把卸载对应的程序删除了 给修好了。

https://www.office26.com/office/office_2990.html
https://www.cnblogs.com/baiboy/p/nltk1.html
https://www.cnblogs.com/baiboy/p/wpf1.html
https://docs.microsoft.com/en-us/previous-versions/office/developer/office-2007/aa286798(v=office.12)
https://docs.microsoft.com/en-us/previous-versions/office/developer/office-2007/ms788684(v=office.12)
https://docs.microsoft.com/en-us/previous-versions/office/developer/office-2007/ms788684(v=office.12)?redirectedfrom=MSDN
https://docs.microsoft.com/zh-cn/archive/msdn-magazine/2010/july/onenote-2010-creating-onenote-2010-extensions-with-the-onenote-object-model#%E4%BD%BF%E7%94%A8-com-api-%E8%AE%BF%E9%97%AE-onenote-%E6%95%B0%E6%8D%AE

无法嵌入互操作类型“Microsoft.Office.Interop.Excel.ApplicationClass”。请改用适用的接口

解决办法是将引用的DLL:Microsoft.Office.Interop.Excel;的嵌入互操作类型改为false,就可以了。

操作步骤:

右键引用中Microsoft.Office.Interop.Excel属性,嵌入互操作类型改为false, OK。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值