/*自定义导航栏*/

【基于WPF+OneNote+Oracle的中文图片识别系统阶段总结】之篇二:基于OneNote难点突破和批量识别

篇一:WPF常用知识以及本项目设计总结:http://www.cnblogs.com/baiboy/p/wpf.html

篇二:基于OneNote难点突破和批量识别:http://www.cnblogs.com/baiboy/p/wpf1.html

篇三:批量处理后的txt文件入库处理:http://www.cnblogs.com/baiboy/p/wpf2.html

篇四:关于OneNote入库处理以及审核:http://www.cnblogs.com/baiboy/p/wpf3.html


【小记】:大胆尝试才能突破,某个中医药大学有一批图片需要处理(ORC),然后进行数据挖掘。之前没有接触过ORC这个东西,但是还是应允了。在网上搜索一番,关于中文图片识别,最终敲定为基于微软的OneNote,其识别率相对较高。网上这个技术点的资料真心不多,后来于博客园找到一篇博文,但是那个程序还是bug百出,而且只是单处理。后来经过一番摸索逐个突破,批处理完成。然后进行界面设计,这些零碎工作完成后,便是入库处理。由于OneNote生成的xml文件封装好的,即不可视的。便将其代码处理生成txt文件,再进行Oracle入库处理。入库前需要文件内容审核,并且在WPF开发中数据绑定和分页中做了独特处理。现在经过半个月的工作,本项目做个阶段总结。一则知识总结便于二次开发,尽量保持程序流畅性,核心知识做以梳理;另外,相关WPF和OneNote常用技术共享,便于部分园友所需。本人技术有限,欢迎交流。项目还未结束,暂作阶段文章发布,随后相继发布。


篇二:基于OneNote难点突破和批量识别

【1】开篇概述:在对本章技术介绍前,还是梳理下思路。描述下本章功能和开发过程。做个系统大致了解之后,在粘贴出本节效果图配以完整代码,然后分拆之,个体技术剖析。这样既可以达到全局整体效果,也可以对局部技术或者知识点做以小结。功能看图描述:本程序开发基于C#+WPF,然后这些条件具备前需要安装office2010以上版本,包含OneNote即可

 

完整代码:

 

namespace OnenoteOCRDemo
{
    /// <summary>
    /// Main.xaml 的交互逻辑
    /// </summary>
    public partial class Main : Window
    {
        #region 全局变量
        private string __OutputFileName = string.Empty;
        private WebClient client = new WebClient();
        private delegate void UpdateProgressBarDelegate(System.Windows.DependencyProperty dp, Object value);
        #endregion

        #region 系统函数
        public Main()
        {
            InitializeComponent();
        }
        #endregion

        #region 用户函数
        #region 验证文件夹是否存在,以及是否为空 :2014-7-10 17:12:57
        //验证文件夹是否存在,以及是否为空
        //时间:2014-7-10 14:54:44
        //作者:白宁超
        private bool fn数据验证()
        {
            if ((bool)this.rbtn本地图片.IsChecked)
            {
                if (!Directory.Exists(this.txtfile.Text))
                {
                    this.labMsg.Content = "目录不存在,重新选择!";
                    this.txtfile.Focus();
                    return true;
                }
                System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(@txtfile.Text);
                if (di.GetFiles().Length + di.GetDirectories().Length == 0)
                {
                    this.labMsg.Content = "目录为空!";
                    this.txtfile.Focus();
                    return true;
                }
            }
            if (!Directory.Exists(this.txt输出目录.Text))
            {
                this.labMsg.Content = "输出目录不存在,请重新选择。";
                this.txt输出目录.Focus();
                return true;
            }

            return false;
        }
        #endregion

        #region   遍历出文件中图片路径
        //文件中选择所有文件处理
        //时间:2014-7-10 17:17:29
        //作者:白宁超
        public List<string> GetImgPath(string Filepath)
        {
            DirectoryInfo d = new DirectoryInfo(Filepath);
            ArrayList Flst = GetAll(d);              //存放图片完整路径

            List<string> imgpath = new List<string>();
            foreach (object o in Flst)
            {
                imgpath.Add(@txtfile.Text + "\\" + o.ToString());
            }
            return imgpath;//返回图片路径集合
        }
        #endregion

        #region  获取图片的过程
        private void fnStartDownload(string v_strImgPath, string v_strOutputDir, out string v_strTmpPath)
        {
            int n = v_strImgPath.LastIndexOf('/');
            string URLAddress = v_strImgPath.Substring(0, n);
            string fileName = v_strImgPath.Substring(n + 1, v_strImgPath.Length - n - 1);
            this.__OutputFileName = v_strOutputDir + "\\" + fileName.Substring(0, fileName.LastIndexOf(".")) + ".txt";

            if (!Directory.Exists(System.Configuration.ConfigurationManager.AppSettings["tmpPath"]))
            {
                Directory.CreateDirectory(System.Configuration.ConfigurationManager.AppSettings["tmpPath"]);
            }

            string Dir = System.Configuration.ConfigurationManager.AppSettings["tmpPath"];
            v_strTmpPath = Dir + "\\" + fileName;
            this.client.DownloadFile(v_strImgPath, v_strTmpPath);
        }
        #endregion

        #region//删除指定目录下的所有文件夹以及文件
        private void DelFillOrDir(string strPath)
        {

            if (Directory.GetDirectories(strPath).Length > 0 || Directory.GetFiles(strPath).Length > 0)
            {
                // 获得文件夹数组 
                string[] strDirs = System.IO.Directory.GetDirectories(strPath); // 获得文件数组 
                string[] strFiles = System.IO.Directory.GetFiles(strPath); // 遍历所有子文件夹 
                foreach (string strFile in strFiles)
                {
                    // 删除文件夹 
                    System.IO.File.Delete(strFile);
                } // 遍历所有文件 
                foreach (string strdir in strDirs)
                { // 删除文件 
                    System.IO.Directory.Delete(strdir, true);
                }
            } // 成功 
        }
        #endregion

