ThisApp.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. return originalDateTime;
  89. }
  90. }
  91. public static string ConvertDateFormat_Date(string originalDateTime)
  92. {
  93. // 定义原始格式
  94. string originalFormat = "yyyyMMddHHmmss";
  95. // 尝试将原始字符串解析为DateTime对象
  96. if (DateTime.TryParseExact(originalDateTime, originalFormat,
  97. System.Globalization.CultureInfo.InvariantCulture,
  98. System.Globalization.DateTimeStyles.None,
  99. out DateTime parsedDate))
  100. {
  101. // 成功解析后,将其格式化为目标格式
  102. return parsedDate.ToString("yyyy-MM-dd");
  103. }
  104. else
  105. {
  106. // 如果解析失败,可以返回一个默认值或者抛出异常
  107. //throw new ArgumentException("无法解析提供的日期时间字符串。", nameof(originalDateTime));
  108. return string.Empty;
  109. }
  110. }
  111. public static string ConvertDateFormat_Time(string originalDateTime)
  112. {
  113. // 定义原始格式
  114. string originalFormat = "yyyyMMddHHmmss";
  115. // 尝试将原始字符串解析为DateTime对象
  116. if (DateTime.TryParseExact(originalDateTime, originalFormat,
  117. System.Globalization.CultureInfo.InvariantCulture,
  118. System.Globalization.DateTimeStyles.None,
  119. out DateTime parsedDate))
  120. {
  121. // 成功解析后,将其格式化为目标格式
  122. return parsedDate.ToString("HH:mm:ss");
  123. }
  124. else
  125. {
  126. // 如果解析失败,可以返回一个默认值或者抛出异常
  127. //throw new ArgumentException("无法解析提供的日期时间字符串。", nameof(originalDateTime));
  128. return string.Empty;
  129. }
  130. }
  131. public static string GetFileNameWithoutExtension(string filePath)
  132. {
  133. if (string.IsNullOrEmpty(filePath))
  134. {
  135. //throw new ArgumentNullException(nameof(filePath), "文件路径不能为空");
  136. return string.Empty;
  137. }
  138. return Path.GetFileNameWithoutExtension(filePath);
  139. }
  140. //删除文件
  141. public static void DeleteFile(string filePath)
  142. {
  143. if (File.Exists(filePath))
  144. {
  145. try
  146. {
  147. File.Delete(filePath);
  148. //MessageBox.Show("文件已成功删除!");
  149. }
  150. catch (Exception ex)
  151. {
  152. Console.WriteLine(ex.Message);
  153. //MessageBox.Show($"删除文件时出错: {ex.Message}");
  154. }
  155. }
  156. else
  157. {
  158. //MessageBox.Show("文件不存在!");
  159. }
  160. }
  161. //导出AI日志文件
  162. public static void ExportAiLog(string logPath)
  163. {
  164. try
  165. {
  166. if (string.IsNullOrEmpty(logPath) || !File.Exists(logPath))
  167. {
  168. MessageBox.Show("无法确定原始文件路径或文件不存在。");
  169. return;
  170. }
  171. // 2. 弹出保存对话框
  172. VistaSaveFileDialog saveFileDialog = new VistaSaveFileDialog
  173. {
  174. FileName = Path.GetFileName(logPath), // 默认文件名
  175. DefaultExt = Path.GetExtension(logPath),
  176. Filter = $"AiLog Files (*{Path.GetExtension(logPath)})|*{Path.GetExtension(logPath)}|All Files (*.*)|*.*"
  177. };
  178. if (saveFileDialog.ShowDialog() == true)
  179. {
  180. // 3. 直接复制文件
  181. File.Copy(logPath, saveFileDialog.FileName, true);
  182. // MessageBox.Show("图片导出成功。");
  183. }
  184. }
  185. catch (Exception ex)
  186. {
  187. MessageBox.Show($"导出失败: {ex.Message}");
  188. }
  189. }
  190. public static int ConvertToInt(string input)
  191. {
  192. if (int.TryParse(input, out int result))
  193. {
  194. return result; // 如果转换成功,返回结果
  195. }
  196. else
  197. {
  198. return 0;
  199. //throw new FormatException("The input string is not a valid integer.");
  200. }
  201. }
  202. //////////////////////////////////////////////////////
  203. }
  204. }