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