-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathShowLocalizedVSDecodedBarcodes.cpp
More file actions
219 lines (197 loc) · 7.99 KB
/
ShowLocalizedVSDecodedBarcodes.cpp
File metadata and controls
219 lines (197 loc) · 7.99 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
217
218
219
#include <iostream>
#include <vector>
#include <string>
#include <tuple>
#include <set>
#include "../../Include/DynamsoftCaptureVisionRouter.h"
#include "../../Include/DynamsoftUtility.h"
using namespace dynamsoft;
using namespace dynamsoft::cvr;
using namespace dynamsoft::license;
using namespace dynamsoft::intermediate_results;
using namespace dynamsoft::utility;
using namespace dynamsoft::dbr;
#ifndef MYMAX
#define MYMAX(a,b) (((a) > (b)) ? (a) : (b))
#endif // !MYMAX
#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")
#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")
#endif
#endif
class MyIntermediateResultReceiver : public CIntermediateResultReceiver {
public:
std::vector<CQuadrilateral> locations;
void OnLocalizedBarcodesReceived(dbr::intermediate_results::CLocalizedBarcodesUnit* result, const IntermediateResultExtraInfo* info) override {
if (info->isSectionLevelResult) {
for (int i = 0; i < result->GetCount(); ++i) {
locations.push_back(result->GetLocalizedBarcode(i)->GetLocation());
}
}
}
};
bool isWithin10Percent(double area1, double area2) {
if (area1 == 0 || area2 == 0) return false;
double ratio = abs(area1 - area2) / MYMAX(area1, area2);
return ratio <= 0.10;
}
bool seemAsSameLocation(const CQuadrilateral& location, const CQuadrilateral& resultLoc) {
CPoint centralPoint(0, 0);
for (const auto& point : location.points) {
centralPoint[0] += point[0];
centralPoint[1] += point[1];
}
centralPoint[0] /= 4;
centralPoint[1] /= 4;
if (!resultLoc.Contains(¢ralPoint)) return false;
if (isWithin10Percent(location.GetArea(), resultLoc.GetArea())) return true;
return false;
}
std::tuple<std::vector<CQuadrilateral>, std::vector<CQuadrilateral>> removeIfLocExistsInResultLocs(
const std::vector<CQuadrilateral>& locations,
const std::vector<CQuadrilateral>& resultLocs)
{
if (locations.empty() || resultLocs.empty()) {
return std::make_tuple(locations, resultLocs);
}
std::vector<CQuadrilateral> retLoc, retResLoc;
std::set<int> excludedLoc, includeResLocs;
for (size_t i = 0; i < locations.size(); ++i) {
for (size_t j = 0; j < resultLocs.size(); ++j) {
if (seemAsSameLocation(locations[i], resultLocs[j])) {
includeResLocs.insert(j);
excludedLoc.insert(i);
break;
}
}
}
for (int j : includeResLocs) {
retResLoc.push_back(resultLocs[j]);
}
for (size_t i = 0; i < locations.size(); ++i) {
if (excludedLoc.find(i) == excludedLoc.end()) {
retLoc.push_back(locations[i]);
}
}
return make_tuple(retLoc, retResLoc);
}
CImageData* drawOnImage(const CImageData* image, const std::vector<CQuadrilateral>& locations, const std::vector<CQuadrilateral>& resultLocs)
{
CImageData* ret = nullptr;
CImageDrawer drawer;
auto tupleLoc = removeIfLocExistsInResultLocs(locations, resultLocs);
if (!std::get<0>(tupleLoc).empty())
ret = drawer.DrawOnImage(image, std::get<0>(tupleLoc).data(), std::get<0>(tupleLoc).size(), 0xFFFF0000, 2);
if (!std::get<1>(tupleLoc).empty())
{
CImageData* temp = nullptr;
temp = drawer.DrawOnImage(ret ? ret : image, std::get<1>(tupleLoc).data(), std::get<1>(tupleLoc).size(), 0xFF00FF00, 2);
if (ret)
delete ret, ret = nullptr;
ret = temp;
}
return ret;
}
int main() {
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=dbr&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 << std::endl;
return -1;
}
CCaptureVisionRouter* cvRouter = new CCaptureVisionRouter();
MyIntermediateResultReceiver* irr = new MyIntermediateResultReceiver();
CIntermediateResultManager* irm = cvRouter->GetIntermediateResultManager();
irm->AddResultReceiver(irr);
std::string imagePath = "../../Images/GeneralBarcodes.png";
std::string resultImagePath = "../../Images/GeneralBarcodes_result.png";
SimplifiedCaptureVisionSettings settings;
cvRouter->GetSimplifiedSettings(CPresetTemplate::PT_READ_BARCODES, &settings);
settings.outputOriginalImage = 1;
cvRouter->UpdateSettings(CPresetTemplate::PT_READ_BARCODES, &settings, errorMessage, 1024);
CCapturedResult* result = cvRouter->Capture(imagePath.c_str(), CPresetTemplate::PT_READ_BARCODES);
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;
}
const CImageData* originalImage = nullptr;
for (int i = 0; i < result->GetItemsCount(); ++i)
{
const CCapturedResultItem* item = result->GetItem(i);
if (item && item->GetType() == CRIT_ORIGINAL_IMAGE)
{
const COriginalImageResultItem* originalResultItem = dynamic_cast<const COriginalImageResultItem*>(item);
originalImage = originalResultItem->GetImageData();
break;
}
}
CDecodedBarcodesResult* barcodeResult = result->GetDecodedBarcodesResult();
if (irr->locations.empty() && ((!barcodeResult) || barcodeResult->GetItemsCount() == 0))
{
std::cout << "No barcode found." << std::endl;
}
else
{
std::vector<CQuadrilateral> resultLocs;
if (barcodeResult)
{
int count = barcodeResult->GetItemsCount();
for (int i = 0; i < count; ++i)
{
const CBarcodeResultItem* item = barcodeResult->GetItem(i);
if (item)
resultLocs.emplace_back(item->GetLocation());
}
}
CImageIO io;
CImageData* drawedImage = nullptr;
if (originalImage)
{
drawedImage = drawOnImage(originalImage, irr->locations, resultLocs);
}
else
{
CImageData* image = io.ReadFromFile(imagePath.c_str());
drawedImage = drawOnImage(image, irr->locations, resultLocs);
if (image)
delete image, image = nullptr;
}
if (drawedImage)
{
io.SaveToFile(drawedImage, resultImagePath.c_str());
delete drawedImage, drawedImage = nullptr;
#ifdef _WIN32
system(("start " + resultImagePath).c_str());
#elif __APPLE__
system(("open " + resultImagePath).c_str());
#else
int ret = system(("xdg-open " + resultImagePath).c_str());
if (ret != 0)
{
std::cout << "Failed to open the result image. Please check the file manually: " << resultImagePath << std::endl;
}
#endif
}
}
if (result)
result->Release();
delete irr, irr = nullptr;
delete cvRouter, cvRouter = nullptr;
return 0;
}