        #region//删除文件
        public static bool DeleteFile(string xmlPath)
        {
            FileInfo newFile = new FileInfo(xmlPath);

            if (newFile.Exists == true)
            {
                newFile.Delete();
                return true;
            }
            else
            {
                return false;
            }
        }
        #endregion

        #region//删除xml数据
        public void DeleteXML(string xpath)
        {
            var onenoteApp = new Microsoft.Office.Interop.OneNote.Application();  //onenote提供的API
            string notebookXml;
            onenoteApp.GetHierarchy(null, Microsoft.Office.Interop.OneNote.HierarchyScope.hsPages, out notebookXml);
            var docc = XDocument.Parse(notebookXml);
            var ns = docc.Root.Name.Namespace;
            XDocument doc = XDocument.Load(xpath);
            string author = "USER-";

            XElement xe = (from db in
                               doc.Element(ns + "Page").Elements(ns + "Outline")
                           where db.Attribute("author").Value == author
                           select db).Single() as XElement;
            try
            {
                xe.Remove();
                doc.Save(xpath);
            }
            catch
            {
            }
        }
        #region  OCR写入Onenote和生成文字整个过程
        //string xpath = @"C:\Users\Administrator\Desktop\OCR_Onenote\OnenoteOCR2.1\tmp.xml";
        private void fnOCR(string v_strImgPath)
        {
      
            ORC(v_strImgPath);
        }
        public void ORC(string v_strImgPath)
        {
            FileInfo file = new FileInfo(v_strImgPath);
            #region  //获取图片的Base64编码
            using (MemoryStream ms = new MemoryStream())
            {
                Bitmap bp = new Bitmap(v_strImgPath);
                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:
                        this.labMsg.Content = "不支持的图片格式。";
                        return;
                }
                byte[] buffer = ms.GetBuffer();
                string _Base64 = Convert.ToBase64String(buffer);
            #endregion
                //向Onenote2010中插入图片
                var onenoteApp = new Microsoft.Office.Interop.OneNote.Application();  //onenote提供的API
                /***************************************************************************************/
                string sectionID;
                onenoteApp.OpenHierarchy(@"C:\Users\Administrator\Documents\OneNote 笔记本\个人\newfile.one", null, out sectionID, Microsoft.Office.Interop.OneNote.CreateFileType.cftSection);
                string pageID = "{A975EE72-19C3-4C80-9C0E-EDA576DAB5C6}{1}{B0}";
                onenoteApp.CreateNewPage(sectionID, out pageID, Microsoft.Office.Interop.OneNote.NewPageStyle.npsBlankPageNoTitle);
                /********************************************************************************/
                string notebookXml;
                onenoteApp.GetHierarchy(null, Microsoft.Office.Interop.OneNote.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;
                #region
                if (pageNode != null)
                {
                    //Image Type 只支持这些类型:auto|png|emf|jpg
                    string ImgExtension = file.Extension.ToLower().Substring(1);
                    switch (ImgExtension)
                    {
                        case "jpg":
                            ImgExtension = "jpg";
                            break;
                        case "png":
                            ImgExtension = "png";
                            break;
                        case "emf":
                            ImgExtension = "emf";
                            break;
                        default:
                            ImgExtension = "auto";
                            break;
                    }

                #endregion
                    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", ImgExtension), 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", bp.Width.ToString()), new XAttribute("height", bp.Height.ToString())),
                                                          new XElement(ns + "Data", _Base64)))))));
                    page.Root.SetAttributeValue("ID", existingPageId);


                    //保存图片进入Onenote页面
                    //注意以下几点:(待解决)
                    //1,onenote页面自动会生成一个页面,可能造成一直ocr错误。删除默认页面,新建空页
                    //2,图片存储在新建页面中,通过如下代码实现的,以追加存储方式进行。
                    onenoteApp.UpdatePageContent(page.ToString(), DateTime.MinValue);

                    //线程休眠时间,单位毫秒,若图片很大,则延长休眠时间,保证Onenote OCR完毕
                    System.Threading.Thread.Sleep(Int32.Parse(System.Configuration.ConfigurationManager.AppSettings["WaitTIme"]));

                    string pageXml;
                    onenoteApp.GetPageContent(existingPageId, out pageXml, Microsoft.Office.Interop.OneNote.PageInfo.piBinaryData);
                    //获取OCR后的内容
                    FileStream tmpXml = new FileStream(System.Configuration.ConfigurationManager.AppSettings["tmpPath"] + @"\tmp.xml", FileMode.Create, FileAccess.ReadWrite);
                    StreamWriter sw = new StreamWriter(tmpXml);
                    sw.Write(pageXml);
                    sw.Flush();
                    sw.Close();
                    tmpXml.Close();
                    //加载xml中的数据
                    FileStream tmpOnenote = new FileStream(System.Configuration.ConfigurationManager.AppSettings["tmpPath"] + @"\tmp.xml", FileMode.Open, FileAccess.ReadWrite);
                    XmlReader reader = XmlReader.Create(tmpOnenote);
                    XElement rdlc = XElement.Load(reader);

                    // rdlc.Save(xpath);



                    /*****************************************************************************************************/
                    XmlNameTable nameTable = reader.NameTable;
                    XmlNamespaceManager mgr = new XmlNamespaceManager(nameTable);
                    mgr.AddNamespace("one", ns.ToString());

                    StringReader sr = new StringReader(pageXml);
                    XElement onenote = XElement.Load(sr);
                    ////读取xml中数据,并保存到文本中。
                    //XDocument doc1 = XDocument.Load(xpath);
                    var xml = from o in onenote.XPathSelectElements("//one:Image", mgr)
                              select o.XPathSelectElement("//one:OCRText", mgr).Value;
                    this.txtOCRed.Text = xml.First().ToString();

                    /**********************************************************************/
                    sr.Close();
                    reader.Close();
                    tmpOnenote.Close();
                    //DeleteXML(xpath);
                    onenoteApp.DeleteHierarchy(existingPageId);
                    //onenoteApp.DeletePageContent(existingPageId, "{242BE490-7BF9-43D5-B719-3999E7631259}{46}{B0}");
                    //onenoteApp.DeletePageContent(existingPageId, "{242BE490-7BF9-43D5-B719-3999E7631259}{50}{B0}");
                }
            }
        }
        #endregion
        #endregion
        #endregion

        #region 系统事件

        #region 搜索文件夹中的文件:2014-7-10 17:12:57
        //搜索文件夹中的文件
        //时间:2014-7-10 14:54:44
        //作者:白宁超
        ArrayList GetAll(DirectoryInfo dir)
        {
            try
            {
                ArrayList FileList = new ArrayList();//定义数组存放图片文件名
                FileInfo[] allFile = dir.GetFiles();
                foreach (FileInfo fi in allFile)
                {
                    FileList.Add(fi.Name);//遍历图片,并添加到数组
                }

                DirectoryInfo[] allDir = dir.GetDirectories();
                foreach (DirectoryInfo d in allDir)
                {
                    GetAll(d);
                }
                return FileList;
            }
            catch (Exception e)
            {
                labMsg.Content = e.Message + "文件夹选择方式错误,重新选择!!";
                return null;
            }
        }
        #endregion

        #region 选择遍历文件夹图片
        //文件中选择所有文件处理
        //时间:2014-7-10 14:54:44
        //作者:白宁超
        private void btn浏览_Click(object sender, RoutedEventArgs e)
        {
            txtmulu.Text = null;
            FolderBrowserDialog dialog = new FolderBrowserDialog();
            dialog.Description = "请选择待处理目录";
            if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                string foldPath = dialog.SelectedPath;
                txtfile.Text = foldPath;
                DirectoryInfo d = new DirectoryInfo(@txtfile.Text);
                ArrayList Flst = GetAll(d);
                foreach (object o in Flst)
                {
                    txtmulu.Text += "正在ORC图片: 【" + o.ToString() + "】\r\n";
                }
            }
            else
            {
                labMsg.Content = "打开失败!!";
            }
            #region 上传单个图片  2014-7-10 15:34:17
            /*OpenFileDialog ofd = new OpenFileDialog();
            ofd.Title = "请选择一个本地图片";
            ofd.Multiselect = false;
            ofd.Filter = "支持的图片格式(*.jpg,*.jpeg,*.gif,*.bmp,*.png,*.tiff,*.emf)|*.jpg;*.jpeg;*.gif;*.bmp;*.png;*.tiff;*.emf";

            if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                this.txt本地图片.Text = ofd.FileName;
                this.img图片.Source = new BitmapImage(new Uri(ofd.FileName, UriKind.RelativeOrAbsolute));

                this.labMsg.Content = "本地图片已成功加载。";
            }*/
            #endregion
        }
        #endregion

        #region  选择本地图片OCR转化
        private void rbtn本地图片_Checked(object sender, RoutedEventArgs e)
        {
            if (this.IsInitialized)
            {
                if ((bool)this.rbtn本地图片.IsChecked)
                {
                    this.txtfile.Text = string.Empty;
                    this.txtfile.IsEnabled = true;
                    this.btn浏览.IsEnabled = true;
                    this.txtfile.Focus();
                    this.txtmulu.Text = null;
                }
            }
        }
        #endregion

        #region  选择需要输入处理的文件
        private void btn浏览_Click_1(object sender, RoutedEventArgs e)
        {
            txtmulu.Text = null;
            FolderBrowserDialog dialog = new FolderBrowserDialog();
            dialog.Description = "请选择待处理目录";
            if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                string foldPath = dialog.SelectedPath;
                txtfile.Text = foldPath;
                DirectoryInfo d = new DirectoryInfo(@txtfile.Text);
                ArrayList Flst = GetAll(d);
                foreach (object o in Flst)
                {
                    txtmulu.Text += "正在ORC图片: 【" + o.ToString() + "】\r\n";
                }
            }
            else
            {
                labMsg.Content = "打开失败!!";
            }
            #region 上传单个图片  2014-7-10 15:34:17
            /*OpenFileDialog ofd = new OpenFileDialog();
            ofd.Title = "请选择一个本地图片";
            ofd.Multiselect = false;
            ofd.Filter = "支持的图片格式(*.jpg,*.jpeg,*.gif,*.bmp,*.png,*.tiff,*.emf)|*.jpg;*.jpeg;*.gif;*.bmp;*.png;*.tiff;*.emf";

            if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                this.txt本地图片.Text = ofd.FileName;
                this.img图片.Source = new BitmapImage(new Uri(ofd.FileName, UriKind.RelativeOrAbsolute));

                this.labMsg.Content = "本地图片已成功加载。";
            }*/
            #endregion
        }
        #endregion

        #region 选择输出文件夹
        private void btn输出浏览_Click(object sender, RoutedEventArgs e)
        {
            FolderBrowserDialog fbd = new FolderBrowserDialog();
            fbd.Description = "请选择一个输出目录";
           
            if (fbd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                this.txt输出目录.Text = fbd.SelectedPath;
                DelFillOrDir(this.txt输出目录.Text);
            }
        }
       
        #endregion
        
        #region  清空输出文件
        private void btn清空_Click(object sender, RoutedEventArgs e)
        {
            OCRtxt.Text = "";
            this.OCRtxt.Focus();
        }
        #endregion

        #region  ORC转化过程
        private void btnOCR_Click(object sender, RoutedEventArgs e)
        {
            if (this.fn数据验证())
            {
         
                OCRtxt.Text = "";
                return;
            }
            try
            {
                DirectoryInfo dir = new DirectoryInfo(this.txt输出目录.Text);
                if ((bool)this.rbtn本地图片.IsChecked)
                {
                    List<string> imgpath = GetImgPath(@txtfile.Text);//获取图片列表
                    //定义进度条默认值
                    pb_import.Minimum = 0;
                    pb_import.Maximum = imgpath.Count();
                    double i = 0;

                    txtOCRed.Text = "";
                    OCRtxt.Text += "******************************************************************************";
                    foreach (object img in imgpath)
                    {
                        this.fnOCR(img.ToString());//遍历处理图片orc
                        FileInfo file = new FileInfo(img.ToString());
                        string name = file.Name.Substring(0, file.Name.LastIndexOf("."));
                        this.__OutputFileName = dir.FullName + @"\" + name + ".txt";
                        FileStream fs = new FileStream(this.__OutputFileName, FileMode.Create, FileAccess.ReadWrite);
                        StreamWriter sw = new StreamWriter(fs);
                        sw.Write(this.txtOCRed.Text);//将this.txtOCRed.Text内容写进txt文件中
                        sw.Flush();
                        sw.Close();
                        fs.Close();
                        OCRtxt.Text += "成功ORC出:  【" + name + ".txt】" + "\r\n";

                        //回调UI进度显示
                        i++;
                        pb_import.Value = i;
                        UpdateProgressBarDelegate updatePbDelegate = new UpdateProgressBarDelegate(pb_import.SetValue);
                        Dispatcher.Invoke(updatePbDelegate, System.Windows.Threading.DispatcherPriority.Background, new object[] { System.Windows.Controls.ProgressBar.ValueProperty, Convert.ToDouble(i + 1) });
                        Thread.Sleep(1000);
                    }
                }
                if (pb_import.Value == pb_import.Maximum)
                {

                    System.Windows.Forms.MessageBox.Show("数据转换完成。");
                }
                else
                {
                    System.Windows.Forms.MessageBox.Show("数据转换失败。");
                }
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show("数据转换失败:" + ex.Message + ex.StackTrace);
                this.labMsg.Content = "OCR失败。";
            }
            finally {
                //System.Threading.Thread.CurrentThread.Abort();
                Thread.Sleep(3000);
            }
        }
        #endregion

        #region//窗体关闭
        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            DirectoryInfo dir = new DirectoryInfo(System.Configuration.ConfigurationManager.AppSettings["tmpPath"]);
            foreach (FileInfo file in dir.GetFiles())
            {
                file.Delete();
            }
        }
        #endregion

        #endregion


    }
}
View Code

 

