BitmapFormat.cs 11 KB

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