BitmapFormat.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Runtime.InteropServices;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace NEIntelligentControl2.Models.User
  9. {
  10. class BitmapFormat
  11. {
  12. public struct BITMAPFILEHEADER
  13. {
  14. public ushort bfType;
  15. public int bfSize;
  16. public ushort bfReserved1;
  17. public ushort bfReserved2;
  18. public int bfOffBits;
  19. }
  20. public struct MASK
  21. {
  22. public byte redmask;
  23. public byte greenmask;
  24. public byte bluemask;
  25. public byte rgbReserved;
  26. }
  27. public struct BITMAPINFOHEADER
  28. {
  29. public int biSize;
  30. public int biWidth;
  31. public int biHeight;
  32. public ushort biPlanes;
  33. public ushort biBitCount;
  34. public int biCompression;
  35. public int biSizeImage;
  36. public int biXPelsPerMeter;
  37. public int biYPelsPerMeter;
  38. public int biClrUsed;
  39. public int biClrImportant;
  40. }
  41. /*******************************************
  42. * 函数名称:RotatePic
  43. * 函数功能:旋转图片,目的是保存和显示的图片与按的指纹方向不同
  44. * 函数入参:BmpBuf---旋转前的指纹字符串
  45. * 函数出参:ResBuf---旋转后的指纹字符串
  46. * 函数返回:无
  47. *********************************************/
  48. public static void RotatePic(byte[] BmpBuf, int width, int height, ref byte[] ResBuf)
  49. {
  50. int RowLoop = 0;
  51. int ColLoop = 0;
  52. int BmpBuflen = width * height;
  53. try
  54. {
  55. for (RowLoop = 0; RowLoop < BmpBuflen;)
  56. {
  57. for (ColLoop = 0; ColLoop < width; ColLoop++)
  58. {
  59. ResBuf[RowLoop + ColLoop] = BmpBuf[BmpBuflen - RowLoop - width + ColLoop];
  60. }
  61. RowLoop = RowLoop + width;
  62. }
  63. }
  64. catch (Exception ex)
  65. {
  66. //ZKCE.SysException.ZKCELogger logger = new ZKCE.SysException.ZKCELogger(ex);
  67. //logger.Append();
  68. }
  69. }
  70. /*******************************************
  71. * 函数名称:StructToBytes
  72. * 函数功能:将结构体转化成无符号字符串数组
  73. * 函数入参:StructObj---被转化的结构体
  74. * Size---被转化的结构体的大小
  75. * 函数出参:无
  76. * 函数返回:结构体转化后的数组
  77. *********************************************/
  78. public static byte[] StructToBytes(object StructObj, int Size)
  79. {
  80. int StructSize = Marshal.SizeOf(StructObj);
  81. byte[] GetBytes = new byte[StructSize];
  82. try
  83. {
  84. IntPtr StructPtr = Marshal.AllocHGlobal(StructSize);
  85. Marshal.StructureToPtr(StructObj, StructPtr, false);
  86. Marshal.Copy(StructPtr, GetBytes, 0, StructSize);
  87. Marshal.FreeHGlobal(StructPtr);
  88. if (Size == 14)
  89. {
  90. byte[] NewBytes = new byte[Size];
  91. int Count = 0;
  92. int Loop = 0;
  93. for (Loop = 0; Loop < StructSize; Loop++)
  94. {
  95. if (Loop != 2 && Loop != 3)
  96. {
  97. NewBytes[Count] = GetBytes[Loop];
  98. Count++;
  99. }
  100. }
  101. return NewBytes;
  102. }
  103. else
  104. {
  105. return GetBytes;
  106. }
  107. }
  108. catch (Exception ex)
  109. {
  110. //ZKCE.SysException.ZKCELogger logger = new ZKCE.SysException.ZKCELogger(ex);
  111. //logger.Append();
  112. return GetBytes;
  113. }
  114. }
  115. /*******************************************
  116. * 函数名称:GetBitmap
  117. * 函数功能:将传进来的数据保存为图片
  118. * 函数入参:buffer---图片数据
  119. * nWidth---图片的宽度
  120. * nHeight---图片的高度
  121. * 函数出参:无
  122. * 函数返回:无
  123. *********************************************/
  124. public static void GetBitmap(byte[] buffer, int nWidth, int nHeight, ref MemoryStream ms)
  125. {
  126. int ColorIndex = 0;
  127. ushort m_nBitCount = 8;
  128. int m_nColorTableEntries = 256;
  129. byte[] ResBuf = new byte[nWidth * nHeight * 2];
  130. try
  131. {
  132. BITMAPFILEHEADER BmpHeader = new BITMAPFILEHEADER();
  133. BITMAPINFOHEADER BmpInfoHeader = new BITMAPINFOHEADER();
  134. MASK[] ColorMask = new MASK[m_nColorTableEntries];
  135. int w = (((nWidth + 3) / 4) * 4);
  136. //图片头信息
  137. BmpInfoHeader.biSize = Marshal.SizeOf(BmpInfoHeader);
  138. BmpInfoHeader.biWidth = nWidth;
  139. BmpInfoHeader.biHeight = nHeight;
  140. BmpInfoHeader.biPlanes = 1;
  141. BmpInfoHeader.biBitCount = m_nBitCount;
  142. BmpInfoHeader.biCompression = 0;
  143. BmpInfoHeader.biSizeImage = 0;
  144. BmpInfoHeader.biXPelsPerMeter = 0;
  145. BmpInfoHeader.biYPelsPerMeter = 0;
  146. BmpInfoHeader.biClrUsed = m_nColorTableEntries;
  147. BmpInfoHeader.biClrImportant = m_nColorTableEntries;
  148. //文件头信息
  149. BmpHeader.bfType = 0x4D42;
  150. BmpHeader.bfOffBits = 14 + Marshal.SizeOf(BmpInfoHeader) + BmpInfoHeader.biClrUsed * 4;
  151. BmpHeader.bfSize = BmpHeader.bfOffBits + ((((w * BmpInfoHeader.biBitCount + 31) / 32) * 4) * BmpInfoHeader.biHeight);
  152. BmpHeader.bfReserved1 = 0;
  153. BmpHeader.bfReserved2 = 0;
  154. ms.Write(StructToBytes(BmpHeader, 14), 0, 14);
  155. ms.Write(StructToBytes(BmpInfoHeader, Marshal.SizeOf(BmpInfoHeader)), 0, Marshal.SizeOf(BmpInfoHeader));
  156. //调试板信息
  157. for (ColorIndex = 0; ColorIndex < m_nColorTableEntries; ColorIndex++)
  158. {
  159. ColorMask[ColorIndex].redmask = (byte)ColorIndex;
  160. ColorMask[ColorIndex].greenmask = (byte)ColorIndex;
  161. ColorMask[ColorIndex].bluemask = (byte)ColorIndex;
  162. ColorMask[ColorIndex].rgbReserved = 0;
  163. ms.Write(StructToBytes(ColorMask[ColorIndex], Marshal.SizeOf(ColorMask[ColorIndex])), 0, Marshal.SizeOf(ColorMask[ColorIndex]));
  164. }
  165. //图片旋转,解决指纹图片倒立的问题
  166. RotatePic(buffer, nWidth, nHeight, ref ResBuf);
  167. byte[] filter = null;
  168. if (w - nWidth > 0)
  169. {
  170. filter = new byte[w - nWidth];
  171. }
  172. for (int i = 0; i < nHeight; i++)
  173. {
  174. ms.Write(ResBuf, i * nWidth, nWidth);
  175. if (w - nWidth > 0)
  176. {
  177. ms.Write(ResBuf, 0, w - nWidth);
  178. }
  179. }
  180. }
  181. catch (Exception ex)
  182. {
  183. // ZKCE.SysException.ZKCELogger logger = new ZKCE.SysException.ZKCELogger(ex);
  184. // logger.Append();
  185. }
  186. }
  187. /*******************************************
  188. * 函数名称:WriteBitmap
  189. * 函数功能:将传进来的数据保存为图片
  190. * 函数入参:buffer---图片数据
  191. * nWidth---图片的宽度
  192. * nHeight---图片的高度
  193. * 函数出参:无
  194. * 函数返回:无
  195. *********************************************/
  196. public static void WriteBitmap(byte[] buffer, int nWidth, int nHeight)
  197. {
  198. int ColorIndex = 0;
  199. ushort m_nBitCount = 8;
  200. int m_nColorTableEntries = 256;
  201. byte[] ResBuf = new byte[nWidth * nHeight];
  202. try
  203. {
  204. BITMAPFILEHEADER BmpHeader = new BITMAPFILEHEADER();
  205. BITMAPINFOHEADER BmpInfoHeader = new BITMAPINFOHEADER();
  206. MASK[] ColorMask = new MASK[m_nColorTableEntries];
  207. int w = (((nWidth + 3) / 4) * 4);
  208. //图片头信息
  209. BmpInfoHeader.biSize = Marshal.SizeOf(BmpInfoHeader);
  210. BmpInfoHeader.biWidth = nWidth;
  211. BmpInfoHeader.biHeight = nHeight;
  212. BmpInfoHeader.biPlanes = 1;
  213. BmpInfoHeader.biBitCount = m_nBitCount;
  214. BmpInfoHeader.biCompression = 0;
  215. BmpInfoHeader.biSizeImage = 0;
  216. BmpInfoHeader.biXPelsPerMeter = 0;
  217. BmpInfoHeader.biYPelsPerMeter = 0;
  218. BmpInfoHeader.biClrUsed = m_nColorTableEntries;
  219. BmpInfoHeader.biClrImportant = m_nColorTableEntries;
  220. //文件头信息
  221. BmpHeader.bfType = 0x4D42;
  222. BmpHeader.bfOffBits = 14 + Marshal.SizeOf(BmpInfoHeader) + BmpInfoHeader.biClrUsed * 4;
  223. BmpHeader.bfSize = BmpHeader.bfOffBits + ((((w * BmpInfoHeader.biBitCount + 31) / 32) * 4) * BmpInfoHeader.biHeight);
  224. BmpHeader.bfReserved1 = 0;
  225. BmpHeader.bfReserved2 = 0;
  226. Stream FileStream = File.Open("finger.bmp", FileMode.Create, FileAccess.Write);
  227. BinaryWriter TmpBinaryWriter = new BinaryWriter(FileStream);
  228. TmpBinaryWriter.Write(StructToBytes(BmpHeader, 14));
  229. TmpBinaryWriter.Write(StructToBytes(BmpInfoHeader, Marshal.SizeOf(BmpInfoHeader)));
  230. //调试板信息
  231. for (ColorIndex = 0; ColorIndex < m_nColorTableEntries; ColorIndex++)
  232. {
  233. ColorMask[ColorIndex].redmask = (byte)ColorIndex;
  234. ColorMask[ColorIndex].greenmask = (byte)ColorIndex;
  235. ColorMask[ColorIndex].bluemask = (byte)ColorIndex;
  236. ColorMask[ColorIndex].rgbReserved = 0;
  237. TmpBinaryWriter.Write(StructToBytes(ColorMask[ColorIndex], Marshal.SizeOf(ColorMask[ColorIndex])));
  238. }
  239. //图片旋转,解决指纹图片倒立的问题
  240. RotatePic(buffer, nWidth, nHeight, ref ResBuf);
  241. //写图片
  242. TmpBinaryWriter.Write(ResBuf);
  243. byte[] filter = null;
  244. if (w - nWidth > 0)
  245. {
  246. filter = new byte[w - nWidth];
  247. }
  248. for (int i = 0; i < nHeight; i++)
  249. {
  250. TmpBinaryWriter.Write(ResBuf, i * nWidth, nWidth);
  251. if (w - nWidth > 0)
  252. {
  253. TmpBinaryWriter.Write(ResBuf, 0, w - nWidth);
  254. }
  255. }
  256. FileStream.Close();
  257. TmpBinaryWriter.Close();
  258. }
  259. catch (Exception ex)
  260. {
  261. //ZKCE.SysException.ZKCELogger logger = new ZKCE.SysException.ZKCELogger(ex);
  262. //logger.Append();
  263. }
  264. }
  265. }
  266. }