功能以及操作描述:

  1. 运行本页面如上图,然后通过选择待处理目录(含中文文字图片),和输出目录,点击ORC即可。
  2. 在待处理目录下,点击浏览按钮,然后选择相应的文件夹,后台通过判断文件夹是否存在,如果存在进行遍历,读取出所有图片,然后在本页左端文本框对图片进行显示。
  3. 然后选择输出目录中的浏览按钮,选择处理后的结果存放文件夹,此项必填。否则不执行。在每次操作浏览按钮时,会对选择的文件夹进行清空处理,这样下次操作保持干净数据。
  4. 完成以上步骤,可以进行ORC操作,操作过程中通过遍历文件夹中图片文件,在一个方面中打开OneNote接口,对图片进行处理。图片处理生成的是一个xml文件,此文件不可视,一个缓存的xml文件,随着程序结束而终止,在处理过程中通过代码解析缓存xml,将文本数据提取到一个txt文件中。同时没执行一张图片,如图左边打印成功执行的文件,同时进度条滚动,结束后进行提示。
  5. 最后打开对应文件夹里面显示生产数据与对应图片保持一致。规范字体识别率还是可以的

 


所遇问题以及瓶颈突破:

  1. 在描述难点之前,我还是先提取出核心ORC代码,下面再做以概述。遍历处理图片

 

   
  #region
                    foreach (object img in imgpath)
                    {
                        this.fnOCR(img.ToString());//遍历处理图片orc
                        FileInfo file = new FileInfo(img.ToString());
                        string name = file.Name.Substring(0, file.Name.LastIndexOf("."));
                        this.__OutputFileName = dir.FullName + @"\" + name + ".txt";
                        FileStream fs = new FileStream(this.__OutputFileName, FileMode.Create, FileAccess.ReadWrite);
                        StreamWriter sw = new StreamWriter(fs);
                        sw.Write(this.txtOCRed.Text);//将this.txtOCRed.Text内容写进txt文件中
                        sw.Flush();
                        sw.Close();
                        fs.Close();
                        OCRtxt.Text += "成功ORC出:  【" + name + ".txt】" + "\r\n";

                        //回调UI进度显示
                        i++;
                        pb_import.Value = i;
                        UpdateProgressBarDelegate updatePbDelegate = new UpdateProgressBarDelegate(pb_import.SetValue);
                        Dispatcher.Invoke(updatePbDelegate, System.Windows.Threading.DispatcherPriority.Background, new object[] { System.Windows.Controls.ProgressBar.ValueProperty, Convert.ToDouble(i + 1) });
                        Thread.Sleep(1000);
                    }
