ScanContentViewModel.cs 9.7 KB

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