1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
|
public class OrcImage
{
private static readonly string tmpPath = AppDomain.CurrentDomain.BaseDirectory + "tmpPath/";
private static readonly int waitTime = Convert.ToInt32(ConfigurationManager.AppSettings["WaitTime"]);
<span class="k">private</span> <span class="n">Tuple</span><span class="p"><</span><span class="kt">string</span><span class="p">,</span> <span class="kt">int</span><span class="p">,</span> <span class="kt">int</span><span class="p">></span> <span class="n">GetBase64</span><span class="p">(</span><span class="kt">string</span> <span class="n">strImgPath</span><span class="p">)</span>
<span class="p">{</span>
<span class="k">return</span> <span class="n">GetBase64</span><span class="p">(</span><span class="k">new</span> <span class="n">FileInfo</span><span class="p">(</span><span class="n">strImgPath</span><span class="p">));</span>
<span class="p">}</span>
<span class="c1">/// <summary>
/// 获取图片的Base64编码
/// </summary>
/// <param name="file"></param>
/// <returns></returns>
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;
<span class="k">case</span> <span class="s">".jpeg"</span><span class="p">:</span>
<span class="n">bp</span><span class="p">.</span><span class="n">Save</span><span class="p">(</span><span class="n">ms</span><span class="p">,</span> <span class="n">ImageFormat</span><span class="p">.</span><span class="n">Jpeg</span><span class="p">);</span>
<span class="k">break</span><span class="p">;</span>
<span class="k">case</span> <span class="s">".gif"</span><span class="p">:</span>
<span class="n">bp</span><span class="p">.</span><span class="n">Save</span><span class="p">(</span><span class="n">ms</span><span class="p">,</span> <span class="n">ImageFormat</span><span class="p">.</span><span class="n">Gif</span><span class="p">);</span>
<span class="k">break</span><span class="p">;</span>
<span class="k">case</span> <span class="s">".bmp"</span><span class="p">:</span>
<span class="n">bp</span><span class="p">.</span><span class="n">Save</span><span class="p">(</span><span class="n">ms</span><span class="p">,</span> <span class="n">ImageFormat</span><span class="p">.</span><span class="n">Bmp</span><span class="p">);</span>
<span class="k">break</span><span class="p">;</span>
<span class="k">case</span> <span class="s">".tiff"</span><span class="p">:</span>
<span class="n">bp</span><span class="p">.</span><span class="n">Save</span><span class="p">(</span><span class="n">ms</span><span class="p">,</span> <span class="n">ImageFormat</span><span class="p">.</span><span class="n">Tiff</span><span class="p">);</span>
<span class="k">break</span><span class="p">;</span>
<span class="k">case</span> <span class="s">".png"</span><span class="p">:</span>
<span class="n">bp</span><span class="p">.</span><span class="n">Save</span><span class="p">(</span><span class="n">ms</span><span class="p">,</span> <span class="n">ImageFormat</span><span class="p">.</span><span class="n">Png</span><span class="p">);</span>
<span class="k">break</span><span class="p">;</span>
<span class="k">case</span> <span class="s">".emf"</span><span class="p">:</span>
<span class="n">bp</span><span class="p">.</span><span class="n">Save</span><span class="p">(</span><span class="n">ms</span><span class="p">,</span> <span class="n">ImageFormat</span><span class="p">.</span><span class="n">Emf</span><span class="p">);</span>
<span class="k">break</span><span class="p">;</span>
<span class="k">default</span><span class="p">:</span>
<span class="k">return</span> <span class="k">new</span> <span class="n">Tuple</span><span class="p"><</span><span class="kt">string</span><span class="p">,</span> <span class="kt">int</span><span class="p">,</span> <span class="kt">int</span><span class="p">>(</span><span class="s">"不支持的图片格式。"</span><span class="p">,</span> <span class="m">0</span><span class="p">,</span> <span class="m">0</span><span class="p">);</span>
<span class="p">}</span>
<span class="kt">byte</span><span class="p">[]</span> <span class="n">buffer</span> <span class="p">=</span> <span class="n">ms</span><span class="p">.</span><span class="n">GetBuffer</span><span class="p">();</span>
<span class="k">return</span> <span class="k">new</span> <span class="n">Tuple</span><span class="p"><</span><span class="kt">string</span><span class="p">,</span> <span class="kt">int</span><span class="p">,</span> <span class="kt">int</span><span class="p">>(</span><span class="n">Convert</span><span class="p">.</span><span class="n">ToBase64String</span><span class="p">(</span><span class="n">buffer</span><span class="p">),</span> <span class="n">bp</span><span class="p">.</span><span class="n">Width</span><span class="p">,</span> <span class="n">bp</span><span class="p">.</span><span class="n">Height</span><span class="p">);</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="k">public</span> <span class="kt">string</span> <span class="n">Orc_Img</span><span class="p">(</span><span class="n">FileInfo</span> <span class="n">fi</span><span class="p">)</span>
<span class="p">{</span>
<span class="c1">// 向Onenote2010中插入图片
var onenoteApp = new Microsoft.Office.Interop.OneNote.Application(); //onenote提供的API
/*******/
string sectionID;
onenoteApp.OpenHierarchy(tmpPath + "newfile.one", null, out sectionID, CreateFileType.cftSection);
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;
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);
<span class="n">onenoteApp</span><span class="p">.</span><span class="n">UpdatePageContent</span><span class="p">(</span><span class="n">page</span><span class="p">.</span><span class="n">ToString</span><span class="p">(),</span> <span class="n">DateTime</span><span class="p">.</span><span class="n">MinValue</span><span class="p">);</span>
<span class="c1">// 线程休眠时间,单位毫秒,若图片很大,则延长休眠时间,保证Onenote OCR完毕
int fileSize = Convert.ToInt32(fi.Length / 1024 / 1024); // 文件大小 单位M
System.Threading.Thread.Sleep(waitTime * (fileSize > 1 ? fileSize : 1)); // 小于1M的都默认1M
string pageXml;
onenoteApp.GetPageContent(existingPageId, out pageXml, PageInfo.piBinaryData);
<span class="cm">/*********************************************************************************/</span>
<span class="n">XmlDocument</span> <span class="n">xmlDoc</span> <span class="p">=</span> <span class="k">new</span> <span class="n">XmlDocument</span><span class="p">();</span>
<span class="n">xmlDoc</span><span class="p">.</span><span class="n">LoadXml</span><span class="p">(</span><span class="n">pageXml</span><span class="p">);</span>
<span class="n">XmlNamespaceManager</span> <span class="n">nsmgr</span> <span class="p">=</span> <span class="k">new</span> <span class="n">XmlNamespaceManager</span><span class="p">(</span><span class="n">xmlDoc</span><span class="p">.</span><span class="n">NameTable</span><span class="p">);</span>
<span class="n">nsmgr</span><span class="p">.</span><span class="n">AddNamespace</span><span class="p">(</span><span class="s">"one"</span><span class="p">,</span> <span class="n">ns</span><span class="p">.</span><span class="n">ToString</span><span class="p">());</span>
<span class="n">XmlNode</span> <span class="n">xmlNode</span> <span class="p">=</span> <span class="n">xmlDoc</span><span class="p">.</span><span class="n">SelectSingleNode</span><span class="p">(</span><span class="s">"//one:Image//one:OCRText"</span><span class="p">,</span> <span class="n">nsmgr</span><span class="p">);</span>
<span class="kt">string</span> <span class="n">strRet</span> <span class="p">=</span> <span class="n">xmlNode</span><span class="p">.</span><span class="n">InnerText</span><span class="p">;</span>
<span class="cm">/**********************************************************************/</span>
<span class="n">onenoteApp</span><span class="p">.</span><span class="n">DeleteHierarchy</span><span class="p">(</span><span class="n">sectionID</span><span class="p">,</span> <span class="n">DateTime</span><span class="p">.</span><span class="n">MinValue</span><span class="p">,</span> <span class="k">true</span><span class="p">);</span> <span class="c1">// 摧毁原始页面
return strRet;
}
<span class="k">return</span> <span class="s">"没有识别"</span><span class="p">;</span>
<span class="p">}</span>
<span class="p">}</span>
|