#endregion
View Code

 

  • fnOCR图片处理核心代码:下面截取核心代码,详细见付码
    public void ORC(string v_strImgPath)
            {
                FileInfo file = new FileInfo(v_strImgPath);
                    //向Onenote2010中插入图片
                    var onenoteApp = new Microsoft.Office.Interop.OneNote.Application();  //onenote提供的API
                    /***************************************************************************************/
                    string sectionID;
                    onenoteApp.OpenHierarchy(@"C:\Users\Administrator\Documents\OneNote 笔记本\个人\newfile.one", null, out sectionID, Microsoft.Office.Interop.OneNote.CreateFileType.cftSection);
                    string pageID = "{A975EE72-19C3-4C80-9C0E-EDA576DAB5C6}{1}{B0}";
                    onenoteApp.CreateNewPage(sectionID, out pageID, Microsoft.Office.Interop.OneNote.NewPageStyle.npsBlankPageNoTitle);
                    /********************************************************************************/
                    string notebookXml;
                    onenoteApp.GetHierarchy(null, Microsoft.Office.Interop.OneNote.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;
            
                        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", ImgExtension), 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", bp.Width.ToString()), new XAttribute("height", bp.Height.ToString())),
                                                              new XElement(ns + "Data", _Base64)))))));
                        page.Root.SetAttributeValue("ID", existingPageId);
    
                        //保存图片进入Onenote页面
                        //注意以下几点:(待解决)
                        //1,onenote页面自动会生成一个页面,可能造成一直ocr错误。删除默认页面,新建空页
                        //2,图片存储在新建页面中,通过如下代码实现的,以追加存储方式进行。
                        onenoteApp.UpdatePageContent(page.ToString(), DateTime.MinValue);
    
                        //线程休眠时间,单位毫秒,若图片很大,则延长休眠时间,保证Onenote OCR完毕
                        System.Threading.Thread.Sleep(Int32.Parse(System.Configuration.ConfigurationManager.AppSettings["WaitTIme"]));
    
                        string pageXml;
                        onenoteApp.GetPageContent(existingPageId, out pageXml, Microsoft.Office.Interop.OneNote.PageInfo.piBinaryData);
                        //获取OCR后的内容
                        FileStream tmpXml = new FileStream(System.Configuration.ConfigurationManager.AppSettings["tmpPath"] + @"\tmp.xml", FileMode.Create, FileAccess.ReadWrite);
                        StreamWriter sw = new StreamWriter(tmpXml);
                        sw.Write(pageXml);
                        sw.Flush();
                        sw.Close();
                        tmpXml.Close();
                        //加载xml中的数据
                        FileStream tmpOnenote = new FileStream(System.Configuration.ConfigurationManager.AppSettings["tmpPath"] + @"\tmp.xml", FileMode.Open, FileAccess.ReadWrite);
                        XmlReader reader = XmlReader.Create(tmpOnenote);
                        XElement rdlc = XElement.Load(reader);
                        /*****************************************************************************************************/
                        XmlNameTable nameTable = reader.NameTable;
                        XmlNamespaceManager mgr = new XmlNamespaceManager(nameTable);
                        mgr.AddNamespace("one", ns.ToString());
    
                        StringReader sr = new StringReader(pageXml);
                        XElement onenote = XElement.Load(sr);
                        ////读取xml中数据,并保存到文本中。
                        //XDocument doc1 = XDocument.Load(xpath);
                        var xml = from o in onenote.XPathSelectElements("//one:Image", mgr)
                                  select o.XPathSelectElement("//one:OCRText", mgr).Value;
                        this.txtOCRed.Text = xml.First().ToString();
                        /**********************************************************************/
                        sr.Close();
                        reader.Close();
                        tmpOnenote.Close();
                        onenoteApp.DeleteHierarchy(existingPageId);
                    }
                }
            }
    View Code

     

