AppUtil.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. using CScript.Entity;
  2. using CScript.Net;
  3. using OpenCVForUnity.CoreModule;
  4. using OpenCVForUnity.ImgprocModule;
  5. using System;
  6. using System.Collections;
  7. using System.Collections.Generic;
  8. using System.IO;
  9. using System.Runtime.Serialization;
  10. using System.Runtime.Serialization.Formatters.Binary;
  11. using UnityEngine;
  12. namespace CScript.Utilities
  13. {
  14. public static class AppUtil
  15. {
  16. /// <summary>
  17. ///
  18. /// </summary>
  19. /// <param name="netMeshData"></param>
  20. /// <returns></returns>
  21. public static byte[] ParseData(NetMeshData netMeshData)
  22. {
  23. //1 colorList 标识1个字节 长度两个字节 内容 长度*2个字节
  24. //2 sideVertexList
  25. //3 frontVertexList
  26. //4 uvList
  27. //5 texture
  28. //6 frontTriangles
  29. //7 sideTriangles
  30. int tempInfoLength = 0;
  31. List<byte> infos = new List<byte>();
  32. byte colorSigh = (byte)1;
  33. byte[] colorLength = BitConverter.GetBytes(netMeshData.netColorList.Count);
  34. //List<byte> colorBytes =new List<byte>(netMeshData.colorList);
  35. //infos.Add(colorSigh);
  36. //infos.AddRange(colorLength);
  37. //infos.AddRange(colorBytes);
  38. byte sideVertexSigh = (byte)2;
  39. tempInfoLength = netMeshData.netSideVertexList.Count * 4;
  40. byte[] sideVertexLength = BitConverter.GetBytes(tempInfoLength);
  41. List<byte> sideVertexBytes = new List<byte>(tempInfoLength);
  42. for (int i = 0, count = netMeshData.netSideVertexList.Count; i < count; i++)
  43. {
  44. sideVertexBytes.AddRange(BitConverter.GetBytes(netMeshData.netSideVertexList[i]));
  45. }
  46. infos.Add(sideVertexSigh);
  47. infos.AddRange(sideVertexLength);
  48. infos.AddRange(sideVertexBytes);
  49. byte frontVertexSigh = (byte)3;
  50. tempInfoLength = netMeshData.netFrontVertexList.Count * 4;
  51. byte[] frontVertexLength = BitConverter.GetBytes(tempInfoLength);
  52. List<byte> frontVertexBytes = new List<byte>(tempInfoLength);
  53. for (int i = 0, count = netMeshData.netFrontVertexList.Count; i < count; i++)
  54. {
  55. frontVertexBytes.AddRange(BitConverter.GetBytes(netMeshData.netFrontVertexList[i]));
  56. }
  57. infos.Add(frontVertexSigh);
  58. infos.AddRange(frontVertexLength);
  59. infos.AddRange(frontVertexBytes);
  60. byte uvSigh = (byte)4;
  61. tempInfoLength = netMeshData.netUvList.Count * 4;
  62. byte[] uvLength = BitConverter.GetBytes(tempInfoLength);
  63. List<byte> uvBytes = new List<byte>(tempInfoLength);
  64. for (int i = 0, count = netMeshData.netUvList.Count; i < count; i++)
  65. {
  66. uvBytes.AddRange(BitConverter.GetBytes(netMeshData.netUvList[i]));
  67. }
  68. infos.Add(uvSigh);
  69. infos.AddRange(uvLength);
  70. infos.AddRange(uvBytes);
  71. byte photoSigh = (byte)5;
  72. //byte[] photoByte = netMeshData.texture.EncodeToPNG();
  73. //byte[] photoLength = BitConverter.GetBytes(photoByte.Length);
  74. byte[] photoLength = BitConverter.GetBytes(netMeshData.netTexture.Count);
  75. infos.Add(photoSigh);
  76. infos.AddRange(photoLength);
  77. //infos.AddRange(photoByte);
  78. infos.AddRange(netMeshData.netTexture);
  79. byte frontTriangleSigh = (byte)6;
  80. tempInfoLength = netMeshData.netFrontTriangles.Count * 4;
  81. byte[] frontTriangleLength = BitConverter.GetBytes(tempInfoLength);
  82. List<byte> frontTriangleBytes = new List<byte>(tempInfoLength);
  83. for (int i = 0, count = netMeshData.netFrontTriangles.Count; i < count; i++)
  84. {
  85. frontTriangleBytes.AddRange(BitConverter.GetBytes(netMeshData.netFrontTriangles[i]));
  86. }
  87. infos.Add(frontTriangleSigh);
  88. infos.AddRange(frontTriangleLength);
  89. infos.AddRange(frontTriangleBytes);
  90. byte sideTriangleSigh = (byte)7;
  91. tempInfoLength = netMeshData.netSideTriangles.Count * 4;
  92. byte[] sideTriangleLength = BitConverter.GetBytes(tempInfoLength);
  93. List<byte> sideTriangleBytes = new List<byte>(tempInfoLength);
  94. for (int i = 0, count = netMeshData.netSideTriangles.Count; i < count; i++)
  95. {
  96. sideTriangleBytes.AddRange(BitConverter.GetBytes(netMeshData.netSideTriangles[i]));
  97. }
  98. infos.Add(sideTriangleSigh);
  99. infos.AddRange(sideTriangleLength);
  100. infos.AddRange(sideTriangleBytes);
  101. int total = infos.Count;
  102. List<byte> allInfos = new List<byte>();
  103. byte infoSigh = 101;
  104. allInfos.Add(infoSigh);
  105. allInfos.AddRange(BitConverter.GetBytes(total));
  106. allInfos.AddRange(infos);
  107. return allInfos.ToArray();
  108. }
  109. public static NetMeshData ParseBinary(byte[] message)
  110. {
  111. int index = 0;
  112. NetMeshData netMeshData = new NetMeshData();
  113. byte messageID = message[index];
  114. if (messageID == 101)
  115. {
  116. //0 是id 101
  117. int infoLenght = BitConverter.ToInt32(message, 0);
  118. byte[] totalLenth = new byte[4];
  119. index += 1;//1
  120. //1-4 包内容长度
  121. Array.Copy(message, index, totalLenth, 0, 4);
  122. index += 4;//5
  123. int totalCount = BitConverter.ToInt32(totalLenth, 0);
  124. // 5 color 标识
  125. byte colorSign = message[index];
  126. index += 1;
  127. if (colorSign == 1)
  128. {
  129. //byte[] colorLength = new byte[4];
  130. //Array.Copy(message, index, colorLength, 0, 4);
  131. //index += 4;
  132. //int colorCount = BitConverter.ToInt32(colorLength, 0);
  133. //Debug.LogError(message[index] +"_" +message[index+1]);
  134. // netMeshData.colorList = new byte[colorCount];
  135. // Array.Copy(message, index, netMeshData.colorList, 0, colorCount);
  136. // index += colorCount;
  137. }
  138. // sideVertexList
  139. byte sideVertexSign = message[index];
  140. if (sideVertexSign == 2)
  141. {
  142. int[] sideVertexInfos = GetInfoValues(message, ref index);
  143. netMeshData.netSideVertexList.AddRange(sideVertexInfos);
  144. }
  145. // frontVertexList
  146. byte frontVertexSign = message[index];
  147. if (frontVertexSign == 3)
  148. {
  149. int[] frontVertexInfos = GetInfoValues(message, ref index);
  150. netMeshData.netFrontVertexList.AddRange(frontVertexInfos);
  151. }
  152. //uvList
  153. byte uvVertexSign = message[index];
  154. if (uvVertexSign == 4)
  155. {
  156. int[] uvInfos = GetInfoValues(message, ref index);
  157. netMeshData.netUvList.AddRange(uvInfos);
  158. }
  159. // texture
  160. byte textureSign = message[index];
  161. if (textureSign == 5)
  162. {
  163. index += 1;
  164. byte[] textureLength = new byte[4];
  165. Array.Copy(message, index, textureLength, 0, 4);
  166. index += 4;
  167. int textureCount = BitConverter.ToInt32(textureLength, 0);
  168. byte[] tempBytes = new byte[textureCount];
  169. Array.Copy(message, index, tempBytes, 0, textureCount);
  170. // netMeshData.texture = ByteToTex2d(tempBytes);
  171. netMeshData.netTexture.AddRange(tempBytes);
  172. index += textureCount;
  173. }
  174. //frontTriangles
  175. byte frontTrianglesSign = message[index];
  176. if (frontTrianglesSign == 6)
  177. {
  178. int[] frontTrianglesInfos = GetInfoValues(message, ref index);
  179. netMeshData.netFrontTriangles.AddRange(frontTrianglesInfos);
  180. }
  181. //sideTriangles
  182. byte sideTrianglesSign = message[index];
  183. if (sideTrianglesSign == 7)
  184. {
  185. int[] sideTrianglesInfos = GetInfoValues(message, ref index);
  186. netMeshData.netSideTriangles.AddRange(sideTrianglesInfos);
  187. }
  188. return netMeshData;
  189. }
  190. return null;
  191. }
  192. private static int[] GetInfoValues(byte[] message, ref int index)
  193. {
  194. byte[] tempLength = new byte[4];
  195. index++;
  196. Array.Copy(message, index, tempLength, 0, 4);
  197. index += 4;
  198. int tempCount = BitConverter.ToInt32(tempLength, 0);
  199. int[] tempInfos = new int[tempCount / 4];
  200. for (int i = 0; i < tempCount / 4; i++)
  201. {
  202. byte[] tempBytes = new byte[4];
  203. Array.Copy(message, index, tempBytes, 0, 4);
  204. index += 4;
  205. //Debug.LogError(i+" "+ index);
  206. tempInfos[i] = BitConverter.ToInt32(tempBytes, 0);
  207. }
  208. return tempInfos;
  209. }
  210. private static T[] GetValues<T>(byte[] byteData, int byteByteLength)
  211. {
  212. Type t = typeof(T);
  213. int index = 0;
  214. int valueLength = byteData.Length / byteByteLength;
  215. T[] tempByteValue = new T[valueLength];
  216. for (int i = 0; i < valueLength; i++)
  217. {
  218. if (t == typeof(Int32))
  219. {
  220. tempByteValue[i] = (T)(object)BitConverter.ToInt32(byteData, index);
  221. }
  222. else if (t == typeof(Int16))
  223. {
  224. tempByteValue[i] = (T)(object)BitConverter.ToInt16(byteData, index);
  225. }
  226. else if (t == typeof(Int64))
  227. {
  228. tempByteValue[i] = (T)(object)BitConverter.ToInt64(byteData, index);
  229. }
  230. else if (t == typeof(UInt16))
  231. {
  232. tempByteValue[i] = (T)(object)BitConverter.ToUInt16(byteData, index);
  233. }
  234. else if (t == typeof(UInt32))
  235. {
  236. tempByteValue[i] = (T)(object)BitConverter.ToUInt32(byteData, index);
  237. }
  238. else if (t == typeof(UInt64))
  239. {
  240. tempByteValue[i] = (T)(object)BitConverter.ToUInt64(byteData, index);
  241. }
  242. index += byteByteLength;
  243. }
  244. return tempByteValue;
  245. }
  246. public static T BytesToIntX<T>(byte[] byteData, int offset, int byteByteLength) where T : IComparable
  247. {
  248. int mask = 0xff;
  249. long temp = 0;
  250. for (int i = 0; i < byteByteLength; i++)
  251. {
  252. temp |= (byteData[offset + i] & mask) << (8 * i);
  253. }
  254. return (T)Convert.ChangeType(temp, typeof(T));
  255. }
  256. public static T BytesToIntX<T>(List<byte> byteData, int offset, int byteByteLength) where T : IComparable
  257. {
  258. int mask = 0xff;
  259. long temp = 0;
  260. for (int i = 0; i < byteByteLength; i++)
  261. {
  262. temp |= (byteData[offset + i] & mask) << (8 * i);
  263. }
  264. return (T)Convert.ChangeType(temp, typeof(T));
  265. }
  266. public static NetMessage UnpackMessage(byte[] packet)
  267. {
  268. NetMessage message = null;
  269. using (MemoryStream ms = new MemoryStream(packet))
  270. {
  271. message = ProtoBuf.Serializer.Deserialize<NetMessage>(ms);
  272. }
  273. return message;
  274. }
  275. public static Texture2D tex;
  276. public static Texture2D ByteToTex2d(byte[] bytes, int w = 512, int h = 512)
  277. {
  278. if (tex == null)
  279. {
  280. tex = new Texture2D(w, h);
  281. }
  282. tex.LoadImage(bytes);
  283. return tex;
  284. }
  285. public static T Clone<T>(T RealObject)
  286. {
  287. using (Stream objectStream = new MemoryStream())
  288. {
  289. //利用 System.Runtime.Serialization序列化与反序列化完成引用对象的复制
  290. IFormatter formatter = new BinaryFormatter();
  291. formatter.Serialize(objectStream, RealObject);
  292. objectStream.Seek(0, SeekOrigin.Begin);
  293. return (T)formatter.Deserialize(objectStream);
  294. }
  295. }
  296. //static double[,] YUV2RGB_CONVERT_MATRIX = new double[3, 3] { { 1, 0, 1.4022 }, { 1, -0.3456, -0.7145 }, { 1, 1.771, 0 } };
  297. //public static byte[] ConvertYUV2RGB(byte[] yuvFrame, int width, int height)
  298. //{
  299. // int uIndex = width * height;
  300. // int vIndex = uIndex + ((width * height) >> 2);
  301. // int gIndex = width * height;
  302. // int bIndex = gIndex * 2;
  303. // int temp = 0;
  304. // byte[] rgbFrame = new byte[width * height * 3];
  305. // for (int y = 0; y < height; y++)
  306. // {
  307. // for (int x = 0; x < width; x++)
  308. // {
  309. // //R分量
  310. // temp = (int)(yuvFrame[y * width + x] + (yuvFrame[vIndex + (y / 2) * (width / 2) + x / 2] - 128) * YUV2RGB_CONVERT_MATRIX[0, 2]);
  311. // rgbFrame[y * width + x] = (byte)(temp < 0 ? 0 : (temp > 255 ? 255 : temp));
  312. // // G分量
  313. // temp = (int)(yuvFrame[y * width + x] + (yuvFrame[uIndex + (y / 2) * (width / 2) + x / 2] - 128) * YUV2RGB_CONVERT_MATRIX[1, 1] + (yuvFrame[vIndex + (y / 2) * (width / 2) + x / 2] - 128) * YUV2RGB_CONVERT_MATRIX[1, 2]);
  314. // rgbFrame[gIndex + y * width + x] = (byte)(temp < 0 ? 0 : (temp > 255 ? 255 : temp));
  315. // // B分量
  316. // temp = (int)(yuvFrame[y * width + x] + (yuvFrame[uIndex + (y / 2) * (width / 2) + x / 2] - 128) * YUV2RGB_CONVERT_MATRIX[2, 1]);
  317. // rgbFrame[bIndex + y * width + x] = (byte)(temp < 0 ? 0 : (temp > 255 ? 255 : temp));
  318. // }
  319. // }
  320. // return rgbFrame;
  321. //}
  322. public static void Nv420_to_rgb(Mat y, Mat nv, Mat rgb)
  323. {
  324. MatOfByte uuvv =new MatOfByte( Mat.zeros(nv.size(), CvType.CV_8UC1));
  325. byte[] dptr = uuvv.toArray();
  326. byte[] sptr =new MatOfByte(nv).toArray();
  327. for (int i = 0; i < uuvv.rows() * uuvv.cols(); ++i)
  328. {
  329. if (i % 2 == 0)
  330. dptr[i / 2] = sptr[i];
  331. else
  332. dptr[(i - 1) / 2 + uuvv.rows() * uuvv.cols() / 2] = sptr[i];
  333. }
  334. Mat yuv = new Mat((int)(y.rows() * 1.5f), y.cols(), CvType.CV_8UC1);
  335. y.copyTo(yuv.submat(new OpenCVForUnity.CoreModule.Rect(0, 0, y.cols(), y.rows())));
  336. uuvv.copyTo(yuv.submat(new OpenCVForUnity.CoreModule.Rect(0, y.rows(), y.cols(), y.rows() / 2)));
  337. Imgproc.cvtColor(yuv, rgb, Imgproc.COLOR_YUV2BGR_I420);
  338. }
  339. }
  340. }