-
Notifications
You must be signed in to change notification settings - Fork 2
/
client.cpp
316 lines (273 loc) · 10.5 KB
/
client.cpp
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
#include "client.h"
#include "ui_client.h"
Client::Client(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::Client)
{
ui->setupUi(this);
if(this->scannerInit(this->hscanner)==false){
qDebug() << "Error during scanner init.";
//exit(-1);
}
QObject::connect(&(this->socket), SIGNAL(connected()), this, SLOT(connectedSlot()));
QObject::connect(&(this->socket), SIGNAL(readyRead()), this, SLOT(readSlot()));
QObject::connect(&(this->socket), SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(errorSlot(QAbstractSocket::SocketError)));
QObject::connect(&(this->socket), SIGNAL(disconnected()), this, SLOT(disconnectedSlot()));
QObject::connect(&(this->socket), SIGNAL(stateChanged(QAbstractSocket::SocketState)), this, SLOT(changedSlot(QAbstractSocket::SocketState)));
}
Client::~Client()
{
delete ui;
}
void Client::on_connect_button_clicked()
{
this->socket.connectToHost(QHostAddress(ui->remote_ip->text()),ui->remote_port->text().toUInt());
}
void Client::connectedSlot()
{
qDebug() << "Connection established to " << this->socket.peerAddress().toString();
ui->statusBar->showMessage("Connection established to " + this->socket.peerAddress().toString());
ui->local_ip->setText(this->socket.localAddress().toString());
ui->local_port->setText(QString::number(this->socket.localPort()));
ui->local_socket->setText(QString::number(this->socket.socketDescriptor()));
}
void Client::readSlot()
{
QByteArray message = this->socket.readAll();
qDebug() << "Message from server: " << message;
}
void Client::errorSlot(QAbstractSocket::SocketError error)
{
qDebug() << this->socket.errorString();
ui->statusBar->showMessage(this->socket.errorString());
}
void Client::disconnectedSlot()
{
qDebug() << "Disconnected from server.";
ui->statusBar->showMessage("Disconnected from server.");
ui->local_ip->setText("");
ui->local_port->setText("");
ui->local_socket->setText("");
}
void Client::changedSlot(QAbstractSocket::SocketState)
{
qDebug() << "State changed.";
ui->statusBar->showMessage("State changed.");
}
void Client::on_disconnect_button_clicked()
{
if(this->socket.state() == QTcpSocket::ConnectedState){
this->socket.disconnectFromHost();
}
}
void Client::on_suprema_scan_button_clicked()
{
if(this->socket.state()!=QTcpSocket::ConnectedState){
qWarning() << "No TCP connection to server.";
ui->suprema_log->append("No TCP connection to server.");
return;
}
int width, height;
unsigned char* img_data = this->scannerCapture(width,height);
if(img_data == nullptr){
qDebug() << "Scanner error.";
ui->suprema_log->append("Scanner error.");
return;
}
QImage fing_img(img_data,width,height,QImage::Format_Grayscale8);
ui->suprema_fingerprint_img->setPixmap(QPixmap::fromImage(fing_img));
QByteArray byteCount;
QByteArray imgWidth, imgHeight;
QByteArray serverWindowID;
QDataStream dataStream1(&byteCount, QIODevice::WriteOnly);
QDataStream dataStream2(&imgWidth, QIODevice::WriteOnly);
QDataStream dataStream3(&imgHeight, QIODevice::WriteOnly);
QDataStream dataStream4(&serverWindowID, QIODevice::WriteOnly);
quint32 bc = SUPREMA_IMG_SIZE+CONTROL_DATA_SIZE;
quint32 iw = SUPREMA_IMG_WIDTH;
quint32 ih = SUPREMA_IMG_HEIGHT;
quint32 swid = 1;
dataStream1 << bc;
dataStream2 << iw;
dataStream3 << ih;
dataStream4 << swid;
this->socket.write(byteCount); // number of bytes
this->socket.write(imgWidth); // image width
this->socket.write(imgHeight); // image height
this->socket.write(serverWindowID); // server window ID
this->socket.write((const char*)img_data,sizeof(unsigned char)*width*height);
free(img_data);
ui->save_image_button->setEnabled(true);
}
void Client::on_suprema_scan_button2_clicked()
{
if(this->socket.state()!=QTcpSocket::ConnectedState){
qWarning() << "No TCP connection to server.";
ui->suprema_log->append("No TCP connection to server.");
return;
}
int width, height;
unsigned char* img_data = this->scannerCapture(width,height);
if(img_data == nullptr){
qDebug() << "Scanner error.";
ui->suprema_log->append("Scanner error.");
return;
}
QImage fing_img(img_data,width,height,QImage::Format_Grayscale8);
ui->suprema_fingerprint_img2->setPixmap(QPixmap::fromImage(fing_img));
QByteArray byteCount;
QByteArray imgWidth, imgHeight;
QByteArray serverWindowID;
QDataStream dataStream1(&byteCount, QIODevice::WriteOnly);
QDataStream dataStream2(&imgWidth, QIODevice::WriteOnly);
QDataStream dataStream3(&imgHeight, QIODevice::WriteOnly);
QDataStream dataStream4(&serverWindowID, QIODevice::WriteOnly);
quint32 bc = SUPREMA_IMG_SIZE+CONTROL_DATA_SIZE;
quint32 iw = SUPREMA_IMG_WIDTH;
quint32 ih = SUPREMA_IMG_HEIGHT;
quint32 swid = 2;
dataStream1 << bc;
dataStream2 << iw;
dataStream3 << ih;
dataStream4 << swid;
this->socket.write(byteCount); // number of bytes
this->socket.write(imgWidth); // image width
this->socket.write(imgHeight); // image height
this->socket.write(serverWindowID); // server window ID
this->socket.write((const char*)img_data,sizeof(unsigned char)*width*height);
free(img_data);
ui->save_image_button2->setEnabled(true);
}
void Client::on_save_image_button_clicked()
{
qDebug() << ui->suprema_fingerprint_img->grab().save(QFileDialog::getSaveFileName(nullptr,"Save image as"));
}
void Client::on_save_image_button2_clicked()
{
qDebug() << ui->suprema_fingerprint_img2->grab().save(QFileDialog::getSaveFileName(nullptr,"Save image as"));
}
bool Client::scannerInit(HUFScanner& hscanner)
{
if(UFS_Init()==UFS_OK){
qDebug() << "Suprema BioMini Slim initialized successfully.";
ui->suprema_log->append("Suprema BioMini Slim initialized successfully.");
int scannerNumber;
UFS_GetScannerNumber(&scannerNumber);
if(scannerNumber==0){
return false;
}
qDebug() << "Number of scanners: " << scannerNumber;
if(UFS_GetScannerHandle(scannerNumber-1,&hscanner)== UFS_OK){
qDebug() << "Scanner handle obtained.";
ui->suprema_log->append("Scanner handle obtained.");
return true;
}
else{
qDebug() << "SCANNER HANDLE: Fail.";
ui->suprema_log->append("SCANNER HANDLE: Fail.");
}
}
else{
qDebug() << "INIT: Fail.";
ui->suprema_log->append("INIT: Fail.");
}
return false;
}
unsigned char* Client::scannerCapture(int& width, int& height)
{
int status;
char* errStr = (char*)calloc(1000,sizeof(char));
if((status=UFS_CaptureSingleImage(this->hscanner)) == UFS_OK){
qDebug() << "Image captured.";
ui->suprema_log->append("Image captured.");
int resolution;
UFS_GetCaptureImageBufferInfo(this->hscanner,&width,&height,&resolution);
qDebug() << width << " x " << height;
ui->suprema_log->append(QString::number(width) + " x " + QString::number(height));
unsigned char * pdata = (unsigned char*)malloc(width*height*sizeof(unsigned char));
UFS_GetCaptureImageBuffer(this->hscanner, pdata);
free(errStr);
return pdata;
}
else{
UFS_GetErrorString(status,errStr);
qDebug() << errStr;
ui->suprema_log->append(errStr);
}
free(errStr);
return nullptr;
}
void Client::on_load_image_button_clicked()
{
if(this->socket.state()!=QTcpSocket::ConnectedState){
qWarning() << "No TCP connection to server.";
ui->suprema_log->append("No TCP connection to server.");
return;
}
QString opnFile = QFileDialog::getOpenFileName(nullptr,"Please select a fingerprint image");
if(opnFile==""){
qDebug() << "No file selected.";
return;
}
QImage opnImage(opnFile);
ui->suprema_fingerprint_img->setPixmap(QPixmap::fromImage(opnImage));
ui->left_image_path->setText(opnFile);
QByteArray byteCount;
QByteArray imgWidth, imgHeight;
QByteArray serverWindowID;
QDataStream dataStream1(&byteCount, QIODevice::WriteOnly);
QDataStream dataStream2(&imgWidth, QIODevice::WriteOnly);
QDataStream dataStream3(&imgHeight, QIODevice::WriteOnly);
QDataStream dataStream4(&serverWindowID, QIODevice::WriteOnly);
quint32 bc = opnImage.byteCount()+CONTROL_DATA_SIZE;
quint32 iw = opnImage.width();
quint32 ih = opnImage.height();
quint32 swid = 1;
dataStream1 << bc;
dataStream2 << iw;
dataStream3 << ih;
dataStream4 << swid;
this->socket.write(byteCount); // number of bytes
this->socket.write(imgWidth); // image width
this->socket.write(imgHeight); // image height
this->socket.write(serverWindowID); // server window ID
this->socket.write((const char*)opnImage.bits(),sizeof(unsigned char)*opnImage.width()*opnImage.height());
ui->save_image_button->setEnabled(true);
}
void Client::on_load_image_button2_clicked()
{
if(this->socket.state()!=QTcpSocket::ConnectedState){
qWarning() << "No TCP connection to server.";
ui->suprema_log->append("No TCP connection to server.");
return;
}
QString opnFile = QFileDialog::getOpenFileName(nullptr,"Please select a fingerprint image");
if(opnFile==""){
qDebug() << "No file selected.";
return;
}
QImage opnImage(opnFile);
ui->suprema_fingerprint_img2->setPixmap(QPixmap::fromImage(opnImage));
ui->right_image_path->setText(opnFile);
QByteArray byteCount;
QByteArray imgWidth, imgHeight;
QByteArray serverWindowID;
QDataStream dataStream1(&byteCount, QIODevice::WriteOnly);
QDataStream dataStream2(&imgWidth, QIODevice::WriteOnly);
QDataStream dataStream3(&imgHeight, QIODevice::WriteOnly);
QDataStream dataStream4(&serverWindowID, QIODevice::WriteOnly);
quint32 bc = opnImage.byteCount()+CONTROL_DATA_SIZE;
quint32 iw = opnImage.width();
quint32 ih = opnImage.height();
quint32 swid = 2;
dataStream1 << bc;
dataStream2 << iw;
dataStream3 << ih;
dataStream4 << swid;
this->socket.write(byteCount); // number of bytes
this->socket.write(imgWidth); // image width
this->socket.write(imgHeight); // image height
this->socket.write(serverWindowID); // server window ID
this->socket.write((const char*)opnImage.bits(),sizeof(unsigned char)*opnImage.width()*opnImage.height());
ui->save_image_button2->setEnabled(true);
}