问题继续描述,未加入如上代码时,暂且不说批处理,即便是单处理。结果是无论加载多少张图片,可以生成对应txt文件,但是对应的所有txt文件里面文本内容始终一样。解决方案分析下:

  1. 是文本生成问题,于是想到每次图片生成清理出前一个xml,但是无论如何找不到xml存在,后来断点调试发现,执行onenoteApp.UpdatePageContent(page.ToString(), DateTime.MinValue);之后,打开Onenote页面会发现图片出来,说明onenote已经在处理,此刻xml已经生成。接下来 onenoteApp.GetPageContent(existingPageId, out pageXml, Microsoft.Office.Interop.OneNote.PageInfo.piBinaryData);方法把不可见的xml以字符串形式pageXml显示处理,后面就是对其处理了。证明从xml着手是不能解决问题的。
  2. 接下来就是想另一个方案:采用接口提供的DeletePageContent方法,但是面临问题在于其需要两个参数(页面序列id,和图片id),对于页面序列id通过断点可以找到,但是图片是随机的,id无法控制。因此此方案失败。

      3. 在读取xml中发现this.txtOCRed.Text = xml.First().ToString();那么让去每次读取最后一条数据不就ok?对,,肯定这样,抱着坚定信心解决半日不得解。断点再经调试发现,读取的xml是不可控的,即是缓存中xml以输出参数,输出的字符串的处理。最终还不得解决。为此困惑数日。

     4. 那么在生成的缓存xml重新放置到新建的xml中,然后每次读取一条结束,删除本次记录。问题肯定解决的,大喜,于是做出如下构造:

