ThisApp.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. using Ookii.Dialogs.Wpf;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Windows;
  9. using System.Windows.Media.Imaging;
  10. namespace MeterVision
  11. {
  12. public static class ThisApp
  13. {
  14. public static bool IsDebug = false;
  15. public static bool IsPermanentLicense = false; //是否为永久有效
  16. // 静态属性:页面大小选项
  17. public static readonly int[] PageSizeOptions = new int[] { 10, 20, 50 };
  18. public const int MaxFileSize = 300 * 1024;
  19. public const int RequiredWidth = 320;
  20. public const int RequiredHeight = 240;
  21. public static bool IsJpgFile(string filePath)
  22. {
  23. // 检查文件扩展名是否为 .jpg 或 .jpeg
  24. string extension = System.IO.Path.GetExtension(filePath).ToLower();
  25. return extension == ".jpg"; // || extension == ".jpeg";
  26. }
  27. public static bool IsBmpFile(string filePath)
  28. {
  29. string extension = System.IO.Path.GetExtension(filePath).ToLower();
  30. return extension == ".bmp"; // || extension == ".jpeg";
  31. }
  32. public static bool IsFileSizeValid(string filePath)
  33. {
  34. // 获取文件大小
  35. FileInfo fileInfo = new FileInfo(filePath);
  36. return fileInfo.Length <= MaxFileSize;
  37. }
  38. public static bool IsImageDimensionsValid(string filePath)
  39. {
  40. try
  41. {
  42. // 加载图像并检查尺寸
  43. using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
  44. {
  45. BitmapImage bitmap = new BitmapImage();
  46. bitmap.BeginInit();
  47. bitmap.StreamSource = fs;
  48. bitmap.CacheOption = BitmapCacheOption.OnLoad;
  49. bitmap.EndInit();
  50. // 检查图像的宽度和高度
  51. return bitmap.PixelWidth == RequiredWidth && bitmap.PixelHeight == RequiredHeight;
  52. }
  53. }
  54. catch (Exception ex)
  55. {
  56. // MessageBox.Show($"无法读取图像尺寸: {ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
  57. return false;
  58. }
  59. }
  60. public static string GetNowTime_yyyyMMddHHmmss()
  61. {
  62. DateTime now = DateTime.Now;
  63. return now.ToString("yyyyMMddHHmmss");
  64. }
  65. public static string GetNowTime_yyyyMMdd()
  66. {
  67. DateTime now = DateTime.Now;
  68. return now.ToString("yyyyMMdd");
  69. }
  70. public static string ConvertDateFormat(string originalDateTime)
  71. {
  72. // 定义原始格式
  73. string originalFormat = "yyyyMMddHHmmss";
  74. // 尝试将原始字符串解析为DateTime对象
  75. if (DateTime.TryParseExact(originalDateTime, originalFormat,
  76. System.Globalization.CultureInfo.InvariantCulture,
  77. System.Globalization.DateTimeStyles.None,
  78. out DateTime parsedDate))
  79. {
  80. // 成功解析后,将其格式化为目标格式
  81. return parsedDate.ToString("yyyy-MM-dd HH:mm:ss");
  82. }
  83. else
  84. {
  85. // 如果解析失败,可以返回一个默认值或者抛出异常
  86. //throw new ArgumentException("无法解析提供的日期时间字符串。", nameof(originalDateTime));
  87. return string.Empty;
  88. }
  89. }
  90. public static string ConvertDateFormat_Date(string originalDateTime)
  91. {
  92. // 定义原始格式
  93. string originalFormat = "yyyyMMddHHmmss";
  94. // 尝试将原始字符串解析为DateTime对象
  95. if (DateTime.TryParseExact(originalDateTime, originalFormat,
  96. System.Globalization.CultureInfo.InvariantCulture,
  97. System.Globalization.DateTimeStyles.None,
  98. out DateTime parsedDate))
  99. {
  100. // 成功解析后,将其格式化为目标格式
  101. return parsedDate.ToString("yyyy-MM-dd");
  102. }
  103. else
  104. {
  105. // 如果解析失败,可以返回一个默认值或者抛出异常
  106. //throw new ArgumentException("无法解析提供的日期时间字符串。", nameof(originalDateTime));
  107. return string.Empty;
  108. }
  109. }
  110. public static string ConvertDateFormat_Time(string originalDateTime)
  111. {
  112. // 定义原始格式
  113. string originalFormat = "yyyyMMddHHmmss";
  114. // 尝试将原始字符串解析为DateTime对象
  115. if (DateTime.TryParseExact(originalDateTime, originalFormat,
  116. System.Globalization.CultureInfo.InvariantCulture,
  117. System.Globalization.DateTimeStyles.None,
  118. out DateTime parsedDate))
  119. {
  120. // 成功解析后,将其格式化为目标格式
  121. return parsedDate.ToString("HH:mm:ss");
  122. }
  123. else
  124. {
  125. // 如果解析失败,可以返回一个默认值或者抛出异常
  126. //throw new ArgumentException("无法解析提供的日期时间字符串。", nameof(originalDateTime));
  127. return string.Empty;
  128. }
  129. }
  130. public static string GetFileNameWithoutExtension(string filePath)
  131. {
  132. if (string.IsNullOrEmpty(filePath))
  133. {
  134. //throw new ArgumentNullException(nameof(filePath), "文件路径不能为空");
  135. return string.Empty;
  136. }
  137. return Path.GetFileNameWithoutExtension(filePath);
  138. }
  139. //删除文件
  140. public static void DeleteFile(string filePath)
  141. {
  142. if (File.Exists(filePath))
  143. {
  144. try
  145. {
  146. File.Delete(filePath);
  147. //MessageBox.Show("文件已成功删除!");
  148. }
  149. catch (Exception ex)
  150. {
  151. Console.WriteLine(ex.Message);
  152. //MessageBox.Show($"删除文件时出错: {ex.Message}");
  153. }
  154. }
  155. else
  156. {
  157. //MessageBox.Show("文件不存在!");
  158. }
  159. }
  160. //导出AI日志文件
  161. public static void ExportAiLog(string logPath)
  162. {
  163. try
  164. {
  165. if (string.IsNullOrEmpty(logPath) || !File.Exists(logPath))
  166. {
  167. MessageBox.Show("无法确定原始文件路径或文件不存在。");
  168. return;
  169. }
  170. // 2. 弹出保存对话框
  171. VistaSaveFileDialog saveFileDialog = new VistaSaveFileDialog
  172. {
  173. FileName = Path.GetFileName(logPath), // 默认文件名
  174. DefaultExt = Path.GetExtension(logPath),
  175. Filter = $"AiLog Files (*{Path.GetExtension(logPath)})|*{Path.GetExtension(logPath)}|All Files (*.*)|*.*"
  176. };
  177. if (saveFileDialog.ShowDialog() == true)
  178. {
  179. // 3. 直接复制文件
  180. File.Copy(logPath, saveFileDialog.FileName, true);
  181. // MessageBox.Show("图片导出成功。");
  182. }
  183. }
  184. catch (Exception ex)
  185. {
  186. MessageBox.Show($"导出失败: {ex.Message}");
  187. }
  188. }
  189. public static int ConvertToInt(string input)
  190. {
  191. if (int.TryParse(input, out int result))
  192. {
  193. return result; // 如果转换成功,返回结果
  194. }
  195. else
  196. {
  197. return 0;
  198. //throw new FormatException("The input string is not a valid integer.");
  199. }
  200. }
  201. //////////////////////////////////////////////////////
  202. }
  203. }