Repository navigation
Expand file tree
/
Copy pathGS1AIScanner.cpp
More file actions
180 lines (164 loc) · 7.84 KB
/
Copy pathGS1AIScanner.cpp
File metadata and controls
180 lines (164 loc) · 7.84 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
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <map>
#include <regex>
#include "../../Include/DynamsoftCaptureVisionRouter.h"
#include "../../Include/DynamsoftUtility.h"
using namespace dynamsoft;
using namespace dynamsoft::cvr;
using namespace dynamsoft::license;
using namespace dynamsoft::dcp;
using namespace dynamsoft::dbr;
#if defined(_WIN64) || defined(_WIN32)
#ifdef _WIN64
#pragma comment(lib, "../../Dist/Lib/Windows/x64/DynamsoftCaptureVisionRouterx64.lib")
#pragma comment(lib, "../../Dist/Lib/Windows/x64/DynamsoftCorex64.lib")
#pragma comment(lib, "../../Dist/Lib/Windows/x64/DynamsoftUtilityx64.lib")
#pragma comment(lib, "../../Dist/Lib/Windows/x64/DynamsoftLicensex64.lib")
#pragma comment(lib, "../../Dist/Lib/Windows/x64/DynamsoftCodeParserx64.lib")
#else
#pragma comment(lib, "../../Dist/Lib/Windows/x86/DynamsoftCaptureVisionRouterx86.lib")
#pragma comment(lib, "../../Dist/Lib/Windows/x86/DynamsoftCorex86.lib")
#pragma comment(lib, "../../Dist/Lib/Windows/x86/DynamsoftUtilityx86.lib")
#pragma comment(lib, "../../Dist/Lib/Windows/x86/DynamsoftLicensex86.lib")
#pragma comment(lib, "../../Dist/Lib/Windows/x86/DynamsoftCodeParserx86.lib")
#endif
#endif
bool isAI(const std::string& x)
{
static const std::regex pattern(R"(\d+|(\d+n))");
return std::regex_match(x, pattern);
}
std::map<std::string, std::pair<std::string, std::string>> groupByFirstLayerAndFindInfo(const std::vector<std::string>& paths)
{
std::map<std::string, std::pair<std::string, std::string>> grouped;
for (const auto& path : paths) {
size_t pos = path.find('.');
std::string firstLayer = (pos == std::string::npos) ? path : path.substr(0, pos);
size_t posEnd = path.rfind('.');
std::string lastLayer = (posEnd == std::string::npos) ? path : path.substr(posEnd + 1);
if (lastLayer == firstLayer + "AI")
grouped[firstLayer].first = lastLayer;
if (lastLayer == firstLayer + "Data")
grouped[firstLayer].second = lastLayer;
}
return grouped;
}
class GS1AIResult
{
public:
GS1AIResult(const CParsedResultItem* result) :m_pRes(result) {}
std::string toString()
{
std::string ret;
int fieldsCount = m_pRes->GetFieldCount();
std::vector<std::string> fieldNames;
for (int i = 0; i < fieldsCount; ++i)
{
const char* name = m_pRes->GetFieldName(i);
if (name)
fieldNames.emplace_back(name);
}
std::map<std::string, std::pair<std::string, std::string>> nameMap = groupByFirstLayerAndFindInfo(fieldNames);
for (auto& kv : nameMap)
{
if (isAI(kv.first))
{
const char* ai = m_pRes->GetFieldValue(kv.second.first.c_str());
const char* value = m_pRes->GetFieldValue(kv.second.second.c_str());
ret.append("AI: ");
ret.append(kv.first);
ret.append(" (" + (ai ? std::string(ai) : std::string("")) + ")");
ret.append(", Value: " + (value ? std::string(value) : std::string("")) + "\n");
}
}
if (!ret.empty())
ret.insert(0, "Parsed GS1 Application Identifiers (AIs):\n");
return ret;
}
private:
const CParsedResultItem* m_pRes{ nullptr };
};
int main() {
std::cout << "***********************************************************" << std::endl;
std::cout << " Welcome to Dynamsoft Capture Vision - GS1 AI Sample" << std::endl;
std::cout << "***********************************************************" << std::endl;
int errorCode;
char errorMessage[1024];
// Initialize license.
// The string 'DLS2eyJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSJ9' here is a free public trial license. Note that network connection is required for this license to work.
// You can also request a 30-day trial license in the customer portal: https://www.dynamsoft.com/customer/license/trialLicense?product=dcv&utm_source=samples&package=c_cpp
errorCode = CLicenseManager::InitLicense("DLS2eyJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSJ9", errorMessage, 1024);
if (errorCode != ErrorCode::EC_OK && errorCode != ErrorCode::EC_LICENSE_WARNING) {
std::cout << "License initialization failed: ErrorCode: " << errorCode << ", ErrorString: " << errorMessage << std::endl;
return -1;
}
CCaptureVisionRouter* cvRouter = new CCaptureVisionRouter();
std::string templatePath = "../../CustomTemplates/ReadGS1AIBarcode.json";
errorCode = cvRouter->InitSettingsFromFile(templatePath.c_str(), errorMessage, 1024);
if (errorCode != ErrorCode::EC_OK && errorCode != ErrorCode::EC_UNSUPPORTED_JSON_KEY_WARNING) {
std::cout << "Init template failed: " << errorCode << ", " << errorMessage << std::endl;
}
else
{
while (true)
{
std::cout << std::endl;
std::cout << ">> Input your image full path:" << std::endl;
std::cout << ">> 'Enter' for sample image or 'Q'/'q' to quit" << std::endl;
std::string imagePath;
std::getline(std::cin, imagePath);
if (imagePath == "q" || imagePath == "Q")
break;
if (imagePath.empty())
imagePath = "../../Images/gs1-ai-sample.png";
if (imagePath.length() >= 2 && imagePath.front() == '"' && imagePath.back() == '"')
imagePath = imagePath.substr(1, imagePath.length() - 2);
if (!std::ifstream(imagePath).good())
{
std::cout << "The image path does not exist." << std::endl;
continue;
}
CCapturedResultArray* resultArray = cvRouter->CaptureMultiPages(imagePath.c_str(), "ReadGS1AIBarcode");
int count = resultArray->GetResultsCount();
bool noResultPrint = true;
for (int i = 0; i < count; ++i)
{
const CCapturedResult* result = resultArray->GetResult(i);
if (result->GetErrorCode() == ErrorCode::EC_UNSUPPORTED_JSON_KEY_WARNING)
std::cout << "Warning: " << result->GetErrorCode() << ", " << result->GetErrorString() << std::endl;
else if (result->GetErrorCode() != ErrorCode::EC_OK)
std::cout << "Error: " << result->GetErrorCode() << ", " << result->GetErrorString() << std::endl;
CParsedResult* parsedResult = result->GetParsedResult();
if (parsedResult)
{
int itemCount = parsedResult->GetItemsCount();
for (int j = 0; j < itemCount; ++j)
{
const CParsedResultItem* item = parsedResult->GetItem(j);
const CCapturedResultItem* referenceItem = item->GetReferenceItem();
if (referenceItem && referenceItem->GetType() == CapturedResultItemType::CRIT_BARCODE)
{
const CBarcodeResultItem* barcodeItem = static_cast<const CBarcodeResultItem*>(referenceItem);
std::cout << "Barcode result: " << barcodeItem->GetText() << std::endl;
}
GS1AIResult gs1Result(item);
std::cout << gs1Result.toString();
}
if (noResultPrint)
noResultPrint = false;
parsedResult->Release();
}
}
if (noResultPrint)
{
std::cout << "No GS1 Application Identifiers (AIs) barcode found in the image." << std::endl;
}
resultArray->Release();
}
}
delete cvRouter, cvRouter = nullptr;
return 0;
}