#region//删除xml数据
        public void DeleteXML(string xpath)
        {
            var onenoteApp = new Microsoft.Office.Interop.OneNote.Application();  //onenote提供的API
            string notebookXml;
            onenoteApp.GetHierarchy(null, Microsoft.Office.Interop.OneNote.HierarchyScope.hsPages, out notebookXml);
            var docc = XDocument.Parse(notebookXml);
            var ns = docc.Root.Name.Namespace;
            XDocument doc = XDocument.Load(xpath);
            string author = "USER-";

            XElement xe = (from db in
                               doc.Element(ns + "Page").Elements(ns + "Outline")
                           where db.Attribute("author").Value == author
                           select db).Single() as XElement;
            try
            {
                xe.Remove();
                doc.Save(xpath);
            }
            catch
            {
            }
        }
        #region  OCR写入Onenote和生成文字整个过程
        //string xpath = @"C:\Users\Administrator\Desktop\OCR_Onenote\OnenoteOCR2.1\tmp.xml";
        private void fnOCR(string v_strImgPath)
        {
      
            ORC(v_strImgPath);
        }
        public void ORC(string v_strImgPath)
        {
            FileInfo file = new FileInfo(v_strImgPath);
            #region  //获取图片的Base64编码
            using (MemoryStream ms = new MemoryStream())
            {
                Bitmap bp = new Bitmap(v_strImgPath);
                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:
                        this.labMsg.Content = "不支持的图片格式。";
                        return;
                }
                byte[] buffer = ms.GetBuffer();
                string _Base64 = Convert.ToBase64String(buffer);
            #endregion
                //向Onenote2010中插入图片
                var onenoteApp = new Microsoft.Office.Interop.OneNote.Application();  //onenote提供的API
                /***************************************************************************************/
                string sectionID;
                onenoteApp.OpenHierarchy(@"C:\Users\Administrator\Documents\OneNote 笔记本\个人\newfile.one", null, out sectionID, Microsoft.Office.Interop.OneNote.CreateFileType.cftSection);
                string pageID = "{A975EE72-19C3-4C80-9C0E-EDA576DAB5C6}{1}{B0}";
                onenoteApp.CreateNewPage(sectionID, out pageID, Microsoft.Office.Interop.OneNote.NewPageStyle.npsBlankPageNoTitle);
                /********************************************************************************/
                string notebookXml;
                onenoteApp.GetHierarchy(null, Microsoft.Office.Interop.OneNote.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;
                #region
                if (pageNode != null)
                {
                    //Image Type 只支持这些类型:auto|png|emf|jpg
                    string ImgExtension = file.Extension.ToLower().Substring(1);
                    switch (ImgExtension)
                    {
                        case "jpg":
                            ImgExtension = "jpg";
                            break;
                        case "png":
                            ImgExtension = "png";
                            break;
                        case "emf":
                            ImgExtension = "emf";
                            break;
                        default:
                            ImgExtension = "auto";
                            break;
                    }

                #endregion
                    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", ImgExtension), 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", bp.Width.ToString()), new XAttribute("height", bp.Height.ToString())),
                                                          new XElement(ns + "Data", _Base64)))))));
                    page.Root.SetAttributeValue("ID", existingPageId);


                    //保存图片进入Onenote页面
                    //注意以下几点:(待解决)
                    //1,onenote页面自动会生成一个页面,可能造成一直ocr错误。删除默认页面,新建空页
                    //2,图片存储在新建页面中,通过如下代码实现的,以追加存储方式进行。
                    onenoteApp.UpdatePageContent(page.ToString(), DateTime.MinValue);

                    //线程休眠时间,单位毫秒,若图片很大,则延长休眠时间,保证Onenote OCR完毕
                    System.Threading.Thread.Sleep(Int32.Parse(System.Configuration.ConfigurationManager.AppSettings["WaitTIme"]));

                    string pageXml;
                    onenoteApp.GetPageContent(existingPageId, out pageXml, Microsoft.Office.Interop.OneNote.PageInfo.piBinaryData);
                    //获取OCR后的内容
                    FileStream tmpXml = new FileStream(System.Configuration.ConfigurationManager.AppSettings["tmpPath"] + @"\tmp.xml", FileMode.Create, FileAccess.ReadWrite);
                    StreamWriter sw = new StreamWriter(tmpXml);
                    sw.Write(pageXml);
                    sw.Flush();
                    sw.Close();
                    tmpXml.Close();
                    //加载xml中的数据
                    FileStream tmpOnenote = new FileStream(System.Configuration.ConfigurationManager.AppSettings["tmpPath"] + @"\tmp.xml", FileMode.Open, FileAccess.ReadWrite);
                    XmlReader reader = XmlReader.Create(tmpOnenote);
                    XElement rdlc = XElement.Load(reader);

                    // rdlc.Save(xpath);



                    /*****************************************************************************************************/
                    XmlNameTable nameTable = reader.NameTable;
                    XmlNamespaceManager mgr = new XmlNamespaceManager(nameTable);
                    mgr.AddNamespace("one", ns.ToString());

                    StringReader sr = new StringReader(pageXml);
                    XElement onenote = XElement.Load(sr);
                    ////读取xml中数据,并保存到文本中。
                    //XDocument doc1 = XDocument.Load(xpath);
                    var xml = from o in onenote.XPathSelectElements("//one:Image", mgr)
                              select o.XPathSelectElement("//one:OCRText", mgr).Value;
                    this.txtOCRed.Text = xml.First().ToString();

                    /**********************************************************************/
                    sr.Close();
                    reader.Close();
                    tmpOnenote.Close();
                    //DeleteXML(xpath);
                    onenoteApp.DeleteHierarchy(existingPageId);
                    //onenoteApp.DeletePageContent(existingPageId, "{242BE490-7BF9-43D5-B719-3999E7631259}{46}{B0}");
                    //onenoteApp.DeletePageContent(existingPageId, "{242BE490-7BF9-43D5-B719-3999E7631259}{50}{B0}");
                }
            }
        }
        #endregion
        #endregion
View Code

  结果还是无济于事,关键还是缓存的xml无法清理。

解决方案:搞定凌晨,无意间打开几篇英文文章(文章看不懂,只看代码),发现其原理在于图片处理过程中,催毁原始页面,新建页面即可。

                var onenoteApp = new Microsoft.Office.Interop.OneNote.Application();  //onenote提供的API
                /***************************************************************************************/
                string sectionID;
                onenoteApp.OpenHierarchy(@"C:\Users\Administrator\Documents\OneNote 笔记本\个人\newfile.one", null, out sectionID, Microsoft.Office.Interop.OneNote.CreateFileType.cftSection);
                string pageID = "{A975EE72-19C3-4C80-9C0E-EDA576DAB5C6}{1}{B0}";
                onenoteApp.CreateNewPage(sectionID, out pageID, Microsoft.Office.Interop.OneNote.NewPageStyle.npsBlankPageNoTitle);
                /********************************************************************************/             

 调用API中创建页面方法CreateNewPage,pageID通过断点可以调试到,sectionID找到本地的地址即可。然后在执行一条数据结束时,调用onenoteApp.DeleteHierarchy(existingPageId)即可。


止于此核心问题解决,下面总结下所用的文件相关操作:

1 、验证文件夹是否存在,以及是否为空 
  

#region 验证文件夹是否存在,以及是否为空 :2014-7-10 17:12:57
        //验证文件夹是否存在,以及是否为空
        //时间:2014-7-10 14:54:44
        //作者:白宁超
        private bool fn数据验证()
        {
            if ((bool)this.rbtn本地图片.IsChecked)
            {
                if (!Directory.Exists(this.txtfile.Text))
                {
                    this.labMsg.Content = "目录不存在,重新选择!";
                    this.txtfile.Focus();
                    return true;
                }
                System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(@txtfile.Text);
                if (di.GetFiles().Length + di.GetDirectories().Length == 0)
                {
                    this.labMsg.Content = "目录为空!";
                    this.txtfile.Focus();
                    return true;
                }
            }
            if (!Directory.Exists(this.txt输出目录.Text))
            {
                this.labMsg.Content = "输出目录不存在,请重新选择。";
                this.txt输出目录.Focus();
                return true;
            }

            return false;
        }
        #endregion
View Code

 

2、遍历出文件中图片路径

