ScanViwerViewModel.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. using ComDocumentAIKit;
  2. using ComPDFKit.PDFDocument;
  3. using ComPDFKit.PDFPage;
  4. using ComPDFKitViewer;
  5. using ComPDFKitViewer.PdfViewer;
  6. using PDF_Office.EventAggregators;
  7. using PDF_Office.Helper;
  8. using PDF_Office.Model;
  9. using Prism.Events;
  10. using Prism.Mvvm;
  11. using Prism.Regions;
  12. using System;
  13. using System.Collections.Generic;
  14. using System.IO;
  15. using System.Linq;
  16. using System.Text;
  17. using System.Threading.Tasks;
  18. using System.Windows;
  19. using System.Windows.Media;
  20. using System.Windows.Media.Imaging;
  21. namespace PDF_Office.ViewModels.Scan
  22. {
  23. class ScanViwerViewModel : BindableBase, INavigationAware
  24. {
  25. private CPDFViewer PDFViewer;
  26. private WriteableBitmap bgImage;
  27. public WriteableBitmap BgImage
  28. {
  29. get { return bgImage; }
  30. set
  31. {
  32. SetProperty(ref bgImage, value);
  33. }
  34. }
  35. private List<KeyValuePair<Rect, string>> textRectList;
  36. public List<KeyValuePair<Rect, string>> TextRectList
  37. {
  38. get { return textRectList; }
  39. set
  40. {
  41. SetProperty(ref textRectList, value);
  42. }
  43. }
  44. private Dictionary<int, List<KeyValuePair<Rect, string>>> cacahe;
  45. public bool IsNavigationTarget(NavigationContext navigationContext)
  46. {
  47. return true;
  48. }
  49. public void OnNavigatedFrom(NavigationContext navigationContext)
  50. {
  51. return;
  52. }
  53. public void OnNavigatedTo(NavigationContext navigationContext)
  54. {
  55. navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer);
  56. if (PDFViewer == null)
  57. {
  58. return;
  59. }
  60. else
  61. {
  62. PDFViewer.InfoChanged += PDFViewer_InfoChanged;
  63. PDFViewer.ChangeViewMode(ViewMode.Single);
  64. //第一次进入获取当前预览显示内容
  65. CPDFPage pdfPage = PDFViewer.Document.PageAtIndex(PDFViewer.CurrentIndex);
  66. float zoom = (float)(DpiHelpers.Dpi / 72D);
  67. int renderWidth = (int)(pdfPage.PageSize.Width * zoom);
  68. int renderHeight = (int)(pdfPage.PageSize.Height * zoom);
  69. byte[] renderData = new byte[renderWidth * renderHeight * 4];
  70. pdfPage.RenderPageBitmapWithMatrix(zoom, new Rect(0, 0, renderWidth, renderHeight), 0xFFFFFFFF, renderData, 0);
  71. BgImage = new WriteableBitmap(renderWidth, renderHeight, DpiHelpers.Dpi, DpiHelpers.Dpi, PixelFormats.Bgra32, null);
  72. BgImage.WritePixels(new Int32Rect(0, 0, renderWidth, renderHeight), renderData, BgImage.BackBufferStride, 0);
  73. }
  74. }
  75. private void PDFViewer_InfoChanged(object sender, KeyValuePair<string, object> e)
  76. {
  77. if (e.Key == "PageNum")
  78. {
  79. RenderData renderData = e.Value as RenderData;
  80. if (renderData != null)
  81. {
  82. CPDFPage pdfPage = PDFViewer.Document.PageAtIndex(renderData.PageIndex - 1);
  83. float zoom = (float)(DpiHelpers.Dpi / 72D);
  84. int renderWidth = (int)(pdfPage.PageSize.Width * zoom);
  85. int renderHeight = (int)(pdfPage.PageSize.Height * zoom);
  86. byte[] Data = new byte[renderWidth * renderHeight * 4];
  87. pdfPage.RenderPageBitmapWithMatrix(zoom, new Rect(0, 0, renderWidth, renderHeight), 0xFFFFFFFF, Data, 0);
  88. BgImage = new WriteableBitmap(renderWidth, renderHeight, DpiHelpers.Dpi, DpiHelpers.Dpi, PixelFormats.Bgra32, null);
  89. BgImage.WritePixels(new Int32Rect(0, 0, renderWidth, renderHeight), Data, BgImage.BackBufferStride, 0);
  90. if (cacahe.Count > 0)
  91. {
  92. if (cacahe.ContainsKey(renderData.PageIndex - 1))
  93. {
  94. TextRectList = cacahe[renderData.PageIndex - 1];
  95. }
  96. else
  97. {
  98. TextRectList = null;
  99. }
  100. }
  101. }
  102. }
  103. }
  104. public ScanViwerViewModel(IEventAggregator eventAggregator)
  105. {
  106. eventAggregator.GetEvent<ScanEvent>().Subscribe(ChangeScanMode, e => e.Unicode == App.mainWindowViewModel.SelectedItem.Unicode);
  107. cacahe = new Dictionary<int, List<KeyValuePair<Rect, string>>>();
  108. }
  109. /// <summary>
  110. /// 根据Vm事件通知处理OCR与区域识别事件
  111. /// </summary>
  112. /// <param name="e"></param>
  113. private void ChangeScanMode(ScanEventArgs e)
  114. {
  115. switch (e.Mode)
  116. {
  117. case ScanMode.Unknown:
  118. break;
  119. case ScanMode.OCR:
  120. OCRProcess(e);
  121. break;
  122. case ScanMode.Area:
  123. break;
  124. default:
  125. break;
  126. }
  127. }
  128. private void OCRProcess(ScanEventArgs args)
  129. {
  130. COCREngine imEngine = new COCREngine(App.modelFolderPath);
  131. //CIMEngine imEngine = new CIMEngine(App.modelFolderPath);
  132. string path = App.CachePath.MergeFilePath;
  133. string name = Guid.NewGuid().ToString();
  134. path = Path.Combine(path, name);
  135. string EnhancePath = Path.Combine(path, "Enhance");
  136. Directory.CreateDirectory(path);
  137. Directory.CreateDirectory(EnhancePath);
  138. CPDFDocument CurrentDoc = PDFViewer.Document;
  139. CErrorCode error = imEngine.SetModel((COCRLanguage)args.ScanLanguage);
  140. cacahe.Clear();
  141. for (int i = 0; i < args.PageRange.Count; i++)
  142. {
  143. string pageImagePath = Path.Combine(path, args.PageRange[i].ToString());
  144. string pageEnhancePath = Path.Combine(EnhancePath, args.PageRange[i].ToString() + ".png");
  145. CPDFPage pdfPage = CurrentDoc.PageAtIndex(args.PageRange[i]);
  146. float zoom = (float)(DpiHelpers.Dpi / 72D);
  147. int renderWidth = (int)(pdfPage.PageSize.Width * zoom);
  148. int renderHeight = (int)(pdfPage.PageSize.Height * zoom);
  149. byte[] renderData = new byte[renderWidth * renderHeight * 4];
  150. pdfPage.RenderPageBitmapWithMatrix(zoom, new Rect(0, 0, renderWidth, renderHeight), 0xFFFFFFFF, renderData, 0);
  151. WriteableBitmap bitmap = new WriteableBitmap(renderWidth, renderHeight, DpiHelpers.Dpi, DpiHelpers.Dpi, PixelFormats.Bgra32, null);
  152. bitmap.WritePixels(new Int32Rect(0, 0, renderWidth, renderHeight), renderData, bitmap.BackBufferStride, 0);
  153. BitmapEncoder encoder = new PngBitmapEncoder();
  154. encoder.Frames.Add(BitmapFrame.Create(bitmap));
  155. using (FileStream imageStream = File.Create(pageImagePath))
  156. {
  157. encoder.Save(imageStream);
  158. imageStream.Flush();
  159. }
  160. //File.Create(pageEnhancePath);
  161. //error = imEngine.Process(pageImagePath, pageEnhancePath);
  162. error = imEngine.Process(pageImagePath);
  163. if (imEngine.OCRResultList == null)
  164. return;
  165. List<KeyValuePair<Rect, string>> RectList = new List<KeyValuePair<Rect, string>>();
  166. foreach (COCRResult ocrResult in imEngine.OCRResultList)
  167. {
  168. List<Point> rectPoints = new List<Point>();
  169. for (int j = 0; j < ocrResult.position.Length; j += 2)
  170. {
  171. rectPoints.Add(new Point(ocrResult.position[j], ocrResult.position[j + 1]));
  172. }
  173. int left = (int)rectPoints.AsEnumerable().Min(x => x.X);
  174. int right = (int)rectPoints.AsEnumerable().Max(x => x.X);
  175. int top = (int)rectPoints.AsEnumerable().Min(x => x.Y);
  176. int bottom = (int)rectPoints.AsEnumerable().Max(x => x.Y);
  177. RectList.Add(new KeyValuePair<Rect, string>(new Rect(left, top, right - left, bottom - top), ocrResult.text));
  178. }
  179. cacahe.Add(args.PageRange[i], RectList);
  180. }
  181. if (cacahe.Count > 0)
  182. {
  183. if (cacahe.ContainsKey(PDFViewer.CurrentIndex))
  184. {
  185. TextRectList = cacahe[PDFViewer.CurrentIndex];
  186. }
  187. else
  188. {
  189. TextRectList = null;
  190. }
  191. }
  192. imEngine.Release();
  193. }
  194. }
  195. }