ScanContentViewModel.cs 8.4 KB

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