Utility.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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 BG_EMERGENCY = "10_emc.bmp";
  24. public const string ICO_PARENTFOLDER = "ICO";
  25. public const string ICO_NAME = "60.ICO";
  26. public const string ICO_MAINLOGO = "10.bmp";
  27. public const string ICO_MAINLOGO_HIDE = "10_hide.bmp";
  28. public const string ICO_COMPLETELOGO = "62.bmp";
  29. public const string ICO_COMPLETELOGO_HIDE = "62_hide.bmp";
  30. public const string ICO_URL = "66.bmp";
  31. public const string ICO_URL_HIDE = "66_hide.bmp";
  32. public const string REQ_PARENTFOLDER = "Required";
  33. public static List<string> REQ_FILELIST = new List<string>()
  34. {
  35. "0_DWIN_ASC.HZK", "12_HZK.BIN", "13触控配置文件.bin", "14变量配置文件.bin", "22_Config.bin",
  36. "24_Unicode_Roboto_18_22.dzk", "26_Unicode_Roboto_36_44.dzk", "28_Unicode_Roboto_28_56.dzk",
  37. "30_Unicode_Roboto_14_28.dzk", "32_Unicode_Roboto_28_56.dzk", "CONFIG.txt"
  38. };
  39. public const string SDCARD_SUBDIR = "DWIN_SET";
  40. public static readonly string OUTPUT_DIRECTORY = Environment.CurrentDirectory + @"\Output";
  41. public static readonly string CUSTOM_DIRECTORY = Environment.CurrentDirectory + @"\CustomTemp";
  42. public static Dictionary<string, bool> ChangedFiles = new Dictionary<string, bool>()
  43. {
  44. {BG_INITIAL, false}, {BG_IDLE, false}, {BG_VERIFY, false},
  45. {BG_VERIFYOK, false}, {BG_VERIFYFAIL, false}, {BG_PLUG, false},
  46. {BG_PRECHARGE, false}, {BG_CHARGING, false}, {BG_COMPLETE, false},
  47. {BG_MAINTAIN, false}, {BG_EMERGENCY, false}, {ICO_NAME, false}
  48. };
  49. public static void CopyFileFromResource(Uri uriString, string destPath)
  50. {
  51. using (var resource = Application.GetResourceStream(uriString).Stream)
  52. {
  53. using (var file = new FileStream(destPath, FileMode.Create, FileAccess.Write))
  54. {
  55. resource.CopyTo(file);
  56. }
  57. }
  58. }
  59. public static void LoadImageFromPath(Image imgControl, string path)
  60. {
  61. FileStream fstream = new FileStream(path, FileMode.Open);
  62. BitmapImage bitmap = new BitmapImage();
  63. bitmap.BeginInit();
  64. bitmap.StreamSource = fstream;
  65. bitmap.CacheOption = BitmapCacheOption.OnLoad;
  66. bitmap.EndInit();
  67. fstream.Close();
  68. imgControl.BeginInit();
  69. imgControl.Source = bitmap;
  70. imgControl.EndInit();
  71. }
  72. public static bool IsBackgroundImageSizeQualified(string path)
  73. {
  74. FileStream fstream = new FileStream(path, FileMode.Open);
  75. BitmapImage bitmap = new BitmapImage();
  76. bitmap.BeginInit();
  77. bitmap.StreamSource = fstream;
  78. bitmap.CacheOption = BitmapCacheOption.OnLoad;
  79. bitmap.EndInit();
  80. fstream.Close();
  81. if (bitmap.PixelWidth != 800 || bitmap.PixelHeight != 480)
  82. {
  83. return false;
  84. }
  85. return true;
  86. }
  87. public static bool IsIcoImageSizeQualified(string path)
  88. {
  89. FileStream fstream = new FileStream(path, FileMode.Open);
  90. BitmapImage bitmap = new BitmapImage();
  91. bitmap.BeginInit();
  92. bitmap.StreamSource = fstream;
  93. bitmap.CacheOption = BitmapCacheOption.OnLoad;
  94. bitmap.EndInit();
  95. fstream.Close();
  96. if (bitmap.PixelWidth > 255 || bitmap.PixelHeight > 255)
  97. {
  98. return false;
  99. }
  100. return true;
  101. }
  102. public static bool IsIcoLogoSizeQualified(string path)
  103. {
  104. FileStream fstream = new FileStream(path, FileMode.Open);
  105. BitmapImage bitmap = new BitmapImage();
  106. bitmap.BeginInit();
  107. bitmap.StreamSource = fstream;
  108. bitmap.CacheOption = BitmapCacheOption.OnLoad;
  109. bitmap.EndInit();
  110. fstream.Close();
  111. if (bitmap.PixelWidth != 255 || bitmap.PixelHeight != 34)
  112. {
  113. return false;
  114. }
  115. return true;
  116. }
  117. public static bool IsIcoUrlSizeQualified(string path)
  118. {
  119. FileStream fstream = new FileStream(path, FileMode.Open);
  120. BitmapImage bitmap = new BitmapImage();
  121. bitmap.BeginInit();
  122. bitmap.StreamSource = fstream;
  123. bitmap.CacheOption = BitmapCacheOption.OnLoad;
  124. bitmap.EndInit();
  125. fstream.Close();
  126. if (bitmap.PixelWidth != 233 || bitmap.PixelHeight != 28)
  127. {
  128. return false;
  129. }
  130. return true;
  131. }
  132. public static void ConvertFileToBitmap565Array(string path, out int width, out int height, out byte[] pixels)
  133. {
  134. FileStream fstream = new FileStream(path, FileMode.Open);
  135. ConvertToBitmap565(out width, out height, out pixels, fstream);
  136. }
  137. public static void ConvertResourceToBitmap565Array(Uri uriString, out int width, out int height, out byte[] pixels)
  138. {
  139. using (var resource = Application.GetResourceStream(uriString).Stream)
  140. {
  141. ConvertToBitmap565(out width, out height, out pixels, resource);
  142. }
  143. }
  144. private static void ConvertToBitmap565(out int width, out int height, out byte[] pixels, Stream fstream)
  145. {
  146. BitmapImage bitmap = new BitmapImage();
  147. bitmap.BeginInit();
  148. bitmap.StreamSource = fstream;
  149. bitmap.CacheOption = BitmapCacheOption.OnLoad;
  150. bitmap.EndInit();
  151. fstream.Close();
  152. FormatConvertedBitmap newFormated = new FormatConvertedBitmap();
  153. newFormated.BeginInit();
  154. newFormated.Source = bitmap;
  155. newFormated.DestinationFormat = PixelFormats.Bgr565;
  156. newFormated.EndInit();
  157. width = newFormated.PixelWidth;
  158. height = newFormated.PixelHeight;
  159. var stride = (newFormated.PixelWidth * newFormated.Format.BitsPerPixel + 7) / 8;
  160. pixels = new byte[newFormated.PixelHeight * stride];
  161. newFormated.CopyPixels(pixels, stride, 0);
  162. for (int i = 0; i < pixels.Length; i += 2)
  163. {
  164. byte temp = pixels[i];
  165. pixels[i] = pixels[i + 1];
  166. pixels[i + 1] = temp;
  167. }
  168. }
  169. }
  170. }