OCRDownloadProgressViewModel.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. using ComPDFKitViewer.PdfViewer;
  2. using Microsoft.Office.Interop.Excel;
  3. using PDF_Master.CustomControl;
  4. using PDF_Master.Helper;
  5. using PDF_Master.Properties;
  6. using PDF_Master.Strings.MainPage;
  7. using Prism.Commands;
  8. using Prism.Mvvm;
  9. using Prism.Services.Dialogs;
  10. using System;
  11. using System.Collections.Generic;
  12. using System.ComponentModel;
  13. using System.Diagnostics;
  14. using System.IO;
  15. using System.IO.Compression;
  16. using System.Linq;
  17. using System.Threading.Tasks;
  18. using System.Windows;
  19. namespace PDF_Master.ViewModels.Dialog.ConverterDialogs
  20. {
  21. public class OCRDownloadProgressViewModel : BindableBase, IDialogAware
  22. {
  23. public string Title => throw new NotImplementedException();
  24. private string T_progressBarName;
  25. public string T_ProgressBarName
  26. {
  27. get { return T_progressBarName; }
  28. set
  29. {
  30. SetProperty(ref T_progressBarName, value);
  31. }
  32. }
  33. private string T_tryAgainBtn;
  34. public string T_TryAgainBtn
  35. {
  36. get { return T_tryAgainBtn; }
  37. set
  38. {
  39. SetProperty(ref T_tryAgainBtn, value);
  40. }
  41. }
  42. private string T_errorTextBlock;
  43. public string T_ErrorTextBlock
  44. {
  45. get { return T_errorTextBlock; }
  46. set
  47. {
  48. SetProperty(ref T_errorTextBlock, value);
  49. }
  50. }
  51. private string T_cancelBtn;
  52. public string T_CancelBtn
  53. {
  54. get { return T_cancelBtn; }
  55. set
  56. {
  57. SetProperty(ref T_cancelBtn, value);
  58. }
  59. }
  60. private void SetLangText()
  61. {
  62. T_ProgressBarName = App.MainPageLoader.GetString("OCR_Downloading");
  63. T_tryAgainBtn = App.MainPageLoader.GetString("OCR_DownloadRedownload");
  64. T_ErrorTextBlock = App.MainPageLoader.GetString("OCR_DownloadError");
  65. T_CancelBtn= App.MainPageLoader.GetString("OCR_CancelDownload");
  66. }
  67. private Visibility errorStackPanelVisible = Visibility.Collapsed;
  68. public Visibility ErrorStackPanelVisible
  69. {
  70. get { return errorStackPanelVisible; }
  71. set
  72. {
  73. SetProperty(ref errorStackPanelVisible, value);
  74. }
  75. }
  76. private Visibility tryAgainBtnVisible = Visibility.Collapsed;
  77. public Visibility TryAgainBtnVisible
  78. {
  79. get { return tryAgainBtnVisible; }
  80. set
  81. {
  82. SetProperty(ref tryAgainBtnVisible, value);
  83. }
  84. }
  85. System.Net.WebClient client = null;
  86. private MainPage mainPage = null;
  87. public string exePath = "";
  88. private string FileNameComPDFKit = "";
  89. private bool isItemClieck = false;
  90. private bool isDownloadCancelled = false;
  91. private CPDFViewer Pdfviewer { set; get; }
  92. private string password = "";
  93. private double currentSize;
  94. //home 是false
  95. private bool SelectHomeOrmain = false;
  96. private string progressBarCurrentSizeValue="0 MB";
  97. public string ProgressBarCurrentSizeValue
  98. {
  99. get { return progressBarCurrentSizeValue; }
  100. set
  101. {
  102. SetProperty(ref progressBarCurrentSizeValue, value);
  103. }
  104. }
  105. private string progressBarFileSize="0 MB";
  106. public string ProgressBarFileSize
  107. {
  108. get { return progressBarFileSize; }
  109. set
  110. {
  111. SetProperty(ref progressBarFileSize, value);
  112. }
  113. }
  114. private double progressBarCurrentValue = 0;
  115. public double ProgressBarCurrentValue
  116. {
  117. get { return progressBarCurrentValue; }
  118. set
  119. {
  120. SetProperty(ref progressBarCurrentValue, value);
  121. }
  122. }
  123. #region 委托声明
  124. public DelegateCommand CancelCommand { get; set; }
  125. public DelegateCommand TryAgainCommnad { get; set; }
  126. #endregion
  127. public OCRDownloadProgressViewModel()
  128. {
  129. CancelCommand= new DelegateCommand(cancel);
  130. TryAgainCommnad= new DelegateCommand(tryAgain);
  131. }
  132. private async void tryAgain()
  133. {
  134. try
  135. {
  136. client = null;
  137. FileComparisonHelper.RemoveOCRModel();
  138. FileComparisonHelper.Getpdfreaderprocast();
  139. await DownloadOCR();
  140. }
  141. catch
  142. {
  143. await Task.Delay(100);
  144. ErrorStackPanelVisible = Visibility.Visible;
  145. TryAgainBtnVisible = Visibility.Visible;
  146. }
  147. }
  148. private void cancel()
  149. {
  150. isDownloadCancelled = true;
  151. RequestClose.Invoke(new DialogResult(ButtonResult.Cancel));
  152. }
  153. public async Task<bool> DownloadOCR()
  154. {
  155. bool canDownloadCancelled = false;
  156. ErrorStackPanelVisible = Visibility.Collapsed;
  157. TryAgainBtnVisible = Visibility.Collapsed;
  158. try
  159. {
  160. await Task.Run(() =>
  161. {
  162. string folder = System.IO.Path.Combine(App.CurrentPath, "modelzip");
  163. FileNameComPDFKit = folder + "\\" + "model" + ".zip";
  164. long fileSize = 0;
  165. if (!Directory.Exists(folder))
  166. {
  167. Directory.CreateDirectory(folder);
  168. client = new System.Net.WebClient();
  169. System.Net.WebRequest request = System.Net.WebRequest.Create(Settings.Default.AppProperties.OCRFile_Url);
  170. request.Method = "HEAD";
  171. using (System.Net.WebResponse response = request.GetResponse())
  172. {
  173. if (long.TryParse(response.Headers.Get("Content-Length"), out long contentLength))
  174. {
  175. fileSize = contentLength;
  176. App.Current.Dispatcher.Invoke(() =>
  177. {
  178. ProgressBarFileSize = ((double)fileSize / (1024 * 1024)).ToString("0.00") + " MB";
  179. currentSize = ((double)fileSize / (1024 * 1024));
  180. });
  181. }
  182. }
  183. client.DownloadProgressChanged += Client_DownloadProgressChanged;
  184. client.DownloadFileCompleted += new System.ComponentModel.AsyncCompletedEventHandler(DownloadCompleted);
  185. client.DownloadFileAsync(new Uri(Settings.Default.AppProperties.OCRFile_Url), FileNameComPDFKit);
  186. }
  187. canDownloadCancelled = true;
  188. });
  189. return canDownloadCancelled;
  190. }
  191. catch
  192. {
  193. canDownloadCancelled = false;
  194. ErrorStackPanelVisible = Visibility.Visible;
  195. TryAgainBtnVisible = Visibility.Visible;
  196. //Close();
  197. return canDownloadCancelled;
  198. }
  199. }
  200. private void DownloadCompleted(object sender, AsyncCompletedEventArgs e)
  201. {
  202. string folderPath = App.CurrentPath;
  203. //解压转档资源文件,intiRes
  204. string folderModelPath = App.CurrentPath;
  205. if (!Directory.Exists(folderModelPath + "//model"))
  206. {
  207. ExtractWithProgress(FileNameComPDFKit, folderModelPath);
  208. }
  209. exePath = folderModelPath;
  210. }
  211. public void ExtractWithProgress(string sourceZipFilePath, string destinationFolderPath)
  212. {
  213. var extractedCount = 0;
  214. if (System.IO.File.Exists(sourceZipFilePath))
  215. {
  216. try
  217. {
  218. using (var zip = ZipFile.OpenRead(sourceZipFilePath))
  219. {
  220. var totalEntries = zip.Entries.Count;
  221. var progressStep = totalEntries / 20;
  222. foreach (var entry in zip.Entries)
  223. {
  224. var extractionPath = System.IO.Path.Combine(destinationFolderPath, entry.FullName);
  225. if (entry.Name == "")
  226. {
  227. // 处理目录的情况,例如在ZIP文件中的文件夹
  228. Directory.CreateDirectory(extractionPath);
  229. continue;
  230. }
  231. var entryDirectoryName = System.IO.Path.GetDirectoryName(extractionPath);
  232. if (!Directory.Exists(entryDirectoryName))
  233. {
  234. Directory.CreateDirectory(entryDirectoryName);
  235. }
  236. entry.ExtractToFile(extractionPath, true);
  237. extractedCount++;
  238. // 更新进度条:输出百分比或者使用其他进度条组件
  239. App.Current.Dispatcher.Invoke(() =>
  240. {
  241. ProgressBarCurrentValue = extractedCount / progressStep + 80;
  242. ProgressBarCurrentSizeValue = (currentSize * (ProgressBarCurrentValue / 100)).ToString("0.00") + " MB";
  243. Trace.WriteLine("进度条 : " + ProgressBarCurrentValue);
  244. });
  245. }
  246. }
  247. App.Current.Dispatcher.Invoke(() =>
  248. {
  249. ProgressBarCurrentValue = 0;
  250. ProgressBarCurrentSizeValue = (currentSize * (ProgressBarCurrentValue / 100)).ToString("0.00") + " MB";
  251. string folderzipPath = System.IO.Path.Combine(App.CurrentPath, "modelzip");
  252. if (Directory.Exists(folderzipPath))
  253. {
  254. Directory.Delete(folderzipPath, true);
  255. }
  256. //Close();
  257. if (FileComparisonHelper.OCRModelItExist())
  258. {
  259. if (SelectHomeOrmain)
  260. {
  261. if (mainPage != null)
  262. {
  263. //mainPage.ShowOCRDialog(isItemClieck);
  264. }
  265. }
  266. else
  267. {
  268. //新OCR弹窗
  269. //ConvertOCRNewDialog convertOCRNewDialog = new ConvertOCRNewDialog();
  270. //convertOCRNewDialog.InitBeforeShow(Pdfviewer, password);
  271. //convertOCRNewDialog.Owner = App.Current.MainWindow;
  272. //convertOCRNewDialog.ShowDialog();
  273. //ConvertOCRDialog dialog = new ConvertOCRDialog();
  274. //dialog.InitBeforeShow(Pdfviewer, password);
  275. //dialog.StartPosition = FormStartPosition.CenterScreen;
  276. //dialog.ShowDialog();
  277. }
  278. }
  279. else
  280. {
  281. string folderPath = System.IO.Path.Combine(App.CurrentPath, "model");
  282. if (Directory.Exists(folderPath))
  283. {
  284. Directory.Delete(folderPath, true);
  285. }
  286. MessageBoxEx.Show("需要更新最新版的APP才能下载哦");
  287. }
  288. });
  289. }
  290. catch
  291. {
  292. App.Current.Dispatcher.Invoke(() =>
  293. {
  294. ProgressBarCurrentValue = 0;
  295. ProgressBarCurrentSizeValue = (currentSize * (ProgressBarCurrentValue / 100)).ToString("0.00") + " MB";
  296. //Close();
  297. ErrorStackPanelVisible = Visibility.Visible;
  298. TryAgainBtnVisible = Visibility.Visible;
  299. });
  300. }
  301. }
  302. else
  303. {
  304. App.Current.Dispatcher.Invoke(() =>
  305. {
  306. ProgressBarCurrentValue = 0;
  307. ProgressBarCurrentSizeValue = (currentSize * (ProgressBarCurrentValue / 100)).ToString("0.00") + " MB";
  308. ErrorStackPanelVisible = Visibility.Visible;
  309. TryAgainBtnVisible = Visibility.Visible;
  310. //Close();
  311. });
  312. }
  313. }
  314. private void Client_DownloadProgressChanged(object sender, System.Net.DownloadProgressChangedEventArgs e)
  315. {
  316. App.Current.Dispatcher.Invoke(() =>
  317. {
  318. ProgressBarCurrentValue = e.ProgressPercentage * 0.8;
  319. ProgressBarCurrentSizeValue = (currentSize * (ProgressBarCurrentValue / 100)).ToString("0.00") + " MB";
  320. });
  321. if (isDownloadCancelled)
  322. {
  323. if (client != null)
  324. {
  325. App.Current.Dispatcher.Invoke(() =>
  326. {
  327. client.CancelAsync();
  328. });
  329. }
  330. // 执行其他取消操作
  331. // ...
  332. }
  333. }
  334. public bool? ShowDialog(MainPage mainPage, bool isItemClieck)
  335. {
  336. this.mainPage = mainPage;
  337. this.isItemClieck = isItemClieck;
  338. SelectHomeOrmain = true;
  339. return false;
  340. //return base.ShowDialog();
  341. }
  342. public bool? ShowDialog(CPDFViewer kmpdfviewer, string password)
  343. {
  344. Pdfviewer = kmpdfviewer;
  345. this.password = password;
  346. SelectHomeOrmain = false;
  347. return false;
  348. }
  349. private async void Window_Loaded(object sender, RoutedEventArgs e)
  350. {
  351. await DownloadOCR();
  352. }
  353. public event Action<IDialogResult> RequestClose;
  354. public bool CanCloseDialog()
  355. {
  356. return true;
  357. }
  358. public void OnDialogClosed()
  359. {
  360. }
  361. public void OnDialogOpened(IDialogParameters parameters)
  362. {
  363. }
  364. }
  365. }