ScanViwerViewModel.cs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682
  1. using ComDocumentAIKit;
  2. using ComPDFKit.PDFDocument;
  3. using ComPDFKit.PDFPage;
  4. using ComPDFKit.PDFPage.Edit;
  5. using ComPDFKitViewer;
  6. using ComPDFKitViewer.PdfViewer;
  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.Media;
  22. using System.Windows.Media.Imaging;
  23. using System.Windows.Threading;
  24. namespace PDF_Master.ViewModels.Scan
  25. {
  26. class ScanViwerViewModel : BindableBase, INavigationAware
  27. {
  28. private CPDFViewer PDFViewer;
  29. private ViewContentViewModel viewContentViewModel;
  30. private WriteableBitmap bgImage;
  31. public WriteableBitmap BgImage
  32. {
  33. get { return bgImage; }
  34. set
  35. {
  36. SetProperty(ref bgImage, value);
  37. }
  38. }
  39. private List<KeyValuePair<Rect, string>> textRectList;
  40. public List<KeyValuePair<Rect, string>> TextRectList
  41. {
  42. get { return textRectList; }
  43. set
  44. {
  45. SetProperty(ref textRectList, value);
  46. }
  47. }
  48. private bool isEnhanced;
  49. /// <summary>
  50. /// 当前是否为增强扫描状态
  51. /// </summary>
  52. public bool IsEnhanced
  53. {
  54. get { return isEnhanced; }
  55. set
  56. {
  57. SetProperty(ref isEnhanced, value);
  58. }
  59. }
  60. private bool isShowRect;
  61. public bool IsShowRect
  62. {
  63. get { return isShowRect; }
  64. set
  65. {
  66. SetProperty(ref isShowRect, value);
  67. }
  68. }
  69. private COCRLanguage language = COCRLanguage.COCRLanguageEnglish;
  70. /// <summary>
  71. /// 增强扫描结果路径
  72. /// </summary>
  73. public List<string> EnhancedFilePathList;
  74. /// <summary>
  75. /// OCR结果路径
  76. /// </summary>
  77. private Dictionary<int, List<KeyValuePair<Rect, string>>> cacahe;
  78. private Visibility showTooltip = Visibility.Collapsed;
  79. public Visibility ShowTooltip
  80. {
  81. get { return showTooltip; }
  82. set
  83. {
  84. SetProperty(ref showTooltip, value);
  85. }
  86. }
  87. public DelegateCommand CloseTooltipCommand { get; set; }
  88. public DelegateCommand CancelEnhancedCommand { get; set; }
  89. public bool IsNavigationTarget(NavigationContext navigationContext)
  90. {
  91. return true;
  92. }
  93. public void OnNavigatedFrom(NavigationContext navigationContext)
  94. {
  95. return;
  96. }
  97. public void OnNavigatedTo(NavigationContext navigationContext)
  98. {
  99. navigationContext.Parameters.TryGetValue<ViewContentViewModel>(ParameterNames.ViewContentViewModel, out viewContentViewModel);
  100. if (viewContentViewModel == null)
  101. {
  102. return;
  103. }
  104. navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer);
  105. if (PDFViewer == null)
  106. {
  107. return;
  108. }
  109. else
  110. {
  111. PDFViewer.InfoChanged += PDFViewer_InfoChanged;
  112. PDFViewer.ChangeViewMode(ViewMode.Single);
  113. //第一次进入获取当前预览显示内容
  114. CPDFPage pdfPage = PDFViewer.Document.PageAtIndex(PDFViewer.CurrentIndex);
  115. float zoom = (float)(DpiHelpers.Dpi / 72D);
  116. int renderWidth = (int)(pdfPage.PageSize.Width * zoom);
  117. int renderHeight = (int)(pdfPage.PageSize.Height * zoom);
  118. byte[] renderData = new byte[renderWidth * renderHeight * 4];
  119. pdfPage.RenderPageBitmapWithMatrix(zoom, new Rect(0, 0, renderWidth, renderHeight), 0xFFFFFFFF, renderData, 0);
  120. BgImage = new WriteableBitmap(renderWidth, renderHeight, DpiHelpers.Dpi, DpiHelpers.Dpi, PixelFormats.Bgra32, null);
  121. BgImage.WritePixels(new Int32Rect(0, 0, renderWidth, renderHeight), renderData, BgImage.BackBufferStride, 0);
  122. }
  123. }
  124. private void PDFViewer_InfoChanged(object sender, KeyValuePair<string, object> e)
  125. {
  126. if (e.Key == "PageNum")
  127. {
  128. RenderData renderData = e.Value as RenderData;
  129. if (renderData != null)
  130. {
  131. if (IsEnhanced)
  132. {
  133. BitmapImage image = new BitmapImage(new Uri(EnhancedFilePathList[PDFViewer.CurrentIndex]));
  134. BgImage = new WriteableBitmap(image);
  135. }
  136. else
  137. {
  138. CPDFPage pdfPage = PDFViewer.Document.PageAtIndex(renderData.PageIndex - 1);
  139. float zoom = (float)(DpiHelpers.Dpi / 72D);
  140. int renderWidth = (int)(pdfPage.PageSize.Width * zoom);
  141. int renderHeight = (int)(pdfPage.PageSize.Height * zoom);
  142. byte[] Data = new byte[renderWidth * renderHeight * 4];
  143. pdfPage.RenderPageBitmapWithMatrix(zoom, new Rect(0, 0, renderWidth, renderHeight), 0xFFFFFFFF, Data, 0);
  144. BgImage = new WriteableBitmap(renderWidth, renderHeight, DpiHelpers.Dpi, DpiHelpers.Dpi, PixelFormats.Bgra32, null);
  145. BgImage.WritePixels(new Int32Rect(0, 0, renderWidth, renderHeight), Data, BgImage.BackBufferStride, 0);
  146. }
  147. if (cacahe.Count > 0)
  148. {
  149. if (cacahe.ContainsKey(renderData.PageIndex - 1))
  150. {
  151. TextRectList = cacahe[renderData.PageIndex - 1];
  152. }
  153. else
  154. {
  155. TextRectList = null;
  156. }
  157. }
  158. ClearDraw();
  159. }
  160. }
  161. }
  162. private void ClearDraw()
  163. {
  164. //笨办法,有好方案随时能换,目前是以触发属性改变事件的方式激活清空
  165. IsShowRect = !IsShowRect;
  166. IsShowRect = !IsShowRect;
  167. }
  168. public ScanViwerViewModel(IEventAggregator eventAggregator)
  169. {
  170. CloseTooltipCommand = new DelegateCommand(CloseTooltip);
  171. CancelEnhancedCommand = new DelegateCommand(CancelEnhanced);
  172. EnhancedFilePathList = new List<string>();
  173. eventAggregator.GetEvent<ScanEvent>().Subscribe(ChangeScanMode, e => e.Unicode == App.mainWindowViewModel.SelectedItem.Unicode);
  174. cacahe = new Dictionary<int, List<KeyValuePair<Rect, string>>>();
  175. }
  176. /// <summary>
  177. /// 区域识别
  178. /// </summary>
  179. public void AreaProcess(Rect AreaRect, Rect ImageRect)
  180. {
  181. COCREngine imEngine = new COCREngine(App.modelFolderPath);
  182. string path = App.CachePath.MergeFilePath;
  183. string name = Guid.NewGuid().ToString();
  184. path = Path.Combine(path, name);
  185. Directory.CreateDirectory(path);
  186. CPDFDocument CurrentDoc = PDFViewer.Document;
  187. Task.Run(() =>
  188. {
  189. CErrorCode error = imEngine.SetModel(language);
  190. cacahe.Remove(PDFViewer.CurrentIndex);
  191. string pageImagePath = Path.Combine(path, "Area");
  192. Directory.CreateDirectory(pageImagePath);
  193. pageImagePath = Path.Combine(pageImagePath, PDFViewer.CurrentIndex.ToString());
  194. if (IsEnhanced)
  195. {
  196. BitmapImage image = new BitmapImage(new Uri(EnhancedFilePathList[PDFViewer.CurrentIndex]));
  197. BgImage = new WriteableBitmap(image);
  198. double zoomWidth = AreaRect.Width / ImageRect.Width;
  199. double zoomHeight = AreaRect.Height / ImageRect.Height;
  200. double zoomX = (AreaRect.X - ImageRect.X) / ImageRect.Width;
  201. double zoomY = (AreaRect.Y - ImageRect.Y) / ImageRect.Height;
  202. Int32Rect rect = new Int32Rect((int)(BgImage.Width * zoomX), (int)(BgImage.Height * zoomY), (int)(BgImage.Width * zoomWidth), (int)(BgImage.Height * zoomHeight));
  203. byte[] renderData = new byte[(int)(BgImage.Width * BgImage.Height * 4)];
  204. BgImage.CopyPixels(rect, renderData, BgImage.BackBufferStride, 0);
  205. WriteableBitmap bitmap = new WriteableBitmap(rect.Width, rect.Height, DpiHelpers.Dpi, DpiHelpers.Dpi, PixelFormats.Bgra32, null);
  206. bitmap.WritePixels(new Int32Rect(0, 0, rect.Width, rect.Height), renderData, BgImage.BackBufferStride, 0);
  207. BitmapEncoder encoder = new PngBitmapEncoder();
  208. encoder.Frames.Add(BitmapFrame.Create(bitmap));
  209. using (FileStream imageStream = File.Create(pageImagePath))
  210. {
  211. encoder.Save(imageStream);
  212. imageStream.Flush();
  213. }
  214. error = imEngine.Process(pageImagePath);
  215. if (imEngine.OCRResultList == null)
  216. return;
  217. List<KeyValuePair<Rect, string>> RectList = new List<KeyValuePair<Rect, string>>();
  218. foreach (COCRResult ocrResult in imEngine.OCRResultList)
  219. {
  220. List<Point> rectPoints = new List<Point>();
  221. for (int j = 0; j < ocrResult.position.Length; j += 2)
  222. {
  223. rectPoints.Add(new Point(ocrResult.position[j], ocrResult.position[j + 1]));
  224. }
  225. int left = (int)rectPoints.AsEnumerable().Min(x => x.X);
  226. int right = (int)rectPoints.AsEnumerable().Max(x => x.X);
  227. int top = (int)rectPoints.AsEnumerable().Min(x => x.Y);
  228. int bottom = (int)rectPoints.AsEnumerable().Max(x => x.Y);
  229. RectList.Add(new KeyValuePair<Rect, string>(new Rect(left + BgImage.Width * zoomX, top + BgImage.Height * zoomY, right - left, bottom - top), ocrResult.text));
  230. }
  231. cacahe.Add(PDFViewer.CurrentIndex, RectList);
  232. if (cacahe.Count > 0)
  233. {
  234. if (cacahe.ContainsKey(PDFViewer.CurrentIndex))
  235. {
  236. TextRectList = cacahe[PDFViewer.CurrentIndex];
  237. }
  238. else
  239. {
  240. TextRectList = null;
  241. }
  242. }
  243. }
  244. else
  245. {
  246. CPDFPage pdfPage = CurrentDoc.PageAtIndex(PDFViewer.CurrentIndex);
  247. double zoomWidth = AreaRect.Width / ImageRect.Width;
  248. double zoomHeight = AreaRect.Height / ImageRect.Height;
  249. double zoomX = (AreaRect.X - ImageRect.X) / ImageRect.Width;
  250. double zoomY = (AreaRect.Y - ImageRect.Y) / ImageRect.Height;
  251. float zoom = (float)(DpiHelpers.Dpi / 72D);
  252. int renderWidth = (int)(pdfPage.PageSize.Width * zoomWidth * zoom);
  253. int renderHeight = (int)(pdfPage.PageSize.Height * zoomHeight * zoom);
  254. int renderX = (int)(pdfPage.PageSize.Width * zoomX * zoom);
  255. int renderY = (int)(pdfPage.PageSize.Height * zoomY * zoom);
  256. byte[] renderData = new byte[renderWidth * renderHeight * 4];
  257. pdfPage.RenderPageBitmapWithMatrix(zoom, new Rect(renderX, renderY, renderWidth, renderHeight), 0xFFFFFFFF, renderData, 0);
  258. WriteableBitmap bitmap = new WriteableBitmap(renderWidth, renderHeight, DpiHelpers.Dpi, DpiHelpers.Dpi, PixelFormats.Bgra32, null);
  259. bitmap.WritePixels(new Int32Rect(0, 0, renderWidth, renderHeight), renderData, bitmap.BackBufferStride, 0);
  260. BitmapEncoder encoder = new PngBitmapEncoder();
  261. encoder.Frames.Add(BitmapFrame.Create(bitmap));
  262. using (FileStream imageStream = File.Create(pageImagePath))
  263. {
  264. encoder.Save(imageStream);
  265. imageStream.Flush();
  266. }
  267. error = imEngine.Process(pageImagePath);
  268. if (imEngine.OCRResultList == null)
  269. return;
  270. List<KeyValuePair<Rect, string>> RectList = new List<KeyValuePair<Rect, string>>();
  271. foreach (COCRResult ocrResult in imEngine.OCRResultList)
  272. {
  273. List<Point> rectPoints = new List<Point>();
  274. for (int j = 0; j < ocrResult.position.Length; j += 2)
  275. {
  276. rectPoints.Add(new Point(ocrResult.position[j], ocrResult.position[j + 1]));
  277. }
  278. int left = (int)rectPoints.AsEnumerable().Min(x => x.X);
  279. int right = (int)rectPoints.AsEnumerable().Max(x => x.X);
  280. int top = (int)rectPoints.AsEnumerable().Min(x => x.Y);
  281. int bottom = (int)rectPoints.AsEnumerable().Max(x => x.Y);
  282. RectList.Add(new KeyValuePair<Rect, string>(new Rect(left + renderX, top + renderY, right - left, bottom - top), ocrResult.text));
  283. }
  284. cacahe.Add(PDFViewer.CurrentIndex, RectList);
  285. if (cacahe.Count > 0)
  286. {
  287. if (cacahe.ContainsKey(PDFViewer.CurrentIndex))
  288. {
  289. TextRectList = cacahe[PDFViewer.CurrentIndex];
  290. }
  291. else
  292. {
  293. TextRectList = null;
  294. }
  295. }
  296. }
  297. imEngine.Release();
  298. });
  299. ClearDraw();
  300. }
  301. private void CancelEnhanced()
  302. {
  303. ShowTooltip = Visibility.Collapsed;
  304. IsEnhanced = false;
  305. CPDFPage pdfPage = PDFViewer.Document.PageAtIndex(PDFViewer.CurrentIndex);
  306. float zoom = (float)(DpiHelpers.Dpi / 72D);
  307. int renderWidth = (int)(pdfPage.PageSize.Width * zoom);
  308. int renderHeight = (int)(pdfPage.PageSize.Height * zoom);
  309. byte[] Data = new byte[renderWidth * renderHeight * 4];
  310. pdfPage.RenderPageBitmapWithMatrix(zoom, new Rect(0, 0, renderWidth, renderHeight), 0xFFFFFFFF, Data, 0);
  311. BgImage = new WriteableBitmap(renderWidth, renderHeight, DpiHelpers.Dpi, DpiHelpers.Dpi, PixelFormats.Bgra32, null);
  312. BgImage.WritePixels(new Int32Rect(0, 0, renderWidth, renderHeight), Data, BgImage.BackBufferStride, 0);
  313. if (cacahe.Count > 0)
  314. {
  315. if (cacahe.ContainsKey(PDFViewer.CurrentIndex))
  316. {
  317. TextRectList = cacahe[PDFViewer.CurrentIndex];
  318. }
  319. else
  320. {
  321. TextRectList = null;
  322. }
  323. }
  324. }
  325. private void CloseTooltip()
  326. {
  327. ShowTooltip = Visibility.Collapsed;
  328. }
  329. /// <summary>
  330. /// 根据Vm事件通知处理OCR与区域识别或者增强扫描事件
  331. /// </summary>
  332. /// <param name="e"></param>
  333. private void ChangeScanMode(ScanEventArgs e)
  334. {
  335. IsShowRect = false;
  336. switch (e.Mode)
  337. {
  338. case ScanMode.Unknown:
  339. break;
  340. case ScanMode.Enhanced:
  341. EnhancedProcess(e);
  342. IsEnhanced = true;
  343. break;
  344. case ScanMode.OCR:
  345. if (IsEnhanced)
  346. {
  347. EnhancedOCRProcess(e);
  348. }
  349. else
  350. {
  351. OCRProcess(e);
  352. }
  353. break;
  354. case ScanMode.Area:
  355. language = (COCRLanguage)e.ScanLanguage;
  356. IsShowRect = true;
  357. break;
  358. default:
  359. break;
  360. }
  361. }
  362. /// <summary>
  363. /// 增强扫描
  364. /// </summary>
  365. private void EnhancedProcess(ScanEventArgs args)
  366. {
  367. CPDFDocument CurrentDoc = PDFViewer.Document;
  368. string path = App.CachePath.MergeFilePath;
  369. string name = Guid.NewGuid().ToString();
  370. path = Path.Combine(path, name);
  371. string EnhancePath = Path.Combine(path, "Enhance");
  372. Directory.CreateDirectory(path);
  373. Directory.CreateDirectory(EnhancePath);
  374. Task.Run(() =>
  375. {
  376. CIMEngine imEngine = new CIMEngine(App.modelFolderPath);
  377. CErrorCode error = imEngine.SetModel();
  378. if (error!= CErrorCode.E_DA_SUCCESS)
  379. {
  380. return;
  381. }
  382. //显示进度条
  383. App.mainWindowViewModel.IsProcessVisible = Visibility.Visible;
  384. App.mainWindowViewModel.MaxValue = CurrentDoc.PageCount;
  385. for (int i = 0; i < CurrentDoc.PageCount; i++)
  386. {
  387. //刷新进度
  388. App.Current.Dispatcher.Invoke(async () =>
  389. {
  390. App.mainWindowViewModel.Value = i;
  391. await Task.Delay(5);
  392. });
  393. string pageImagePath = Path.Combine(path, i.ToString());
  394. string pageEnhancePath = Path.Combine(EnhancePath, i.ToString() + ".png");
  395. try
  396. {
  397. CPDFPage pdfPage = CurrentDoc.PageAtIndex(i);
  398. float zoom = (float)(DpiHelpers.Dpi / 72D);
  399. int renderWidth = (int)(pdfPage.PageSize.Width * zoom);
  400. int renderHeight = (int)(pdfPage.PageSize.Height * zoom);
  401. byte[] renderData = new byte[renderWidth * renderHeight * 4];
  402. pdfPage.RenderPageBitmapWithMatrix(zoom, new Rect(0, 0, renderWidth, renderHeight), 0xFFFFFFFF, renderData, 0);
  403. WriteableBitmap bitmap = new WriteableBitmap(renderWidth, renderHeight, DpiHelpers.Dpi, DpiHelpers.Dpi, PixelFormats.Bgra32, null);
  404. bitmap.WritePixels(new Int32Rect(0, 0, renderWidth, renderHeight), renderData, bitmap.BackBufferStride, 0);
  405. BitmapEncoder encoder = new PngBitmapEncoder();
  406. encoder.Frames.Add(BitmapFrame.Create(bitmap));
  407. using (FileStream imageStream = File.Create(pageImagePath))
  408. {
  409. encoder.Save(imageStream);
  410. imageStream.Flush();
  411. }
  412. //File.Create(pageEnhancePath);
  413. error = imEngine.Process(pageImagePath, pageEnhancePath);
  414. EnhancedFilePathList.Add(pageEnhancePath);
  415. }
  416. catch
  417. {
  418. }
  419. }
  420. Application.Current.Dispatcher.Invoke(() =>
  421. {
  422. BitmapImage image = new BitmapImage(new Uri(EnhancedFilePathList[PDFViewer.CurrentIndex]));
  423. BgImage = new WriteableBitmap(image);
  424. });
  425. imEngine.Release();
  426. ShowTooltip = Visibility.Visible;
  427. //隐藏进度条
  428. App.mainWindowViewModel.IsProcessVisible = Visibility.Collapsed;
  429. App.mainWindowViewModel.MaxValue = 0;
  430. App.mainWindowViewModel.Value = 0;
  431. });
  432. }
  433. /// <summary>
  434. /// 使用当前文档OCR
  435. /// </summary>
  436. /// <param name="args"></param>
  437. private void OCRProcess(ScanEventArgs args)
  438. {
  439. Task.Run(() =>
  440. {
  441. COCREngine imEngine = new COCREngine(App.modelFolderPath);
  442. string path = App.CachePath.MergeFilePath;
  443. string name = Guid.NewGuid().ToString();
  444. path = Path.Combine(path, name);
  445. Directory.CreateDirectory(path);
  446. CPDFDocument CurrentDoc = PDFViewer.Document;
  447. CErrorCode error = imEngine.SetModel((COCRLanguage)args.ScanLanguage);
  448. if (error != CErrorCode.E_DA_SUCCESS)
  449. {
  450. return;
  451. }
  452. cacahe.Clear();
  453. //显示进度条
  454. App.mainWindowViewModel.IsProcessVisible = Visibility.Visible;
  455. App.mainWindowViewModel.MaxValue = args.PageRange.Count;
  456. for (int i = 0; i < args.PageRange.Count; i++)
  457. {
  458. //刷新进度
  459. App.Current.Dispatcher.Invoke(async () =>
  460. {
  461. App.mainWindowViewModel.Value = i;
  462. await Task.Delay(5);
  463. });
  464. string pageImagePath = Path.Combine(path, args.PageRange[i].ToString());
  465. CPDFPage pdfPage = CurrentDoc.PageAtIndex(args.PageRange[i]);
  466. float zoom = (float)(DpiHelpers.Dpi / 72D);
  467. int renderWidth = (int)(pdfPage.PageSize.Width * zoom);
  468. int renderHeight = (int)(pdfPage.PageSize.Height * zoom);
  469. byte[] renderData = new byte[renderWidth * renderHeight * 4];
  470. pdfPage.RenderPageBitmapWithMatrix(zoom, new Rect(0, 0, renderWidth, renderHeight), 0xFFFFFFFF, renderData, 0);
  471. WriteableBitmap bitmap = new WriteableBitmap(renderWidth, renderHeight, DpiHelpers.Dpi, DpiHelpers.Dpi, PixelFormats.Bgra32, null);
  472. bitmap.WritePixels(new Int32Rect(0, 0, renderWidth, renderHeight), renderData, bitmap.BackBufferStride, 0);
  473. BitmapEncoder encoder = new PngBitmapEncoder();
  474. encoder.Frames.Add(BitmapFrame.Create(bitmap));
  475. using (FileStream imageStream = File.Create(pageImagePath))
  476. {
  477. encoder.Save(imageStream);
  478. imageStream.Flush();
  479. }
  480. error = imEngine.Process(pageImagePath);
  481. if (imEngine.OCRResultList == null)
  482. {
  483. App.mainWindowViewModel.IsProcessVisible = Visibility.Collapsed;
  484. App.mainWindowViewModel.MaxValue = 0;
  485. App.mainWindowViewModel.Value = 0;
  486. return;
  487. }
  488. List<KeyValuePair<Rect, string>> RectList = new List<KeyValuePair<Rect, string>>();
  489. foreach (COCRResult ocrResult in imEngine.OCRResultList)
  490. {
  491. List<Point> rectPoints = new List<Point>();
  492. for (int j = 0; j < ocrResult.position.Length; j += 2)
  493. {
  494. rectPoints.Add(new Point(ocrResult.position[j], ocrResult.position[j + 1]));
  495. }
  496. int left = (int)rectPoints.AsEnumerable().Min(x => x.X);
  497. int right = (int)rectPoints.AsEnumerable().Max(x => x.X);
  498. int top = (int)rectPoints.AsEnumerable().Min(x => x.Y);
  499. int bottom = (int)rectPoints.AsEnumerable().Max(x => x.Y);
  500. RectList.Add(new KeyValuePair<Rect, string>(new Rect(left, top, right - left, bottom - top), ocrResult.text));
  501. }
  502. cacahe.Add(args.PageRange[i], RectList);
  503. }
  504. if (cacahe.Count > 0)
  505. {
  506. if (cacahe.ContainsKey(PDFViewer.CurrentIndex))
  507. {
  508. TextRectList = cacahe[PDFViewer.CurrentIndex];
  509. }
  510. else
  511. {
  512. TextRectList = null;
  513. }
  514. }
  515. imEngine.Release();
  516. //隐藏进度条
  517. App.mainWindowViewModel.IsProcessVisible = Visibility.Collapsed;
  518. App.mainWindowViewModel.MaxValue = 0;
  519. App.mainWindowViewModel.Value = 0;
  520. });
  521. }
  522. /// <summary>
  523. /// 使用增强扫描结果进行OCR
  524. /// </summary>
  525. /// <param name="args"></param>
  526. private void EnhancedOCRProcess(ScanEventArgs args)
  527. {
  528. Task.Run(() =>
  529. {
  530. COCREngine imEngine = new COCREngine(App.modelFolderPath);
  531. CErrorCode error = imEngine.SetModel((COCRLanguage)args.ScanLanguage);
  532. cacahe.Clear();
  533. for (int i = 0; i < args.PageRange.Count; i++)
  534. {
  535. error = imEngine.Process(EnhancedFilePathList[args.PageRange[i]]);
  536. if (imEngine.OCRResultList == null)
  537. return;
  538. List<KeyValuePair<Rect, string>> RectList = new List<KeyValuePair<Rect, string>>();
  539. foreach (COCRResult ocrResult in imEngine.OCRResultList)
  540. {
  541. List<Point> rectPoints = new List<Point>();
  542. for (int j = 0; j < ocrResult.position.Length; j += 2)
  543. {
  544. rectPoints.Add(new Point(ocrResult.position[j], ocrResult.position[j + 1]));
  545. }
  546. int left = (int)rectPoints.AsEnumerable().Min(x => x.X);
  547. int right = (int)rectPoints.AsEnumerable().Max(x => x.X);
  548. int top = (int)rectPoints.AsEnumerable().Min(x => x.Y);
  549. int bottom = (int)rectPoints.AsEnumerable().Max(x => x.Y);
  550. RectList.Add(new KeyValuePair<Rect, string>(new Rect(left, top, right - left, bottom - top), ocrResult.text));
  551. }
  552. cacahe.Add(args.PageRange[i], RectList);
  553. }
  554. if (cacahe.Count > 0)
  555. {
  556. if (cacahe.ContainsKey(PDFViewer.CurrentIndex))
  557. {
  558. TextRectList = cacahe[PDFViewer.CurrentIndex];
  559. }
  560. else
  561. {
  562. TextRectList = null;
  563. }
  564. }
  565. imEngine.Release();
  566. });
  567. }
  568. public void Save()
  569. {
  570. for (int i = 0; i < PDFViewer.Document.PageCount; i++)
  571. {
  572. if (cacahe.ContainsKey(i))
  573. {
  574. CPDFPage pdfPage = PDFViewer.Document.PageAtIndex(i);
  575. CPDFEditPage editPage = pdfPage.GetEditPage();
  576. //2023.2.17版本的SDK不能使用EditText枚举,会有内存问题
  577. editPage.BeginEdit(CPDFEditType.EditImage);
  578. for (int y = textRectList.Count - 1; y >= 0; y--)
  579. {
  580. KeyValuePair<Rect, string> textBlock = textRectList[y];
  581. int fontSize = 1;
  582. Rect pdfRect = DpiHelpers.GetRawRect(textBlock.Key);
  583. for (; fontSize < 100; fontSize++)
  584. {
  585. Rect cmpRect = pdfPage.GetTextContentRect(fontSize, "Helvetica", textBlock.Value);
  586. if (cmpRect.Width > pdfRect.Width || cmpRect.Height > pdfRect.Height)
  587. {
  588. fontSize -= 1;
  589. break;
  590. }
  591. }
  592. //字体
  593. CPDFEditTextArea editArea = editPage.CreateNewTextArea(pdfRect, "Helvetica", fontSize, new byte[] { 0, 0, 0 });
  594. //透明度
  595. editArea.SetCharsFontTransparency(0);
  596. bool x = editArea.SetTextAreaAlign(TextAlignType.AlignChar);
  597. editArea.InsertText(textBlock.Value);
  598. }
  599. editPage.EndEdit();
  600. }
  601. }
  602. }
  603. }
  604. }