-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDriverLicenseScanner.cpp
236 lines (206 loc) · 7.24 KB
/
DriverLicenseScanner.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
#include <iostream>
#include <string>
#include <climits>
#include "../../Include/DynamsoftCaptureVisionRouter.h"
#include "../../Include/DynamsoftUtility.h"
using namespace std;
using namespace dynamsoft::cvr;
using namespace dynamsoft::dbr;
using namespace dynamsoft::dcp;
using namespace dynamsoft::license;
using namespace dynamsoft::basic_structures;
using namespace dynamsoft::utility;
// The following code only applies to Windows.
#if defined(_WIN64) || defined(_WIN32)
#ifdef _WIN64
#pragma comment(lib, "../../Distributables/Lib/Windows/x64/DynamsoftCaptureVisionRouterx64.lib")
#pragma comment(lib, "../../Distributables/Lib/Windows/x64/DynamsoftCorex64.lib")
#pragma comment(lib, "../../Distributables/Lib/Windows/x64/DynamsoftUtilityx64.lib")
#pragma comment(lib, "../../Distributables/Lib/Windows/x64/DynamsoftLicensex64.lib")
#else
#pragma comment(lib, "../../Distributables/Lib/Windows/x86/DynamsoftCaptureVisionRouterx86.lib")
#pragma comment(lib, "../../Distributables/Lib/Windows/x86/DynamsoftCorex86.lib")
#pragma comment(lib, "../../Distributables/Lib/Windows/x86/DynamsoftUtilityx86.lib")
#pragma comment(lib, "../../Distributables/Lib/Windows/x86/DynamsoftLicensex86.lib")
#endif
#endif
class DriverLicenseResult
{
public:
string codeType;
string versionNumber;
string licenseNumber;
string vehicleClass;
string fullName;
string lastName;
string givenName;
string gender;
string birthDate;
string issuedDate;
string expirationDate;
DriverLicenseResult FromParsedResultItem(const CParsedResultItem *item)
{
codeType = item->GetCodeType();
if (codeType != "AAMVA_DL_ID" && codeType != "AAMVA_DL_ID_WITH_MAG_STRIPE" && codeType != "SOUTH_AFRICA_DL")
return *this;
if (item->GetFieldValidationStatus("licenseNumber") != VS_FAILED && item->GetFieldValue("licenseNumber") != NULL)
{
licenseNumber = item->GetFieldValue("licenseNumber");
}
if (item->GetFieldValidationStatus("AAMVAVersionNumber") != VS_FAILED && item->GetFieldValue("AAMVAVersionNumber") != NULL)
{
versionNumber = item->GetFieldValue("AAMVAVersionNumber");
}
if (item->GetFieldValidationStatus("vehicleClass") != VS_FAILED && item->GetFieldValue("vehicleClass") != NULL)
{
vehicleClass = item->GetFieldValue("vehicleClass");
}
if (item->GetFieldValidationStatus("lastName") != VS_FAILED && item->GetFieldValue("lastName") != NULL)
{
lastName = item->GetFieldValue("lastName");
}
if (item->GetFieldValidationStatus("surName") != VS_FAILED && item->GetFieldValue("surName") != NULL)
{
lastName = item->GetFieldValue("surName");
}
if (item->GetFieldValidationStatus("givenName") != VS_FAILED && item->GetFieldValue("givenName") != NULL)
{
givenName = item->GetFieldValue("givenName");
}
if (item->GetFieldValidationStatus("fullName") != VS_FAILED && item->GetFieldValue("fullName") != NULL)
{
fullName = item->GetFieldValue("fullName");
}
if (item->GetFieldValidationStatus("sex") != VS_FAILED && item->GetFieldValue("sex") != NULL)
{
gender = item->GetFieldValue("sex");
}
if (item->GetFieldValidationStatus("gender") != VS_FAILED && item->GetFieldValue("gender") != NULL)
{
gender = item->GetFieldValue("gender");
}
if (item->GetFieldValidationStatus("birthDate") != VS_FAILED && item->GetFieldValue("birthDate") != NULL)
{
birthDate = item->GetFieldValue("birthDate");
}
if (item->GetFieldValidationStatus("issuedDate") != VS_FAILED && item->GetFieldValue("issuedDate") != NULL)
{
issuedDate = item->GetFieldValue("issuedDate");
}
if (item->GetFieldValidationStatus("expirationDate") != VS_FAILED && item->GetFieldValue("expirationDate") != NULL)
{
expirationDate = item->GetFieldValue("expirationDate");
}
if (fullName == "")
{
fullName = lastName + givenName;
}
return *this;
}
string ToString()
{
string msg = "Parsed Information:\n";
msg += "\tCode Type: " + codeType + "\n";
msg += "\tLicense Number: " + licenseNumber + "\n";
msg += "\tVehicle Class: " + vehicleClass + "\n";
msg += "\tLast Name: " + lastName + "\n";
msg += "\tGiven Name: " + givenName + "\n";
msg += "\tFull Name: " + fullName + "\n";
msg += "\tGender: " + gender + "\n";
msg += "\tDate of Birth: " + birthDate + "\n";
msg += "\tIssued Date: " + issuedDate + "\n";
msg += "\tExpiration Date: " + expirationDate + "\n";
return msg;
}
};
void PrintResult(CParsedResult *pResult)
{
const CFileImageTag *tag = dynamic_cast<const CFileImageTag *>(pResult->GetOriginalImageTag());
cout << "File: " << tag->GetFilePath() << endl;
cout << "Page: " << tag->GetPageNumber() << endl;
if (pResult->GetErrorCode() != EC_OK)
{
cout << "Error: " << pResult->GetErrorString() << endl;
}
else
{
int lCount = pResult->GetItemsCount();
cout << "Parsed " << lCount << " driver licenses" << endl;
for (int i = 0; i < lCount; i++)
{
const CParsedResultItem *item = pResult->GetItem(i);
DriverLicenseResult result;
result.FromParsedResultItem(item);
cout << result.ToString() << endl;
}
}
cout << endl;
}
int main()
{
int errorcode = 0;
char error[512];
cout << "*****************************************" << endl;
cout << "Welcome to Dynamsoft DriverLicense Sample" << endl;
cout << "*****************************************" << endl;
// 1. Initialize license.
// You can request and extend a trial license from https://www.dynamsoft.com/customer/license/trialLicense?product=dcv&utm_source=samples&package=c_cpp
// The string "DLS2eyJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSJ9" here is a free public trial license. Note that network connection is required for this license to work.
errorcode = CLicenseManager::InitLicense("DLS2eyJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSJ9", error, 512);
if (errorcode != ErrorCode::EC_OK && errorcode != ErrorCode::EC_LICENSE_CACHE_USED)
{
cout << "License initialization failed: ErrorCode: " << errorcode << ", ErrorString: " << error << endl;
cout << "Press Enter to quit..." << endl;
cin.ignore();
return 0;
}
else
{
// 2. Create an instance of CCaptureVisionRouter.
CCaptureVisionRouter *router = new CCaptureVisionRouter;
// 3. Set input image source.
string imgPath;
while (1)
{
cout << endl
<< ">> Input your image full path:" << endl
<<">> 'Enter' for sample image or 'Q'/'q' to quit"<< endl;
getline(cin, imgPath);
if (imgPath == "q" || imgPath == "Q")
{
break;
}
if (imgPath.empty())
imgPath = "../../Images/driver-license-sample.jpg";
if (imgPath.length() >= 2 && imgPath.front() == '"' && imgPath.back() == '"')
imgPath = imgPath.substr(1, imgPath.length() - 2);
// 4. Capture.
CCapturedResult* result = router->Capture(imgPath.c_str(),"ReadDriversLicense");
if(result->GetErrorCode() != ErrorCode::EC_OK)
{
cout << "Capture failed: ErrorCode: " << result->GetErrorCode() << ", ErrorString: " << result->GetErrorString() << endl;
}
else
{
CParsedResult* dcpResult = result->GetParsedResult();
if (dcpResult == NULL || dcpResult->GetItemsCount() == 0)
{
cout<<"No parsed results."<<endl;
}
else
{
PrintResult(dcpResult);
}
//5. Release the parsed result.
if (dcpResult)
dcpResult->Release();
}
//6. Release the capture result.
if (result)
result->Release();
}
// 7. Release the allocated memory.
delete router, router = NULL;
}
return 0;
}