forked from erikssm/futronics-fingerprint-reader
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathftrScanAPI_Ex.c
More file actions
216 lines (201 loc) · 7.12 KB
/
Copy pathftrScanAPI_Ex.c
File metadata and controls
216 lines (201 loc) · 7.12 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ftrScanAPI.h>
typedef struct tagBITMAPINFOHEADER{
unsigned int biSize;
int biWidth;
int biHeight;
unsigned short int biPlanes;
unsigned short int biBitCount;
unsigned int biCompression;
unsigned int biSizeImage;
int biXPelsPerMeter;
int biYPelsPerMeter;
unsigned int biClrUsed;
unsigned int biClrImportant;
} BITMAPINFOHEADER, *PBITMAPINFOHEADER;
typedef struct tagRGBQUAD {
unsigned char rgbBlue;
unsigned char rgbGreen;
unsigned char rgbRed;
unsigned char rgbReserved;
} RGBQUAD;
typedef struct tagBITMAPINFO {
BITMAPINFOHEADER bmiHeader;
RGBQUAD bmiColors[1];
} BITMAPINFO, *PBITMAPINFO;
typedef struct tagBITMAPFILEHEADER {
unsigned short int bfType;
unsigned int bfSize;
unsigned short int bfReserved1;
unsigned short int bfReserved2;
unsigned int bfOffBits;
} BITMAPFILEHEADER, *PBITMAPFILEHEADER;
int write_bmp_file(unsigned char *pImage, int width, int height)
{
BITMAPINFO *pDIBHeader;
BITMAPFILEHEADER bmfHeader;
int iCyc;
// allocate memory for a DIB header
if( (pDIBHeader = (BITMAPINFO *)malloc( sizeof( BITMAPINFO ) + sizeof( RGBQUAD ) * 255 )) == NULL )
{
printf("Alloc memory failed! - Unable to write to file!!\n");
return -1;
}
memset( (void *)pDIBHeader, 0, sizeof( BITMAPINFO ) + sizeof( RGBQUAD ) * 255 );
// fill the DIB header
pDIBHeader->bmiHeader.biSize = sizeof( BITMAPINFOHEADER );
pDIBHeader->bmiHeader.biWidth = width;
pDIBHeader->bmiHeader.biHeight = height;
pDIBHeader->bmiHeader.biPlanes = 1;
pDIBHeader->bmiHeader.biBitCount = 8; // 8bits gray scale bmp
pDIBHeader->bmiHeader.biCompression = 0; // BI_RGB = 0;
// initialize logical and DIB grayscale
for( iCyc = 0; iCyc < 256; iCyc++ )
{
pDIBHeader->bmiColors[iCyc].rgbBlue = pDIBHeader->bmiColors[iCyc].rgbGreen = pDIBHeader->bmiColors[iCyc].rgbRed = (unsigned char)iCyc;
}
// set BITMAPFILEHEADER structure
//((char *)(&bmfHeader.bfType))[0] = 'B';
//((char *)(&bmfHeader.bfType))[1] = 'M';
bmfHeader.bfType = 0x42 + 0x4D * 0x100;
bmfHeader.bfSize = 14 + sizeof( BITMAPINFO ) + sizeof( RGBQUAD ) * 255 + width * height; //sizeof( BITMAPFILEHEADER ) = 14
bmfHeader.bfOffBits = 14 + pDIBHeader->bmiHeader.biSize + sizeof( RGBQUAD ) * 256;
//write to file
FILE *fp;
fp = fopen("frame_Ex.bmp", "wb");
if( fp == NULL )
{
printf("Failed to write to file\n");
free( pDIBHeader );
return -1;
}
//fwrite( (void *)&bmfHeader, 1, sizeof(BITMAPFILEHEADER), fp );
fwrite( (void *)&bmfHeader.bfType, sizeof(unsigned short int), 1, fp );
fwrite( (void *)&bmfHeader.bfSize, sizeof(unsigned int), 1, fp );
fwrite( (void *)&bmfHeader.bfReserved1, sizeof(unsigned short int), 1, fp );
fwrite( (void *)&bmfHeader.bfReserved2, sizeof(unsigned short int), 1, fp );
fwrite( (void *)&bmfHeader.bfOffBits, sizeof(unsigned int), 1, fp );
//fwrite( (void *)pDIBHeader, 1, sizeof( BITMAPINFO ) + sizeof( RGBQUAD ) * 255, fp );
fwrite( (void *)&pDIBHeader->bmiHeader.biSize, sizeof(unsigned int), 1, fp);
fwrite( (void *)&pDIBHeader->bmiHeader.biWidth, sizeof(int), 1, fp);
fwrite( (void *)&pDIBHeader->bmiHeader.biHeight, sizeof(int), 1, fp);
fwrite( (void *)&pDIBHeader->bmiHeader.biPlanes, sizeof(unsigned short int), 1, fp);
fwrite( (void *)&pDIBHeader->bmiHeader.biBitCount, sizeof(unsigned short int), 1, fp);
fwrite( (void *)&pDIBHeader->bmiHeader.biCompression, sizeof(unsigned int), 1, fp);
fwrite( (void *)&pDIBHeader->bmiHeader.biSizeImage, sizeof(unsigned int), 1, fp);
fwrite( (void *)&pDIBHeader->bmiHeader.biXPelsPerMeter, sizeof(int), 1, fp);
fwrite( (void *)&pDIBHeader->bmiHeader.biYPelsPerMeter, sizeof(int), 1, fp);
fwrite( (void *)&pDIBHeader->bmiHeader.biClrUsed, sizeof(unsigned int), 1, fp);
fwrite( (void *)&pDIBHeader->bmiHeader.biClrImportant, sizeof(unsigned int), 1, fp);
for( iCyc=0; iCyc<256; iCyc++ )
{
fwrite( (void *)&pDIBHeader->bmiColors[iCyc].rgbBlue, sizeof(unsigned char), 1, fp );
fwrite( (void *)&pDIBHeader->bmiColors[iCyc].rgbGreen, sizeof(unsigned char), 1, fp );
fwrite( (void *)&pDIBHeader->bmiColors[iCyc].rgbRed, sizeof(unsigned char), 1, fp );
fwrite( (void *)&pDIBHeader->bmiColors[iCyc].rgbReserved, sizeof(unsigned char), 1, fp );
}
//
// copy fingerprint image
unsigned char *cptrData;
unsigned char *cptrDIBData;
unsigned char *pDIBData;
pDIBData = (unsigned char *)malloc( height * width);
memset( (void *)pDIBData, 0, height * width );
cptrData = pImage + (height - 1) * width;
cptrDIBData = pDIBData;
for( iCyc = 0; iCyc < height; iCyc++ )
{
memcpy( cptrDIBData, cptrData, width );
cptrData = cptrData - width;
cptrDIBData = cptrDIBData + width;
}
fwrite( (void *)pDIBData, 1, width * height, fp );
fclose(fp);
printf("Fingerprint image is written to file: frame_Ex.bmp.\n");
free( pDIBData );
free( pDIBHeader );
return 0;
}
void PrintErrorMessage( unsigned long nErrCode )
{
printf("Failed to obtain image. ");
char stError[64];
switch( nErrCode )
{
case 0:
strcpy( stError, "OK" );
break;
case FTR_ERROR_EMPTY_FRAME: // ERROR_EMPTY
strcpy( stError, "- Empty frame -" );
break;
case FTR_ERROR_MOVABLE_FINGER:
strcpy( stError, "- Movable finger -" );
break;
case FTR_ERROR_NO_FRAME:
strcpy( stError, "- Fake finger -" );
break;
case FTR_ERROR_HARDWARE_INCOMPATIBLE:
strcpy( stError, "- Incompatible hardware -" );
break;
case FTR_ERROR_FIRMWARE_INCOMPATIBLE:
strcpy( stError, "- Incompatible firmware -" );
break;
case FTR_ERROR_INVALID_AUTHORIZATION_CODE:
strcpy( stError, "- Invalid authorization code -" );
break;
default:
sprintf( stError, "Unknown return code - %lu", nErrCode );
}
printf("%s\n", stError);
}
int main(int argc, char *argv[])
{
void *hDevice;
FTRSCAN_IMAGE_SIZE ImageSize;
unsigned char *pBuffer;
int i;
hDevice = ftrScanOpenDevice();
if( hDevice == NULL )
{
printf("Failed to open device!\n");
return -1;
}
if( !ftrScanGetImageSize( hDevice, &ImageSize ) )
{
printf("Failed to get image size\n");
ftrScanCloseDevice( hDevice );
return -1;
}
else
{
printf("Image size is %d\n", ImageSize.nImageSize);
pBuffer = (unsigned char *)malloc( ImageSize.nImageSize );
printf("Please put your finger on the scanner:\n");
while(1)
{
if( ftrScanIsFingerPresent( hDevice, NULL ) )
break;
for(i=0; i<100; i++); //sleep
}
printf("Capturing fingerprint ......\n");
while(1)
{
if( ftrScanGetFrame(hDevice, pBuffer, NULL) )
{
printf("Done!\nWriting to file......\n");
write_bmp_file( pBuffer, ImageSize.nWidth, ImageSize.nHeight );
break;
}
else
{
PrintErrorMessage( ftrScanGetLastError() );
for(i=0; i<100; i++);
}
}
free( pBuffer );
}
ftrScanCloseDevice( hDevice );
return 0;
}