ThisApp.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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. Console.WriteLine(ex.Message);
  57. // MessageBox.Show($"无法读取图像尺寸: {ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
  58. return false;
  59. }
  60. }
  61. public static string GetNowTime_yyyyMMddHHmmss()
  62. {
  63. DateTime now = DateTime.Now;
  64. return now.ToString("yyyyMMddHHmmss");
  65. }
  66. public static string GetNowTime_yyyyMMdd()
  67. {
  68. DateTime now = DateTime.Now;
  69. return now.ToString("yyyyMMdd");
  70. }
  71. public static string ConvertDateFormat(string originalDateTime)
  72. {
  73. // 定义原始格式
  74. string originalFormat = "yyyyMMddHHmmss";
  75. // 尝试将原始字符串解析为DateTime对象
  76. if (DateTime.TryParseExact(originalDateTime, originalFormat,
  77. System.Globalization.CultureInfo.InvariantCulture,
  78. System.Globalization.DateTimeStyles.None,
  79. out DateTime parsedDate))
  80. {
  81. // 成功解析后,将其格式化为目标格式
  82. return parsedDate.ToString("yyyy-MM-dd HH:mm:ss");
  83. }
  84. else
  85. {
  86. // 如果解析失败,可以返回一个默认值或者抛出异常
  87. //throw new ArgumentException("无法解析提供的日期时间字符串。", nameof(originalDateTime));
  88. //return string.Empty;
  89. return originalDateTime;
  90. }
  91. }
  92. public static string ConvertDateFormat_Date(string originalDateTime)
  93. {
  94. // 定义原始格式
  95. string originalFormat = "yyyyMMddHHmmss";
  96. // 尝试将原始字符串解析为DateTime对象
  97. if (DateTime.TryParseExact(originalDateTime, originalFormat,
  98. System.Globalization.CultureInfo.InvariantCulture,
  99. System.Globalization.DateTimeStyles.None,
  100. out DateTime parsedDate))
  101. {
  102. // 成功解析后,将其格式化为目标格式
  103. return parsedDate.ToString("yyyy-MM-dd");
  104. }
  105. else
  106. {
  107. // 如果解析失败,可以返回一个默认值或者抛出异常
  108. //throw new ArgumentException("无法解析提供的日期时间字符串。", nameof(originalDateTime));
  109. return string.Empty;
  110. }
  111. }
  112. public static string ConvertDateFormat_Time(string originalDateTime)
  113. {
  114. // 定义原始格式
  115. string originalFormat = "yyyyMMddHHmmss";
  116. // 尝试将原始字符串解析为DateTime对象
  117. if (DateTime.TryParseExact(originalDateTime, originalFormat,
  118. System.Globalization.CultureInfo.InvariantCulture,
  119. System.Globalization.DateTimeStyles.None,
  120. out DateTime parsedDate))
  121. {
  122. // 成功解析后,将其格式化为目标格式
  123. return parsedDate.ToString("HH:mm:ss");
  124. }
  125. else
  126. {
  127. // 如果解析失败,可以返回一个默认值或者抛出异常
  128. //throw new ArgumentException("无法解析提供的日期时间字符串。", nameof(originalDateTime));
  129. return string.Empty;
  130. }
  131. }
  132. public static string ConvertDateFormat_Date2(string originalDateTime)
  133. {
  134. // 定义原始格式
  135. string originalFormat = "yyyy-MM-dd HH:mm:ss";
  136. // 尝试将原始字符串解析为DateTime对象
  137. if (DateTime.TryParseExact(originalDateTime, originalFormat,
  138. System.Globalization.CultureInfo.InvariantCulture,
  139. System.Globalization.DateTimeStyles.None,
  140. out DateTime parsedDate))
  141. {
  142. // 成功解析后,将其格式化为目标格式
  143. return parsedDate.ToString("yyyy-MM-dd");
  144. }
  145. else
  146. {
  147. // 如果解析失败,可以返回一个默认值或者抛出异常
  148. //throw new ArgumentException("无法解析提供的日期时间字符串。", nameof(originalDateTime));
  149. return string.Empty;
  150. }
  151. }
  152. public static string ConvertDateFormat_Time2(string originalDateTime)
  153. {
  154. // 定义原始格式
  155. string originalFormat = "yyyy-MM-dd HH:mm:ss";
  156. // 尝试将原始字符串解析为DateTime对象
  157. if (DateTime.TryParseExact(originalDateTime, originalFormat,
  158. System.Globalization.CultureInfo.InvariantCulture,
  159. System.Globalization.DateTimeStyles.None,
  160. out DateTime parsedDate))
  161. {
  162. // 成功解析后,将其格式化为目标格式
  163. return parsedDate.ToString("HH:mm:ss");
  164. }
  165. else
  166. {
  167. // 如果解析失败,可以返回一个默认值或者抛出异常
  168. //throw new ArgumentException("无法解析提供的日期时间字符串。", nameof(originalDateTime));
  169. return string.Empty;
  170. }
  171. }
  172. public static string GetFileNameWithoutExtension(string filePath)
  173. {
  174. if (string.IsNullOrEmpty(filePath))
  175. {
  176. //throw new ArgumentNullException(nameof(filePath), "文件路径不能为空");
  177. return string.Empty;
  178. }
  179. return Path.GetFileNameWithoutExtension(filePath);
  180. }
  181. //删除文件
  182. public static void DeleteFile(string filePath)
  183. {
  184. if (File.Exists(filePath))
  185. {
  186. try
  187. {
  188. File.Delete(filePath);
  189. //MessageBox.Show("文件已成功删除!");
  190. }
  191. catch (Exception ex)
  192. {
  193. Console.WriteLine(ex.Message);
  194. //MessageBox.Show($"删除文件时出错: {ex.Message}");
  195. }
  196. }
  197. else
  198. {
  199. //MessageBox.Show("文件不存在!");
  200. }
  201. }
  202. //导出AI日志文件
  203. public static void ExportAiLog(string logPath)
  204. {
  205. try
  206. {
  207. if (string.IsNullOrEmpty(logPath) || !File.Exists(logPath))
  208. {
  209. MessageBox.Show("无法确定原始文件路径或文件不存在。");
  210. return;
  211. }
  212. // 2. 弹出保存对话框
  213. VistaSaveFileDialog saveFileDialog = new VistaSaveFileDialog
  214. {
  215. FileName = Path.GetFileName(logPath), // 默认文件名
  216. DefaultExt = Path.GetExtension(logPath),
  217. Filter = $"AiLog Files (*{Path.GetExtension(logPath)})|*{Path.GetExtension(logPath)}|All Files (*.*)|*.*"
  218. };
  219. if (saveFileDialog.ShowDialog() == true)
  220. {
  221. // 3. 直接复制文件
  222. File.Copy(logPath, saveFileDialog.FileName, true);
  223. // MessageBox.Show("图片导出成功。");
  224. }
  225. }
  226. catch (Exception ex)
  227. {
  228. MessageBox.Show($"导出失败: {ex.Message}");
  229. }
  230. }
  231. public static int ConvertToInt(string input)
  232. {
  233. if (int.TryParse(input, out int result))
  234. {
  235. return result; // 如果转换成功,返回结果
  236. }
  237. else
  238. {
  239. return 0;
  240. //throw new FormatException("The input string is not a valid integer.");
  241. }
  242. }
  243. //去除数组中的88,替换为""
  244. public static string GetDebugInfos(byte[] infoBytes)
  245. {
  246. if(infoBytes == null || infoBytes.Length != 24)
  247. {
  248. return "";
  249. }
  250. //List<string> infoList = new List<string>();
  251. string[] infos = new string[24];
  252. for (int i = 0; i < 24; i++)
  253. {
  254. if (i < 12 && infoBytes[i] == 88 && infoBytes[i + 12] == 88)
  255. {
  256. //infoList.Insert(i, "");
  257. //infoList.Insert(i + 12, "");
  258. infos[i] = "";
  259. //infos[i + 12] = "";
  260. continue;
  261. }
  262. else if (i > 12 && infoBytes[i] == 88 && infoBytes[i - 12] == 88)
  263. {
  264. infos[i] = "";
  265. }
  266. else
  267. {
  268. infos[i] = infoBytes[i].ToString();
  269. }
  270. if (infos[i].Length == 1)
  271. {
  272. infos[i] = "0" + infos[i];
  273. }
  274. //infoList.Insert(i, infoBytes[i].ToString());
  275. }
  276. return string.Join(",", infos);
  277. }
  278. //////////////////////////////////////////////////////
  279. }
  280. }