using AKSWebBrowser.Commen; using CPF.Controls; using System; using System.Drawing; using System.Globalization; using System.IO; using System.Threading.Tasks; namespace AksWebBrowser.Common { public class Utils { /// /// 消息弹框 /// /// public static void MessagesBox(string mes) { MessageBox.ShowSync(mes); } /// /// base64转文件 /// /// /// public static void Base64StringToFile(string base64String, string filePath) { // 将base64字符串转换为字节数组 byte[] wavBytes = Convert.FromBase64String(base64String); // 将字节数组写入到wav文件 using (FileStream fileStream = new FileStream(filePath, FileMode.Create, FileAccess.Write)) { fileStream.Write(wavBytes, 0, wavBytes.Length); fileStream.Flush(); fileStream.Close(); } } //删除目录下文件 public static void DeleteAllFiles(string folderPath) { // 确保文件夹路径是完整的,并且文件夹存在 if (!Directory.Exists(folderPath)) { //Log.Error("未找到删除临时目录"); } // 获取文件夹内所有文件,包括子文件夹内的文件 string[] files = Directory.GetFiles(folderPath, "*.*", SearchOption.AllDirectories); // 删除所有文件 foreach (string file in files) { if (!file.Contains("SignFile")) { File.Delete(file); } } } /// /// 获取运行路径 /// /// public static string getSystemPaht() { string dirpath = AppDomain.CurrentDomain.BaseDirectory; string[] array = dirpath.Split("/"); int o = 0; foreach (string file in array) { if (file.Contains("aks")) { o = o + 1; } } if (o == 0) { dirpath = dirpath + "/aks/aks"; } else if (o == 1) { dirpath = dirpath + "/aks"; } return dirpath; } /// /// 获取时间戳:10位,到秒 /// public static string GetTimestamp() { DateTime currentTime = DateTime.Now; DateTime epochTime = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); TimeSpan timeSpan = currentTime.ToUniversalTime() - epochTime; long timestamp = (long)timeSpan.TotalSeconds; return timestamp.ToString(); } //图片旋转 public static string ImagesByRotate(string base64, int rotate) { string time = DateTime.Now.ToString("yyyyMMddHHmmss", DateTimeFormatInfo.InvariantInfo); //生成图片 string dirpath1 = Utils.getSystemPaht() + @"/wwwroot/TmpFile"; if (!Directory.Exists(dirpath1)) { Directory.CreateDirectory(dirpath1); } var outImgpath = dirpath1 + $"/{time}.jpg"; var Imgpath = Base64ByImages(base64); string command = $"convert {Imgpath} -rotate {rotate} {outImgpath}"; MainModel.ShllCommad(command); byte[] bytes = File.ReadAllBytes(outImgpath); string _base64 = Convert.ToBase64String(bytes); //删除图片 Task.Run(() => { //File.Delete(Imgpath); //File.Delete(outImgpath); }); return _base64; } //base64转图片 public static string Base64ByImages(string base64) { string time = DateTime.Now.ToString("yyyyMMddHHmmss", DateTimeFormatInfo.InvariantInfo); //生成文件 string dirpath = Utils.getSystemPaht() + @"/wwwroot/TmpFile"; if (!Directory.Exists(dirpath)) { Directory.CreateDirectory(dirpath); } var tempFile = dirpath + $"/{time}.txt"; using (StreamWriter sw = new StreamWriter(tempFile)) { sw.Write(base64); sw.Close(); sw.Dispose(); } //生成图片 string dirpath1 = Utils.getSystemPaht() + @"/wwwroot/TmpFile"; if (!Directory.Exists(dirpath1)) { Directory.CreateDirectory(dirpath1); } var Imgpath = dirpath1 + $"/{time}.jpg"; string command = $"base64 -d {tempFile} > {Imgpath}"; MainModel.ShllCommad(command); //删除图片 Task.Run(() => { File.Delete(tempFile); }); return Imgpath; } //base64转图片 public static string Base64ByImagesPath(string base64, string filename) { //生成文件 string dirpath = Utils.getSystemPaht() + @"/wwwroot/TmpFile"; if (!Directory.Exists(dirpath)) { Directory.CreateDirectory(dirpath); } var tempFile = dirpath + $"/{filename}.txt"; using (StreamWriter sw = new StreamWriter(tempFile)) { sw.Write(base64); sw.Close(); sw.Dispose(); } //生成图片 string dirpath1 = Utils.getSystemPaht() + @"/wwwroot/TmpFile"; if (!Directory.Exists(dirpath1)) { Directory.CreateDirectory(dirpath1); } var Imgpath = dirpath1 + $"/{filename}.jpg"; string command = $"base64 -d {tempFile} > {Imgpath}"; MainModel.ShllCommad(command); //删除图片 Task.Run(() => { File.Delete(tempFile); }); return Imgpath; } /// /// 文字分段 /// /// /// /// public static string[] SplitTextIntoSegments(string text, int segmentLength) { int segmentsCount = (int)Math.Ceiling((double)text.Length / segmentLength); string[] segments = new string[segmentsCount]; for (int i = 0; i < segmentsCount; i++) { int startIndex = i * segmentLength; int length = Math.Min(segmentLength, text.Length - startIndex); segments[i] = text.Substring(startIndex, length); } return segments; } /// /// 两个数有余数加1 /// /// /// /// public static int AddOneIfRemainder(int number1, int number2) { decimal num = (decimal)number1 / number2; decimal fraction = num - Math.Floor(num); return Convert.ToInt32(num + (fraction > 0 ? 1 : 0)); } } }