#region   遍历出文件中图片路径
        //文件中选择所有文件处理
        //时间:2014-7-10 17:17:29
        //作者:白宁超
        public List<string> GetImgPath(string Filepath)
        {
            DirectoryInfo d = new DirectoryInfo(Filepath);
            ArrayList Flst = GetAll(d);              //存放图片完整路径

            List<string> imgpath = new List<string>();
            foreach (object o in Flst)
            {
                imgpath.Add(@txtfile.Text + "\\" + o.ToString());
            }
            return imgpath;//返回图片路径集合
        }
        #endregion
View Code

 

3、获取图片的过程

 

#region  获取图片的过程
        private void fnStartDownload(string v_strImgPath, string v_strOutputDir, out string v_strTmpPath)
        {
            int n = v_strImgPath.LastIndexOf('/');
            string URLAddress = v_strImgPath.Substring(0, n);
            string fileName = v_strImgPath.Substring(n + 1, v_strImgPath.Length - n - 1);
            this.__OutputFileName = v_strOutputDir + "\\" + fileName.Substring(0, fileName.LastIndexOf(".")) + ".txt";

            if (!Directory.Exists(System.Configuration.ConfigurationManager.AppSettings["tmpPath"]))
            {
                Directory.CreateDirectory(System.Configuration.ConfigurationManager.AppSettings["tmpPath"]);
            }

            string Dir = System.Configuration.ConfigurationManager.AppSettings["tmpPath"];
            v_strTmpPath = Dir + "\\" + fileName;
            this.client.DownloadFile(v_strImgPath, v_strTmpPath);
        }
        #endregion
View Code

 

4、删除指定目录下的所有文件夹以及文件

 

#region//删除指定目录下的所有文件夹以及文件
        private void DelFillOrDir(string strPath)
        {

            if (Directory.GetDirectories(strPath).Length > 0 || Directory.GetFiles(strPath).Length > 0)
            {
                // 获得文件夹数组 
                string[] strDirs = System.IO.Directory.GetDirectories(strPath); // 获得文件数组 
                string[] strFiles = System.IO.Directory.GetFiles(strPath); // 遍历所有子文件夹 
                foreach (string strFile in strFiles)
                {
                    // 删除文件夹 
                    System.IO.File.Delete(strFile);
                } // 遍历所有文件 
                foreach (string strdir in strDirs)
                { // 删除文件 
                    System.IO.Directory.Delete(strdir, true);
                }
            } // 成功 
        }
        #endregion
View Code

 

5、删除文件

 

#region//删除文件
        public static bool DeleteFile(string xmlPath)
        {
            FileInfo newFile = new FileInfo(xmlPath);

            if (newFile.Exists == true)
            {
                newFile.Delete();
                return true;
            }
            else
            {
                return false;
            }
        }
        #endregion
View Code

 

6、删除xml数据

 

#region//删除xml数据
        public void DeleteXML(string xpath)
        {
            var onenoteApp = new Microsoft.Office.Interop.OneNote.Application();  //onenote提供的API
            string notebookXml;
            onenoteApp.GetHierarchy(null, Microsoft.Office.Interop.OneNote.HierarchyScope.hsPages, out notebookXml);
            var docc = XDocument.Parse(notebookXml);
            var ns = docc.Root.Name.Namespace;
            XDocument doc = XDocument.Load(xpath);
            string author = "USER-";

            XElement xe = (from db in
                               doc.Element(ns + "Page").Elements(ns + "Outline")
                           where db.Attribute("author").Value == author
                           select db).Single() as XElement;
            try
            {
                xe.Remove();
                doc.Save(xpath);
            }
            catch
            {
            }
        }
        #region  OCR写入Onenote和生成文字整个过程
        //string xpath = @"C:\Users\Administrator\Desktop\OCR_Onenote\OnenoteOCR2.1\tmp.xml";
        private void fnOCR(string v_strImgPath)
        {
      
            ORC(v_strImgPath);
        }
        public void ORC(string v_strImgPath)
        {
            FileInfo file = new FileInfo(v_strImgPath);
            #region  //获取图片的Base64编码
            using (MemoryStream ms = new MemoryStream())
            {
                Bitmap bp = new Bitmap(v_strImgPath);
                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:
                        this.labMsg.Content = "不支持的图片格式。";
                        return;
                }
                byte[] buffer = ms.GetBuffer();
                string _Base64 = Convert.ToBase64String(buffer);
            #endregion
                //向Onenote2010中插入图片
                var onenoteApp = new Microsoft.Office.Interop.OneNote.Application();  //onenote提供的API
                /***************************************************************************************/
                string sectionID;
                onenoteApp.OpenHierarchy(@"C:\Users\Administrator\Documents\OneNote 笔记本\个人\newfile.one", null, out sectionID, Microsoft.Office.Interop.OneNote.CreateFileType.cftSection);
                string pageID = "{A975EE72-19C3-4C80-9C0E-EDA576DAB5C6}{1}{B0}";
                onenoteApp.CreateNewPage(sectionID, out pageID, Microsoft.Office.Interop.OneNote.NewPageStyle.npsBlankPageNoTitle);
                /********************************************************************************/
                string notebookXml;
                onenoteApp.GetHierarchy(null, Microsoft.Office.Interop.OneNote.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;
                #region
                if (pageNode != null)
                {
                    //Image Type 只支持这些类型:auto|png|emf|jpg
                    string ImgExtension = file.Extension.ToLower().Substring(1);
                    switch (ImgExtension)
                    {
                        case "jpg":
                            ImgExtension = "jpg";
                            break;
                        case "png":
                            ImgExtension = "png";
                            break;
                        case "emf":
                            ImgExtension = "emf";
                            break;
                        default:
                            ImgExtension = "auto";
                            break;
                    }

                #endregion
                    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", ImgExtension), 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", bp.Width.ToString()), new XAttribute("height", bp.Height.ToString())),
                                                          new XElement(ns + "Data", _Base64)))))));
                    page.Root.SetAttributeValue("ID", existingPageId);


                    //保存图片进入Onenote页面
                    //注意以下几点:(待解决)
                    //1,onenote页面自动会生成一个页面,可能造成一直ocr错误。删除默认页面,新建空页
                    //2,图片存储在新建页面中,通过如下代码实现的,以追加存储方式进行。
                    onenoteApp.UpdatePageContent(page.ToString(), DateTime.MinValue);

                    //线程休眠时间,单位毫秒,若图片很大,则延长休眠时间,保证Onenote OCR完毕
                    System.Threading.Thread.Sleep(Int32.Parse(System.Configuration.ConfigurationManager.AppSettings["WaitTIme"]));

                    string pageXml;
                    onenoteApp.GetPageContent(existingPageId, out pageXml, Microsoft.Office.Interop.OneNote.PageInfo.piBinaryData);
                    //获取OCR后的内容
                    FileStream tmpXml = new FileStream(System.Configuration.ConfigurationManager.AppSettings["tmpPath"] + @"\tmp.xml", FileMode.Create, FileAccess.ReadWrite);
                    StreamWriter sw = new StreamWriter(tmpXml);
                    sw.Write(pageXml);
                    sw.Flush();
                    sw.Close();
                    tmpXml.Close();
                    //加载xml中的数据
                    FileStream tmpOnenote = new FileStream(System.Configuration.ConfigurationManager.AppSettings["tmpPath"] + @"\tmp.xml", FileMode.Open, FileAccess.ReadWrite);
                    XmlReader reader = XmlReader.Create(tmpOnenote);
                    XElement rdlc = XElement.Load(reader);

                    // rdlc.Save(xpath);



                    /*****************************************************************************************************/
                    XmlNameTable nameTable = reader.NameTable;
                    XmlNamespaceManager mgr = new XmlNamespaceManager(nameTable);
                    mgr.AddNamespace("one", ns.ToString());

                    StringReader sr = new StringReader(pageXml);
                    XElement onenote = XElement.Load(sr);
                    ////读取xml中数据,并保存到文本中。
                    //XDocument doc1 = XDocument.Load(xpath);
                    var xml = from o in onenote.XPathSelectElements("//one:Image", mgr)
                              select o.XPathSelectElement("//one:OCRText", mgr).Value;
                    this.txtOCRed.Text = xml.First().ToString();

                    /**********************************************************************/
                    sr.Close();
                    reader.Close();
                    tmpOnenote.Close();
                    //DeleteXML(xpath);
                    onenoteApp.DeleteHierarchy(existingPageId);
                    //onenoteApp.DeletePageContent(existingPageId, "{242BE490-7BF9-43D5-B719-3999E7631259}{46}{B0}");
                    //onenoteApp.DeletePageContent(existingPageId, "{242BE490-7BF9-43D5-B719-3999E7631259}{50}{B0}");
                }
            }
        }
        #endregion
        #endregion
