询问者
OneNote2010 ocr C#有时成功有时失败

问题
-
为什么调用onenote2010识别的时候会出现有时候可以识别成功,有时候识别失败的情况呢?类似这种情况
public string Orc_Img(FileInfo fi)
{
// 向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);
onenoteApp.UpdatePageContent(page.ToString(), DateTime.MinValue);
// 线程休眠时间,单位毫秒,若图片很大,则延长休眠时间,保证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);
/*********************************************************************************/
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);
string strRet = xmlNode.InnerText;
/**********************************************************************/
onenoteApp.DeleteHierarchy(sectionID, DateTime.MinValue, true); // 摧毁原始页面
return strRet;
}
return "没有识别";
}
全部回复
-
您好,
当前论坛更多的是处理与Office客户端相关的非代码问题。由于您的问题涉及到代码,建议您到以下论坛重新发帖:
https://social.msdn.microsoft.com/Forums/office/en-US/home?forum=officegeneral
上述论坛为英文论坛,目前暂时没有Office开发相关的中文论坛,望谅解。
Steve Fan
如果以上回复对您有所帮助,建议您将其“标记为答复”。
如果您对我们的论坛支持有任何的建议,可以通过此邮箱联系我们: tnsf@microsoft.com.
- 已编辑 Steve FanModerator 2017年11月20日 7:25
- 已建议为答案 Steve FanModerator 2017年11月30日 9:43