ScanContentViewModel.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. using ComDocumentAIKit;
  2. using ComPDFKit.PDFDocument;
  3. using ComPDFKit.PDFPage;
  4. using ComPDFKit.PDFPage.Edit;
  5. using ComPDFKitViewer.PdfViewer;
  6. using PDF_Office.EventAggregators;
  7. using PDF_Office.Helper;
  8. using PDF_Office.Model;
  9. using Prism.Commands;
  10. using Prism.Events;
  11. using Prism.Mvvm;
  12. using Prism.Regions;
  13. using System;
  14. using System.Collections.Generic;
  15. using System.IO;
  16. using System.Linq;
  17. using System.Text;
  18. using System.Threading.Tasks;
  19. using System.Windows;
  20. using System.Windows.Controls;
  21. using System.Windows.Media;
  22. using System.Windows.Media.Imaging;
  23. namespace PDF_Office.ViewModels.Tools
  24. {
  25. class ScanContentViewModel : BindableBase, INavigationAware
  26. {
  27. #region 模板与导航相关内容
  28. private CPDFViewer PDFViewer;
  29. public ViewContentViewModel viewContentViewModel;
  30. public bool IsNavigationTarget(NavigationContext navigationContext)
  31. {
  32. return true;
  33. }
  34. public void OnNavigatedFrom(NavigationContext navigationContext)
  35. {
  36. return;
  37. }
  38. public void OnNavigatedTo(NavigationContext navigationContext)
  39. {
  40. navigationContext.Parameters.TryGetValue<ViewContentViewModel>(ParameterNames.ViewContentViewModel, out viewContentViewModel);
  41. navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer);
  42. }
  43. #endregion
  44. private IRegionManager regions;
  45. public IEventAggregator events;
  46. public DelegateCommand NavigationCommand { get; set; }
  47. public DelegateCommand EnhancedCommand { get; set; }
  48. public ScanContentViewModel(IRegionManager regionManager, IEventAggregator eventAggregator)
  49. {
  50. regions = regionManager;
  51. events = eventAggregator;
  52. NavigationCommand = new DelegateCommand(BtnNavigation);
  53. EnhancedCommand = new DelegateCommand(EnhancedScan);
  54. }
  55. /// <summary>
  56. ///导航到OCR的侧边栏
  57. /// </summary>
  58. private void BtnNavigation()
  59. {
  60. //替换属性面板的内容
  61. NavigationParameters parameters = new NavigationParameters();
  62. parameters.Add(ParameterNames.PDFViewer, PDFViewer);
  63. parameters.Add(ParameterNames.PropertyPanelContentViewModel, null);
  64. regions.RequestNavigate(RegionNames.PropertyRegionName, "ScanPropertyPanel", parameters);
  65. //显示属性面板
  66. viewContentViewModel.IsPropertyOpen = true;
  67. }
  68. private void EnhancedScan()
  69. {
  70. try
  71. {
  72. List<int> SetPageRange = new List<int>();
  73. CommonHelper.GetPagesInRange(ref SetPageRange, "1-" + PDFViewer.Document.PageCount, PDFViewer.Document.PageCount, new char[] { ',' }, new char[] { '-' });
  74. events.GetEvent<ScanEvent>().Publish(new ScanEventArgs()
  75. {
  76. Unicode = App.mainWindowViewModel.SelectedItem.Unicode,
  77. PageRange = SetPageRange,
  78. Mode = ScanMode.Enhanced,
  79. ScanLanguage = ScanLanguageMode.ChineseS
  80. }
  81. );
  82. }
  83. catch (Exception e)
  84. {
  85. }
  86. }
  87. private void OCREnhancedScan()
  88. {
  89. CPDFDocument EnhanceDoc = CPDFDocument.CreateDocument();
  90. COCREngine imEngine = new COCREngine(App.modelFolderPath);
  91. //CIMEngine imEngine = new CIMEngine(App.modelFolderPath);
  92. string path = App.CachePath.MergeFilePath;
  93. string name = Guid.NewGuid().ToString();
  94. path = Path.Combine(path, name);
  95. string EnhancePath = Path.Combine(path, "Enhance");
  96. Directory.CreateDirectory(path);
  97. Directory.CreateDirectory(EnhancePath);
  98. CPDFDocument CurrentDoc = PDFViewer.Document;
  99. //CurrentDoc.FilePath;
  100. CErrorCode error = imEngine.SetModel(COCRLanguage.Chinese_S);
  101. //CErrorCode error = imEngine.SetModel();
  102. for (int i = 0; i < 1; i++)
  103. {
  104. string pageImagePath = Path.Combine(path, i.ToString());
  105. string pageEnhancePath = Path.Combine(EnhancePath, i.ToString() + ".png");
  106. try
  107. {
  108. CPDFPage pdfPage = CurrentDoc.PageAtIndex(i);
  109. float zoom = (float)(DpiHelpers.Dpi / 72D);
  110. int renderWidth = (int)(pdfPage.PageSize.Width * zoom);
  111. int renderHeight = (int)(pdfPage.PageSize.Height * zoom);
  112. byte[] renderData = new byte[renderWidth * renderHeight * 4];
  113. pdfPage.RenderPageBitmapWithMatrix(zoom, new Rect(0, 0, renderWidth, renderHeight), 0xFFFFFFFF, renderData, 0);
  114. WriteableBitmap bitmap = new WriteableBitmap(renderWidth, renderHeight, DpiHelpers.Dpi, DpiHelpers.Dpi, PixelFormats.Bgra32, null);
  115. bitmap.WritePixels(new Int32Rect(0, 0, renderWidth, renderHeight), renderData, bitmap.BackBufferStride, 0);
  116. BitmapEncoder encoder = new PngBitmapEncoder();
  117. encoder.Frames.Add(BitmapFrame.Create(bitmap));
  118. using (FileStream imageStream = File.Create(pageImagePath))
  119. {
  120. encoder.Save(imageStream);
  121. imageStream.Flush();
  122. }
  123. //File.Create(pageEnhancePath);
  124. //error = imEngine.Process(pageImagePath, pageEnhancePath);
  125. error = imEngine.Process(pageImagePath);
  126. if (imEngine.OCRResultList == null)
  127. return;
  128. List<KeyValuePair<Rect, string>> textRectList = new List<KeyValuePair<Rect, string>>();
  129. foreach (COCRResult ocrResult in imEngine.OCRResultList)
  130. {
  131. List<Point> rectPoints = new List<Point>();
  132. for (int j = 0; j < ocrResult.position.Length; j += 2)
  133. {
  134. rectPoints.Add(new Point(ocrResult.position[j], ocrResult.position[j + 1]));
  135. }
  136. int left = (int)rectPoints.AsEnumerable().Min(x => x.X);
  137. int right = (int)rectPoints.AsEnumerable().Max(x => x.X);
  138. int top = (int)rectPoints.AsEnumerable().Min(x => x.Y);
  139. int bottom = (int)rectPoints.AsEnumerable().Max(x => x.Y);
  140. textRectList.Add(new KeyValuePair<Rect, string>(new Rect(left, top, right - left, bottom - top), ocrResult.text));
  141. }
  142. //写入数据
  143. CPDFEditPage editPage = pdfPage.GetEditPage();
  144. editPage.BeginEdit(CPDFEditType.EditText);
  145. for (int y = textRectList.Count - 1; y >= 0; y--)
  146. {
  147. KeyValuePair<Rect, string> textBlock = textRectList[y];
  148. int fontSize = 1;
  149. Rect pdfRect = DpiHelpers.GetRawRect(textBlock.Key);
  150. for (; fontSize < 100; fontSize++)
  151. {
  152. Rect cmpRect = pdfPage.GetTextContentRect(fontSize, "Helvetica", textBlock.Value);
  153. if (cmpRect.Width > pdfRect.Width || cmpRect.Height > pdfRect.Height)
  154. {
  155. fontSize -= 1;
  156. break;
  157. }
  158. }
  159. CPDFEditTextArea editArea = editPage.CreateNewTextArea(pdfRect, "Helvetica", fontSize, new byte[] { 0, 0, 0 });
  160. bool x = editArea.SetTextAreaAlign(TextAlignType.AlignChar);
  161. editArea.InsertText(textBlock.Value);
  162. }
  163. editPage.EndEdit();
  164. }
  165. catch (Exception e)
  166. {
  167. throw;
  168. }
  169. }
  170. imEngine.Release();
  171. return;
  172. }
  173. }
  174. }