Advertisement
Guest User

Untitled

a guest
Oct 16th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.16 KB | None | 0 0
  1. class Utils
  2. {
  3. public static byte[] UNICODE_TEXT = new byte[] {0x23, 0x23, 0x23,
  4. 0x23, 0x23, 0x23,0x23, 0x23, 0x23,0x23, 0x23, 0x23,0x23, 0x23, 0x23,
  5. 0x23, 0x23, 0x23,0x23, 0x23, 0x23,0x23, 0x23, 0x23,0x23, 0x23, 0x23,
  6. 0x23, 0x23, 0x23};
  7.  
  8. private static string hexStr = "0123456789ABCDEF";
  9. private static string[] binaryArray = { "0000", "0001", "0010", "0011",
  10. "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011",
  11. "1100", "1101", "1110", "1111" };
  12.  
  13. public static byte[] decodeBitmap(Bitmap bmp)
  14. {
  15. int bmpWidth = bmp.Width;
  16. int bmpHeight = bmp.Height;
  17.  
  18. List<string> list = new List<string>(); //binaryString list
  19. StringBuffer sb;
  20.  
  21.  
  22. int bitLen = bmpWidth / 8;
  23. int zeroCount = bmpWidth % 8;
  24.  
  25. string zeroStr = "";
  26. if (zeroCount > 0)
  27. {
  28. bitLen = bmpWidth / 8 + 1;
  29. for (int i = 0; i < (8 - zeroCount); i++)
  30. {
  31. zeroStr = zeroStr + "0";
  32. }
  33. }
  34.  
  35. for (int i = 0; i < bmpHeight; i++)
  36. {
  37. sb = new StringBuffer();
  38. for (int j = 0; j < bmpWidth; j++)
  39. {
  40. int color = bmp.GetPixel(j, i);
  41.  
  42. int r = (color >> 16) & 0xff;
  43. int g = (color >> 8) & 0xff;
  44. int b = color & 0xff;
  45.  
  46. // if color close to white,bit='0', else bit='1'
  47. if (r > 160 && g > 160 && b > 160)
  48. sb.Append("0");
  49. else
  50. sb.Append("1");
  51. }
  52. if (zeroCount > 0)
  53. {
  54. sb.Append(zeroStr);
  55. }
  56. list.Add(sb.ToString());
  57. }
  58.  
  59. List<string> bmpHexList = binaryListToHexStringList(list);
  60. string commandHexString = "1D763000";
  61. string widthHexString = Integer
  62. .ToHexString(bmpWidth % 8 == 0 ? bmpWidth / 8
  63. : (bmpWidth / 8 + 1));
  64. if (widthHexString.Length > 2)
  65. {
  66. Console.Write("decodeBitmap error", " width is too large");
  67. return null;
  68. }
  69. else if (widthHexString.Length == 1)
  70. {
  71. widthHexString = "0" + widthHexString;
  72. }
  73. widthHexString = widthHexString + "00";
  74.  
  75. string heightHexString = Integer.ToHexString(bmpHeight);
  76. if (heightHexString.Length > 2)
  77. {
  78. Console.Write("decodeBitmap error", " height is too large");
  79. return null;
  80. }
  81. else if (heightHexString.Length == 1)
  82. {
  83. heightHexString = "0" + heightHexString;
  84. }
  85. heightHexString = heightHexString + "00";
  86.  
  87. List<string> commandList = new List<string>();
  88. commandList.Add(commandHexString + widthHexString + heightHexString);
  89. commandList.AddRange(bmpHexList);
  90.  
  91. return hexList2Byte(commandList);
  92. }
  93.  
  94. public static List<string> binaryListToHexStringList(List<string> list)
  95. {
  96. List<string> hexList = new List<string>();
  97. foreach (string binaryStr in list)
  98. {
  99. StringBuffer sb = new StringBuffer();
  100. for (int i = 0; i < binaryStr.Length; i += 8)
  101. {
  102. string str = binaryStr.Substring(i, 8);
  103.  
  104. string hexString = myBinaryStrToHexString(str);
  105. sb.Append(hexString);
  106. }
  107. hexList.Add(sb.ToString());
  108. }
  109. return hexList;
  110.  
  111. }
  112.  
  113. public static byte[] hexList2Byte(List<string> list)
  114. {
  115. List<byte[]> commandList = new List<byte[]>();
  116.  
  117. foreach (string hexStr in list)
  118. {
  119. commandList.Add(hexStringToBytes(hexStr));
  120. }
  121. byte[] bytes = sysCopy(commandList);
  122. return bytes;
  123. }
  124.  
  125.  
  126. public static byte[] hexStringToBytes(string hexString)
  127. {
  128. if (hexString == null || hexString.Equals(""))
  129. {
  130. return null;
  131. }
  132. hexString = hexString.ToUpper();
  133. int length = hexString.Length / 2;
  134. char[] hexChars = hexString.ToCharArray();
  135. byte[] d = new byte[length];
  136. for (int i = 0; i < length; i++)
  137. {
  138. int pos = i * 2;
  139. d[i] = (byte)(charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
  140. }
  141. return d;
  142. }
  143.  
  144. public static string myBinaryStrToHexString(string binaryStr)
  145. {
  146. string hex = "";
  147. string f4 = binaryStr.Substring(0, 4);
  148. string b4 = binaryStr.Substring(4, 4);
  149. for (int i = 0; i < binaryArray.Length; i++)
  150. {
  151. if (f4.Equals(binaryArray[i]))
  152. hex += hexStr.Substring(i, 1);
  153. }
  154. for (int i = 0; i < binaryArray.Length; i++)
  155. {
  156. if (b4.Equals(binaryArray[i]))
  157. hex += hexStr.Substring(i, 1);
  158. }
  159.  
  160. return hex;
  161. }
  162.  
  163.  
  164. public static byte[] sysCopy(List<byte[]> srcArrays)
  165. {
  166. int len = 0;
  167. foreach (byte[] srcArray in srcArrays)
  168. {
  169. len += srcArray.Length;
  170. }
  171. byte[] destArray = new byte[len];
  172. int destLen = 0;
  173. foreach (byte[] srcArray in srcArrays)
  174. {
  175. Array.Copy(srcArray, 0, destArray, destLen, srcArray.Length);
  176. destLen += srcArray.Length;
  177. }
  178. return destArray;
  179. }
  180.  
  181.  
  182. private static byte charToByte(char c)
  183. {
  184. return (byte)"0123456789ABCDEF".IndexOf(c);
  185. }
  186. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement