OCRDownloadProgressViewModel.cs 13 KB

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