Utility.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Windows;
  5. using System.Windows.Controls;
  6. using System.Windows.Media;
  7. using System.Windows.Media.Imaging;
  8. namespace Phihong_EVSE_UI_Tool
  9. {
  10. public static class Utility
  11. {
  12. public const string BG_PARENTFOLDER = "Background";
  13. public const string BG_INITIAL = "0_init.bmp";
  14. public const string BG_IDLE = "1_idle.bmp";
  15. public const string BG_VERIFY = "2_verify.bmp";
  16. public const string BG_VERIFYOK = "3_carChkok.bmp";
  17. public const string BG_VERIFYFAIL = "4_chkfail.bmp";
  18. public const string BG_PLUG = "5_plug.bmp";
  19. public const string BG_PRECHARGE = "6_precharge.bmp";
  20. public const string BG_CHARGING = "7_charging.bmp";
  21. public const string BG_COMPLETE = "8_complete.bmp";
  22. public const string BG_MAINTAIN = "9_fix.bmp";
  23. public const string ICO_PARENTFOLDER = "ICO";
  24. public const string ICO_NAME = "60.ICO";
  25. public const string ICO_MAINLOGO = "10.bmp";
  26. public const string ICO_COMPLETELOGO = "62.bmp";
  27. public const string REQ_PARENTFOLDER = "Required";
  28. public static List<string> REQ_FILELIST = new List<string>()
  29. {
  30. "0_DWIN_ASC.HZK", "12_HZK.BIN", "13触控配置文件.bin", "14变量配置文件.bin", "22_Config.bin",
  31. "24_Unicode_Roboto_18_22.dzk", "26_Unicode_Roboto_36_44.dzk", "28_Unicode_Roboto_28_56.dzk",
  32. "30_Unicode_Roboto_14_28.dzk", "32_Unicode_Roboto_28_56.dzk", "CONFIG.txt"
  33. };
  34. public const string SDCARD_SUBDIR = "DWIN_SET";
  35. public static readonly string OUTPUT_DIRECTORY = Environment.CurrentDirectory + @"\Output";
  36. public static readonly string CUSTOM_DIRECTORY = Environment.CurrentDirectory + @"\CustomTemp";
  37. public static Dictionary<string, bool> ChangedFiles = new Dictionary<string, bool>()
  38. {
  39. {BG_INITIAL, false}, {BG_IDLE, false}, {BG_VERIFY, false},
  40. {BG_VERIFYOK, false}, {BG_VERIFYFAIL, false}, {BG_PLUG, false},
  41. {BG_PRECHARGE, false}, {BG_CHARGING, false}, {BG_COMPLETE, false},
  42. {BG_MAINTAIN, false}, {ICO_NAME, false}
  43. };
  44. public static void CopyFileFromResource(Uri uriString, string destPath)
  45. {
  46. using (var resource = Application.GetResourceStream(uriString).Stream)
  47. {
  48. using (var file = new FileStream(destPath, FileMode.Create, FileAccess.Write))
  49. {
  50. resource.CopyTo(file);
  51. }
  52. }
  53. }
  54. public static void LoadImageFromPath(Image imgControl, string path)
  55. {
  56. FileStream fstream = new FileStream(path, FileMode.Open);
  57. BitmapImage bitmap = new BitmapImage();
  58. bitmap.BeginInit();
  59. bitmap.StreamSource = fstream;
  60. bitmap.CacheOption = BitmapCacheOption.OnLoad;
  61. bitmap.EndInit();
  62. fstream.Close();
  63. imgControl.BeginInit();
  64. imgControl.Source = bitmap;
  65. imgControl.EndInit();
  66. }
  67. public static bool IsBackgroundImageSizeQualified(string path)
  68. {
  69. FileStream fstream = new FileStream(path, FileMode.Open);
  70. BitmapImage bitmap = new BitmapImage();
  71. bitmap.BeginInit();
  72. bitmap.StreamSource = fstream;
  73. bitmap.CacheOption = BitmapCacheOption.OnLoad;
  74. bitmap.EndInit();
  75. fstream.Close();
  76. if (bitmap.PixelWidth != 800 || bitmap.PixelHeight != 480)
  77. {
  78. return false;
  79. }
  80. return true;
  81. }
  82. public static bool IsIcoImageSizeQualified(string path)
  83. {
  84. FileStream fstream = new FileStream(path, FileMode.Open);
  85. BitmapImage bitmap = new BitmapImage();
  86. bitmap.BeginInit();
  87. bitmap.StreamSource = fstream;
  88. bitmap.CacheOption = BitmapCacheOption.OnLoad;
  89. bitmap.EndInit();
  90. fstream.Close();
  91. if (bitmap.PixelWidth > 255 || bitmap.PixelHeight > 255)
  92. {
  93. return false;
  94. }
  95. return true;
  96. }
  97. public static bool IsIcoLogoSizeQualified(string path)
  98. {
  99. FileStream fstream = new FileStream(path, FileMode.Open);
  100. BitmapImage bitmap = new BitmapImage();
  101. bitmap.BeginInit();
  102. bitmap.StreamSource = fstream;
  103. bitmap.CacheOption = BitmapCacheOption.OnLoad;
  104. bitmap.EndInit();
  105. fstream.Close();
  106. if (bitmap.PixelWidth != 255 || bitmap.PixelHeight != 34)
  107. {
  108. return false;
  109. }
  110. return true;
  111. }
  112. public static void ConvertFileToBitmap565Array(string path, out int width, out int height, out byte[] pixels)
  113. {
  114. FileStream fstream = new FileStream(path, FileMode.Open);
  115. ConvertToBitmap565(out width, out height, out pixels, fstream);
  116. }
  117. public static void ConvertResourceToBitmap565Array(Uri uriString, out int width, out int height, out byte[] pixels)
  118. {
  119. using (var resource = Application.GetResourceStream(uriString).Stream)
  120. {
  121. ConvertToBitmap565(out width, out height, out pixels, resource);
  122. }
  123. }
  124. private static void ConvertToBitmap565(out int width, out int height, out byte[] pixels, Stream fstream)
  125. {
  126. BitmapImage bitmap = new BitmapImage();
  127. bitmap.BeginInit();
  128. bitmap.StreamSource = fstream;
  129. bitmap.CacheOption = BitmapCacheOption.OnLoad;
  130. bitmap.EndInit();
  131. fstream.Close();
  132. FormatConvertedBitmap newFormated = new FormatConvertedBitmap();
  133. newFormated.BeginInit();
  134. newFormated.Source = bitmap;
  135. newFormated.DestinationFormat = PixelFormats.Bgr565;
  136. newFormated.EndInit();
  137. width = newFormated.PixelWidth;
  138. height = newFormated.PixelHeight;
  139. var stride = (newFormated.PixelWidth * newFormated.Format.BitsPerPixel + 7) / 8;
  140. pixels = new byte[newFormated.PixelHeight * stride];
  141. newFormated.CopyPixels(pixels, stride, 0);
  142. for (int i = 0; i < pixels.Length; i += 2)
  143. {
  144. byte temp = pixels[i];
  145. pixels[i] = pixels[i + 1];
  146. pixels[i + 1] = temp;
  147. }
  148. }
  149. }
  150. }