OCRDownloadProgressViewModel.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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. SetLangText();
  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. if (client != null)
  152. {
  153. //防止退出没释放
  154. client.CancelAsync();
  155. client.Dispose();
  156. FileComparisonHelper.RemoveOCRModel();
  157. }
  158. RequestClose.Invoke(new DialogResult(ButtonResult.Cancel));
  159. }
  160. public async Task<bool> DownloadOCR()
  161. {
  162. bool canDownloadCancelled = false;
  163. ErrorStackPanelVisible = Visibility.Collapsed;
  164. TryAgainBtnVisible = Visibility.Collapsed;
  165. try
  166. {
  167. await Task.Run(() =>
  168. {
  169. string folder = System.IO.Path.Combine(App.CurrentPath, "OCREnginezip");
  170. FileNameComPDFKit = folder + "\\" + "OCREngine" + ".zip";
  171. long fileSize = 0;
  172. if (!Directory.Exists(folder))
  173. {
  174. Directory.CreateDirectory(folder);
  175. client = new System.Net.WebClient();
  176. System.Net.WebRequest request = System.Net.WebRequest.Create(Settings.Default.AppProperties.OCRFile_Url);
  177. request.Method = "HEAD";
  178. using (System.Net.WebResponse response = request.GetResponse())
  179. {
  180. if (long.TryParse(response.Headers.Get("Content-Length"), out long contentLength))
  181. {
  182. fileSize = contentLength;
  183. App.Current.Dispatcher.Invoke(() =>
  184. {
  185. ProgressBarFileSize = ((double)fileSize / (1024 * 1024)).ToString("0.00") + " MB";
  186. currentSize = ((double)fileSize / (1024 * 1024));
  187. });
  188. }
  189. }
  190. client.DownloadProgressChanged += Client_DownloadProgressChanged;
  191. client.DownloadFileCompleted += new System.ComponentModel.AsyncCompletedEventHandler(DownloadCompleted);
  192. client.DownloadFileAsync(new Uri(Settings.Default.AppProperties.OCRFile_Url), FileNameComPDFKit);
  193. }
  194. canDownloadCancelled = true;
  195. });
  196. return canDownloadCancelled;
  197. }
  198. catch
  199. {
  200. canDownloadCancelled = false;
  201. ErrorStackPanelVisible = Visibility.Visible;
  202. TryAgainBtnVisible = Visibility.Visible;
  203. //Close();
  204. return canDownloadCancelled;
  205. }
  206. }
  207. private void DownloadCompleted(object sender, AsyncCompletedEventArgs e)
  208. {
  209. string folderPath = App.CurrentPath;
  210. //解压转档资源文件,intiRes
  211. string folderModelPath = App.CurrentPath;
  212. if (!Directory.Exists(folderModelPath + "//OCREngine"))
  213. {
  214. ExtractWithProgress(FileNameComPDFKit, folderModelPath);
  215. }
  216. exePath = folderModelPath;
  217. }
  218. public void ExtractWithProgress(string sourceZipFilePath, string destinationFolderPath)
  219. {
  220. var extractedCount = 0;
  221. if (System.IO.File.Exists(sourceZipFilePath))
  222. {
  223. try
  224. {
  225. using (var zip = ZipFile.OpenRead(sourceZipFilePath))
  226. {
  227. var totalEntries = zip.Entries.Count;
  228. var progressStep = totalEntries / 20;
  229. foreach (var entry in zip.Entries)
  230. {
  231. var extractionPath = System.IO.Path.Combine(destinationFolderPath, entry.FullName);
  232. string replacement = "OCREngine";
  233. //提换文件夹名字保证文件夹统一
  234. int slashIndex = entry.FullName.IndexOf('/');
  235. if (slashIndex != -1)
  236. {
  237. string modifiedString = replacement + entry.FullName.Substring(slashIndex);
  238. extractionPath = System.IO.Path.Combine(destinationFolderPath, modifiedString);
  239. }
  240. if (entry.Name == "")
  241. {
  242. // 处理目录的情况,例如在ZIP文件中的文件夹
  243. Directory.CreateDirectory(extractionPath);
  244. continue;
  245. }
  246. var entryDirectoryName = System.IO.Path.GetDirectoryName(extractionPath);
  247. if (!Directory.Exists(entryDirectoryName))
  248. {
  249. Directory.CreateDirectory(entryDirectoryName);
  250. }
  251. entry.ExtractToFile(extractionPath, true);
  252. extractedCount++;
  253. // 更新进度条:输出百分比或者使用其他进度条组件
  254. App.Current.Dispatcher.Invoke(() =>
  255. {
  256. ProgressBarCurrentValue = extractedCount / progressStep + 80;
  257. ProgressBarCurrentSizeValue = (currentSize * (ProgressBarCurrentValue / 100)).ToString("0.00") + " MB";
  258. Trace.WriteLine("进度条 : " + ProgressBarCurrentValue);
  259. });
  260. }
  261. }
  262. App.Current.Dispatcher.Invoke(() =>
  263. {
  264. ProgressBarCurrentValue = 0;
  265. ProgressBarCurrentSizeValue = (currentSize * (ProgressBarCurrentValue / 100)).ToString("0.00") + " MB";
  266. string folderzipPath = System.IO.Path.Combine(App.CurrentPath, "OCREnginezip");
  267. if (Directory.Exists(folderzipPath))
  268. {
  269. Directory.Delete(folderzipPath, true);
  270. }
  271. //Close()
  272. if (FileComparisonHelper.OCRModelItExist())
  273. {
  274. var dialogresult = new DialogResult(ButtonResult.OK);
  275. RequestClose.Invoke(dialogresult);
  276. }
  277. else
  278. {
  279. string folderPath = System.IO.Path.Combine(App.CurrentPath, "OCREngine");
  280. if (Directory.Exists(folderPath))
  281. {
  282. Directory.Delete(folderPath, true);
  283. }
  284. //MessageBoxEx.Show("需要更新最新版的APP才能下载哦");
  285. }
  286. });
  287. }
  288. catch
  289. {
  290. App.Current.Dispatcher.Invoke(() =>
  291. {
  292. ProgressBarCurrentValue = 0;
  293. ProgressBarCurrentSizeValue = (currentSize * (ProgressBarCurrentValue / 100)).ToString("0.00") + " MB";
  294. //Close();
  295. ErrorStackPanelVisible = Visibility.Visible;
  296. TryAgainBtnVisible = Visibility.Visible;
  297. });
  298. }
  299. }
  300. else
  301. {
  302. App.Current.Dispatcher.Invoke(() =>
  303. {
  304. ProgressBarCurrentValue = 0;
  305. ProgressBarCurrentSizeValue = (currentSize * (ProgressBarCurrentValue / 100)).ToString("0.00") + " MB";
  306. ErrorStackPanelVisible = Visibility.Visible;
  307. TryAgainBtnVisible = Visibility.Visible;
  308. //Close();
  309. });
  310. }
  311. }
  312. private void Client_DownloadProgressChanged(object sender, System.Net.DownloadProgressChangedEventArgs e)
  313. {
  314. App.Current.Dispatcher.Invoke(() =>
  315. {
  316. ProgressBarCurrentValue = e.ProgressPercentage * 0.8;
  317. ProgressBarCurrentSizeValue = (currentSize * (ProgressBarCurrentValue / 100)).ToString("0.00") + " MB";
  318. });
  319. if (isDownloadCancelled)
  320. {
  321. if (client != null)
  322. {
  323. App.Current.Dispatcher.Invoke(() =>
  324. {
  325. client.CancelAsync();
  326. });
  327. }
  328. // 执行其他取消操作
  329. // ...
  330. }
  331. }
  332. public event Action<IDialogResult> RequestClose;
  333. public bool CanCloseDialog()
  334. {
  335. return true;
  336. }
  337. public void OnDialogClosed()
  338. {
  339. }
  340. public async void OnDialogOpened(IDialogParameters parameters)
  341. {
  342. await DownloadOCR();
  343. }
  344. }
  345. }