View Code

 

7、搜索文件夹中的文件

 

#region 搜索文件夹中的文件:2014-7-10 17:12:57
        //搜索文件夹中的文件
        //时间:2014-7-10 14:54:44
        //作者:白宁超
        ArrayList GetAll(DirectoryInfo dir)
        {
            try
            {
                ArrayList FileList = new ArrayList();//定义数组存放图片文件名
                FileInfo[] allFile = dir.GetFiles();
                foreach (FileInfo fi in allFile)
                {
                    FileList.Add(fi.Name);//遍历图片,并添加到数组
                }

                DirectoryInfo[] allDir = dir.GetDirectories();
                foreach (DirectoryInfo d in allDir)
                {
                    GetAll(d);
                }
                return FileList;
            }
            catch (Exception e)
            {
                labMsg.Content = e.Message + "文件夹选择方式错误,重新选择!!";
                return null;
            }
        }
        #endregion
View Code

 

8、选择遍历文件夹图片

 

 private void btn浏览_Click(object sender, RoutedEventArgs e)
        {
            txtmulu.Text = null;
            FolderBrowserDialog dialog = new FolderBrowserDialog();
            dialog.Description = "请选择待处理目录";
            if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                string foldPath = dialog.SelectedPath;
                txtfile.Text = foldPath;
                DirectoryInfo d = new DirectoryInfo(@txtfile.Text);
                ArrayList Flst = GetAll(d);
                foreach (object o in Flst)
                {
                    txtmulu.Text += "正在ORC图片: 【" + o.ToString() + "】\r\n";
                }
            }
            else
            {
                labMsg.Content = "打开失败!!";
            }
            #region 上传单个图片  2014-7-10 15:34:17
            /*OpenFileDialog ofd = new OpenFileDialog();
            ofd.Title = "请选择一个本地图片";
            ofd.Multiselect = false;
            ofd.Filter = "支持的图片格式(*.jpg,*.jpeg,*.gif,*.bmp,*.png,*.tiff,*.emf)|*.jpg;*.jpeg;*.gif;*.bmp;*.png;*.tiff;*.emf";

            if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                this.txt本地图片.Text = ofd.FileName;
                this.img图片.Source = new BitmapImage(new Uri(ofd.FileName, UriKind.RelativeOrAbsolute));

                this.labMsg.Content = "本地图片已成功加载。";
            }*/
            #endregion
        }
View Code

 

9、窗体关闭

 

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            DirectoryInfo dir = new DirectoryInfo(System.Configuration.ConfigurationManager.AppSettings["tmpPath"]);
            foreach (FileInfo file in dir.GetFiles())
            {
                file.Delete();
            }
        }
View Code

【补充】orc前后结果对照:应部分园友建议,更为直观

  • 整体orc结果:

  • 具体orc结果:


 

【篇末】:程序中使用遍历文件,文件夹清空处理,文件的删除,文件的查找,文件夹的判空,重点在于图片的OneNote处理等操作,完成了批处理。可以自动执行批文件夹中图片,生成新的文本文件与图片名一致。现在不足之处,对文件夹的文件类型筛选为执行图片类型没有处理,容错不足,这个自己用文件放入纯图片可以,另外有时候进程处理不稳定,估计加入进度条原因。待版本2再做改进。下一节介绍文本文件处理入库(Oracle)。特别wpfDataGrid的使用,如何在wpf中完成分页,简单的集合类型数据库分页可以,但是数据库连接如何分页?

 

posted @ 2014-07-24 09:57  伏草惟存  阅读(5069)  评论(34编辑  收藏  举报