Utility.cs 7.5 KB

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