123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318 |
- using Ookii.Dialogs.Wpf;
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Media.Imaging;
- namespace MeterVision
- {
- public static class ThisApp
- {
- public static bool IsDebug = false;
- public static bool IsPermanentLicense = false; //是否为永久有效
- // 静态属性:页面大小选项
- public static readonly int[] PageSizeOptions = new int[] { 10, 20, 50 };
- public const int MaxFileSize = 300 * 1024;
- public const int RequiredWidth = 320;
- public const int RequiredHeight = 240;
- public static bool IsJpgFile(string filePath)
- {
- // 检查文件扩展名是否为 .jpg 或 .jpeg
- string extension = System.IO.Path.GetExtension(filePath).ToLower();
- return extension == ".jpg"; // || extension == ".jpeg";
- }
- public static bool IsBmpFile(string filePath)
- {
- string extension = System.IO.Path.GetExtension(filePath).ToLower();
- return extension == ".bmp"; // || extension == ".jpeg";
- }
- public static bool IsFileSizeValid(string filePath)
- {
- // 获取文件大小
- FileInfo fileInfo = new FileInfo(filePath);
- return fileInfo.Length <= MaxFileSize;
- }
- public static bool IsImageDimensionsValid(string filePath)
- {
- try
- {
- // 加载图像并检查尺寸
- using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
- {
- BitmapImage bitmap = new BitmapImage();
- bitmap.BeginInit();
- bitmap.StreamSource = fs;
- bitmap.CacheOption = BitmapCacheOption.OnLoad;
- bitmap.EndInit();
- // 检查图像的宽度和高度
- return bitmap.PixelWidth == RequiredWidth && bitmap.PixelHeight == RequiredHeight;
- }
- }
- catch (Exception ex)
- {
- Console.WriteLine(ex.Message);
- // MessageBox.Show($"无法读取图像尺寸: {ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
- return false;
- }
- }
- public static string GetNowTime_yyyyMMddHHmmss()
- {
- DateTime now = DateTime.Now;
- return now.ToString("yyyyMMddHHmmss");
- }
- public static string GetNowTime_yyyyMMdd()
- {
- DateTime now = DateTime.Now;
- return now.ToString("yyyyMMdd");
- }
- public static string ConvertDateFormat(string originalDateTime)
- {
- // 定义原始格式
- string originalFormat = "yyyyMMddHHmmss";
- // 尝试将原始字符串解析为DateTime对象
- if (DateTime.TryParseExact(originalDateTime, originalFormat,
- System.Globalization.CultureInfo.InvariantCulture,
- System.Globalization.DateTimeStyles.None,
- out DateTime parsedDate))
- {
- // 成功解析后,将其格式化为目标格式
- return parsedDate.ToString("yyyy-MM-dd HH:mm:ss");
- }
- else
- {
- // 如果解析失败,可以返回一个默认值或者抛出异常
- //throw new ArgumentException("无法解析提供的日期时间字符串。", nameof(originalDateTime));
- //return string.Empty;
- return originalDateTime;
- }
- }
- public static string ConvertDateFormat_Date(string originalDateTime)
- {
- // 定义原始格式
- string originalFormat = "yyyyMMddHHmmss";
- // 尝试将原始字符串解析为DateTime对象
- if (DateTime.TryParseExact(originalDateTime, originalFormat,
- System.Globalization.CultureInfo.InvariantCulture,
- System.Globalization.DateTimeStyles.None,
- out DateTime parsedDate))
- {
- // 成功解析后,将其格式化为目标格式
- return parsedDate.ToString("yyyy-MM-dd");
- }
- else
- {
- // 如果解析失败,可以返回一个默认值或者抛出异常
- //throw new ArgumentException("无法解析提供的日期时间字符串。", nameof(originalDateTime));
- return string.Empty;
- }
- }
- public static string ConvertDateFormat_Time(string originalDateTime)
- {
- // 定义原始格式
- string originalFormat = "yyyyMMddHHmmss";
- // 尝试将原始字符串解析为DateTime对象
- if (DateTime.TryParseExact(originalDateTime, originalFormat,
- System.Globalization.CultureInfo.InvariantCulture,
- System.Globalization.DateTimeStyles.None,
- out DateTime parsedDate))
- {
- // 成功解析后,将其格式化为目标格式
- return parsedDate.ToString("HH:mm:ss");
- }
- else
- {
- // 如果解析失败,可以返回一个默认值或者抛出异常
- //throw new ArgumentException("无法解析提供的日期时间字符串。", nameof(originalDateTime));
- return string.Empty;
- }
- }
- public static string ConvertDateFormat_Date2(string originalDateTime)
- {
- // 定义原始格式
- string originalFormat = "yyyy-MM-dd HH:mm:ss";
- // 尝试将原始字符串解析为DateTime对象
- if (DateTime.TryParseExact(originalDateTime, originalFormat,
- System.Globalization.CultureInfo.InvariantCulture,
- System.Globalization.DateTimeStyles.None,
- out DateTime parsedDate))
- {
- // 成功解析后,将其格式化为目标格式
- return parsedDate.ToString("yyyy-MM-dd");
- }
- else
- {
- // 如果解析失败,可以返回一个默认值或者抛出异常
- //throw new ArgumentException("无法解析提供的日期时间字符串。", nameof(originalDateTime));
- return string.Empty;
- }
- }
- public static string ConvertDateFormat_Time2(string originalDateTime)
- {
- // 定义原始格式
- string originalFormat = "yyyy-MM-dd HH:mm:ss";
- // 尝试将原始字符串解析为DateTime对象
- if (DateTime.TryParseExact(originalDateTime, originalFormat,
- System.Globalization.CultureInfo.InvariantCulture,
- System.Globalization.DateTimeStyles.None,
- out DateTime parsedDate))
- {
- // 成功解析后,将其格式化为目标格式
- return parsedDate.ToString("HH:mm:ss");
- }
- else
- {
- // 如果解析失败,可以返回一个默认值或者抛出异常
- //throw new ArgumentException("无法解析提供的日期时间字符串。", nameof(originalDateTime));
- return string.Empty;
- }
- }
- public static string GetFileNameWithoutExtension(string filePath)
- {
- if (string.IsNullOrEmpty(filePath))
- {
- //throw new ArgumentNullException(nameof(filePath), "文件路径不能为空");
- return string.Empty;
- }
- return Path.GetFileNameWithoutExtension(filePath);
- }
- //删除文件
- public static void DeleteFile(string filePath)
- {
- if (File.Exists(filePath))
- {
- try
- {
- File.Delete(filePath);
- //MessageBox.Show("文件已成功删除!");
- }
- catch (Exception ex)
- {
- Console.WriteLine(ex.Message);
- //MessageBox.Show($"删除文件时出错: {ex.Message}");
- }
- }
- else
- {
- //MessageBox.Show("文件不存在!");
- }
- }
- //导出AI日志文件
- public static void ExportAiLog(string logPath)
- {
- try
- {
- if (string.IsNullOrEmpty(logPath) || !File.Exists(logPath))
- {
- MessageBox.Show("无法确定原始文件路径或文件不存在。");
- return;
- }
- // 2. 弹出保存对话框
- VistaSaveFileDialog saveFileDialog = new VistaSaveFileDialog
- {
- FileName = Path.GetFileName(logPath), // 默认文件名
- DefaultExt = Path.GetExtension(logPath),
- Filter = $"AiLog Files (*{Path.GetExtension(logPath)})|*{Path.GetExtension(logPath)}|All Files (*.*)|*.*"
- };
- if (saveFileDialog.ShowDialog() == true)
- {
- // 3. 直接复制文件
- File.Copy(logPath, saveFileDialog.FileName, true);
- // MessageBox.Show("图片导出成功。");
- }
- }
- catch (Exception ex)
- {
- MessageBox.Show($"导出失败: {ex.Message}");
- }
- }
- public static int ConvertToInt(string input)
- {
- if (int.TryParse(input, out int result))
- {
- return result; // 如果转换成功,返回结果
- }
- else
- {
- return 0;
- //throw new FormatException("The input string is not a valid integer.");
- }
- }
- //去除数组中的88,替换为""
- public static string GetDebugInfos(byte[] infoBytes)
- {
- if(infoBytes == null || infoBytes.Length != 24)
- {
- return "";
- }
- //List<string> infoList = new List<string>();
- string[] infos = new string[24];
- for (int i = 0; i < 24; i++)
- {
- if (i < 12 && infoBytes[i] == 88 && infoBytes[i + 12] == 88)
- {
- //infoList.Insert(i, "");
- //infoList.Insert(i + 12, "");
- infos[i] = "";
- //infos[i + 12] = "";
- continue;
- }
- else if (i > 12 && infoBytes[i] == 88 && infoBytes[i - 12] == 88)
- {
- infos[i] = "";
- }
- else
- {
- infos[i] = infoBytes[i].ToString();
- }
- if (infos[i].Length == 1)
- {
- infos[i] = "0" + infos[i];
- }
- //infoList.Insert(i, infoBytes[i].ToString());
- }
- return string.Join(",", infos);
-
- }
- //////////////////////////////////////////////////////
- }
- }
|