[C#] 纯文本查看 复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.Xml;
using System.IO;
using System.Configuration;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using Microsoft.Office.Core;
using Microsoft.Office.Interop.Graph;
using Microsoft.Office.Interop.OneNote;
using System.Diagnostics;
using System.Windows.Forms;
namespace LH_design_of_graduation
{
public class OrcImage
{
int waitTime = 100;
string tmpPath = AppDomain.CurrentDomain.BaseDirectory + @"newfile";
private Tuple<string, int, int> GetBase64(string strImgPath)
{
return GetBase64(new FileInfo(strImgPath));
}
/// <summary>
/// 获取图片的Base64编码
/// </summary>
/// <param name="file"></param>
/// <returns></returns>
/// 图片资源应该转换成为 Base64 码
private Tuple<string, int, int> GetBase64(FileInfo file)
{
using (MemoryStream ms = new MemoryStream())
{
Bitmap bp = new Bitmap(file.FullName);
switch (file.Extension.ToLower())
{
case ".jpg":
bp.Save(ms, ImageFormat.Jpeg);
break;
case ".jpeg":
bp.Save(ms, ImageFormat.Jpeg);
break;
case ".gif":
bp.Save(ms, ImageFormat.Gif);
break;
case ".bmp":
bp.Save(ms, ImageFormat.Bmp);
break;
case ".tiff":
bp.Save(ms, ImageFormat.Tiff);
break;
case ".png":
bp.Save(ms, ImageFormat.Png);
break;
case ".emf":
bp.Save(ms, ImageFormat.Emf);
break;
default:
return new Tuple<string, int, int>("不支持的图片格式。", 0, 0);
}
byte[] buffer = ms.GetBuffer();
return new Tuple<string, int, int>(Convert.ToBase64String(buffer), bp.Width, bp.Height);
}
}
public string Orc_Img(FileInfo fi)
{
// 向Onenote2013中插入图片 新建一个 OneNote 对象
var onenoteApp = new Microsoft.Office.Interop.OneNote.Application(); //onenote提供的API
///***************************************************************************************/
///建立一个 One 文件
string sectionID;
string tmpPath = AppDomain.CurrentDomain.BaseDirectory + "tmpPath/";
onenoteApp.OpenHierarchy(tmpPath + "newfile.one", null, out sectionID, CreateFileType.cftSection);
//创建一个新的页面,所以我们要使用到 CreateNewPage 函数,它的 out 参数则存储了 pageID
string pageID = "{A975EE72-19C3-4C80-9C0E-EDA576DAB5C6}{1}{B0}"; // 格式 {guid}{tab}{??}
onenoteApp.CreateNewPage(sectionID, out pageID, NewPageStyle.npsBlankPageNoTitle);
/********************************************************************************/
string notebookXml;
onenoteApp.GetHierarchy(null, HierarchyScope.hsPages, out notebookXml);
var doc = XDocument.Parse(notebookXml);
var ns = doc.Root.Name.Namespace;
var pageNode = doc.Descendants(ns + "Page").FirstOrDefault();
var existingPageId = pageNode.Attribute("ID").Value = pageID;
//按照 onenote 的标准利用 XmlDocument 对象将资源进行包装
if (pageNode != null)
{
Tuple<string, int, int> imgInfo = this.GetBase64(fi);
var page = new XDocument(
new XElement(
ns + "Page",
new XElement(
ns + "Outline",
new XElement(
ns + "OEChildren",
new XElement(
ns + "OE",
new XElement(
ns + "Image",
new XAttribute("format", fi.Extension.Remove(0, 1)),
new XAttribute("originalPageNumber", "0"),
new XElement(
ns + "Position",
new XAttribute("x", "0"),
new XAttribute("y", "0"),
new XAttribute("z", "0")),
new XElement(
ns + "Size",
new XAttribute("width", imgInfo.Item2),
new XAttribute("height", imgInfo.Item3)),
new XElement(ns + "Data", imgInfo.Item1)
)
)
)
)
)
);
page.Root.SetAttributeValue("ID", existingPageId);
//将数据插入到 page 中
onenoteApp.UpdatePageContent(page.ToString(), DateTime.MinValue);
//延时0.1秒
flag1:
Stopwatch sw = new Stopwatch();// 开始计时
sw.Start();
int fileSize = Convert.ToInt32(fi.Length / 1024 / 1024); // 文件大小 单位M
System.Threading.Thread.Sleep(waitTime * (fileSize > 1 ? fileSize : 1));
string pageXml;
onenoteApp.GetPageContent(existingPageId, out pageXml, PageInfo.piBinaryData);
/*********************************************************************************/
//通过 XmlDocument 对象,获取到识别结果
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(pageXml);
XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlDoc.NameTable);
nsmgr.AddNamespace("one", ns.ToString());
XmlNode xmlNode = xmlDoc.SelectSingleNode("//one:Image//one:OCRText", nsmgr);
if (xmlNode == null)
{
goto flag1;
}
string strRet = xmlNode.InnerText;
sw.Stop();//结束计时
long times = sw.ElapsedMilliseconds;
Console.WriteLine("执行查询总共使用了" + times + "毫秒");//显示时间
///**********************************************************************/
onenoteApp.DeleteHierarchy(existingPageId, DateTime.MinValue); // 摧毁原始页面
//////////////////////////////////////////////////////////////////////////////
System.Threading.Thread.Sleep(100);
return strRet;
}
return "没有识别";
}
}
}