-
Notifications
You must be signed in to change notification settings - Fork 52
/
main.cxx
105 lines (90 loc) · 3.29 KB
/
main.cxx
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
/* Copyright (C) 2011-2020 Doubango Telecom <https://www.doubango.org>
* File author: Mamadou DIOP (Doubango Telecom, France).
* License: For non commercial use only.
* Source code: https://github.com/DoubangoTelecom/ultimateMRZ-SDK
* WebSite: https://www.doubango.org/webapps/mrz/
*/
// More info about parser application: https://www.doubango.org/SDKs/mrz/docs/MRZ_parser.html
/*
https://github.com/DoubangoTelecom/ultimateMRZ-SDK/blob/master/samples/c++/parser/README.md
Usage:
parser <path-to-file-containing mrz-lines>
Example:
parser C:/Projects/GitHub/ultimate/ultimateMRZ/SDK_dist/assets/samples/td1.txt
*/
#include "../mrz_parser.h"
#include <map>
#include <vector>
#include <sstream>
#include <sys/stat.h>
int main(int argc, char *argv[])
{
if (argc != 2) {
ULTMRZ_SDK_PRINT_INFO(
"\nUsage:\n"
"parser <path-to-file-containing mrz-lines>\n"
"\n"
);
return -1;
}
// Build lines using data from the file
const std::string filePath = argv[1];
FILE* fp = fopen(filePath.c_str(), "rb");
if (!fp) {
ULTMRZ_SDK_PRINT_FATAL("Cannot open file: %s", filePath.c_str());
return -1;
}
struct stat st_;
ULTMRZ_SDK_ASSERT(stat(filePath.c_str(), &st_) == 0);
const size_t size = static_cast<size_t>(st_.st_size);
ULTMRZ_SDK_ASSERT(size > 0);
void* data = malloc(size + 1);
ULTMRZ_SDK_ASSERT(data != nullptr);
ULTMRZ_SDK_ASSERT(size == fread(data, 1, size, fp));
fclose(fp);
reinterpret_cast<char*>(data)[size] = '\0';
std::stringstream stream(reinterpret_cast<const char*>(data));
free(data);
std::vector<std::string> lines;
for (std::string line; std::getline(stream, line); ) {
line.erase(std::remove(line.begin(), line.end(), '\n'), line.end());
line.erase(std::remove(line.begin(), line.end(), '\r'), line.end());
ULTMRZ_SDK_PRINT_INFO("Line #%zu: %s [%zu]", lines.size(), line.c_str(), line.size());
lines.push_back(line);
}
if (lines.size() != 2 && lines.size() != 3) {
ULTMRZ_SDK_PRINT_FATAL("%zu not a valid number of lines. Expecting 2 or 3 lines", lines.size());
return -1;
}
// Make sure all lines have same length
for (size_t i = 1; i < lines.size(); ++i) {
if (lines.front().size() != lines[i].size()) {
ULTMRZ_SDK_PRINT_FATAL("All lines must have same length: %zu != %zu", lines.front().size(), lines[i].size());
return false;
}
}
// Process
MrzData mrz_data;
if (!__mrz_parser_process(lines, mrz_data)) {
ULTMRZ_SDK_PRINT_FATAL("Processing failed");
return -1;
}
// Print result
ULTMRZ_SDK_PRINT_INFO("=========================");
switch (mrz_data.type) {
case MRZ_DOCUMENT_TYPE_TD1: ULTMRZ_SDK_PRINT_INFO("=== Document Type: TD1 ==="); break;
case MRZ_DOCUMENT_TYPE_TD2: ULTMRZ_SDK_PRINT_INFO("=== Document Type: TD2 ==="); break;
case MRZ_DOCUMENT_TYPE_TD3: ULTMRZ_SDK_PRINT_INFO("=== Document Type: TD3 ==="); break;
case MRZ_DOCUMENT_TYPE_MRVA: ULTMRZ_SDK_PRINT_INFO("=== Document Type: MRVA ==="); break;
case MRZ_DOCUMENT_TYPE_MRVB: ULTMRZ_SDK_PRINT_INFO("=== Document Type: MRVB ==="); break;
default: ULTMRZ_SDK_ASSERT(false); break;
}
for (const auto& field : mrz_data.fields) {
ULTMRZ_SDK_PRINT_INFO("%s:\t%s", field.first.c_str(), field.second.c_str());
}
ULTMRZ_SDK_PRINT_INFO("=========================");
// Press any key to terminate
ULTMRZ_SDK_PRINT_INFO("Press any key to terminate !!");
getchar();
return 0;
}