-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathMultiThreadDecoding.cpp
More file actions
221 lines (179 loc) · 5.91 KB
/
MultiThreadDecoding.cpp
File metadata and controls
221 lines (179 loc) · 5.91 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
220
221
#include <iostream>
#include <string>
#include <vector>
#include <thread>
#include <mutex>
#include "../../Include/DynamsoftCaptureVisionRouter.h"
using namespace std;
using namespace dynamsoft::cvr;
using namespace dynamsoft::dbr;
using namespace dynamsoft::license;
#if defined(_WIN64) || defined(_WIN32)
#include <windows.h>
#include <io.h>
#ifdef _WIN64
#pragma comment(lib, "../../Dist/Lib/Windows/x64/DynamsoftCaptureVisionRouterx64.lib")
#pragma comment(lib, "../../Dist/Lib/Windows/x64/DynamsoftLicensex64.lib")
#pragma comment(lib, "../../Dist/Lib/Windows/x64/DynamsoftCorex64.lib")
#else
#pragma comment(lib, "../../Dist/Lib/Windows/x86/DynamsoftCaptureVisionRouterx86.lib")
#pragma comment(lib, "../../Dist/Lib/Windows/x86/DynamsoftLicensex86.lib")
#pragma comment(lib, "../../Dist/Lib/Windows/x86/DynamsoftCorex86.lib")
#endif
#else
#include <string.h>
#include <dirent.h>
#endif
std::mutex coutMutex;
void ThreadDecodeFile(const char *filePath)
{
CCaptureVisionRouter cvRouter;
CCapturedResultArray* resultArray = cvRouter.CaptureMultiPages(filePath, CPresetTemplate::PT_READ_BARCODES);
std::lock_guard<std::mutex> lock(coutMutex);
cout << "Thread: " << this_thread::get_id() << endl;
cout << "File Name: " << filePath << endl;
int count = resultArray->GetResultsCount();
bool noResultPrint = true;
for (int i = 0; i < count; ++i)
{
const CCapturedResult* result = resultArray->GetResult(i);
int pageNumber = i + 1;
// It is usually necessary to determine 'ImageTagType' based on the original image tag.
// Since imageFile is used, it is directly converted to 'const dynamsoft::basic_structures::CFileImageTag *'.
const CFileImageTag *tag = dynamic_cast<const CFileImageTag *>(result->GetOriginalImageTag());
if(tag != nullptr)
{
pageNumber = tag->GetPageNumber() + 1;
}
cout << "Page " << pageNumber << ":" << endl;
cout << "Error: " << result->GetErrorCode() << ", " << result->GetErrorString() << endl;
CDecodedBarcodesResult* barcodeResult = result->GetDecodedBarcodesResult();
if (barcodeResult == nullptr || barcodeResult->GetItemsCount() == 0)
{
cout << "No barcode found in page " << pageNumber << endl;
}
else
{
cout << "Total barcode(s) found in page " << pageNumber << ": " << barcodeResult->GetItemsCount() << endl;
for (int index = 0; index < barcodeResult->GetItemsCount(); ++index)
{
const CBarcodeResultItem* barcode = barcodeResult->GetItem(index);
cout << index + 1 << ": " << barcode->GetFormatString() << ", " << barcode->GetText() << endl;
}
barcodeResult->Release();
}
cout << endl;
}
if (resultArray != nullptr)
resultArray->Release();
cout << endl;
}
void MultiThreadDecoding(const vector<string> &fileList)
{
const int THREAD_COUNT = 4;
const std::size_t filesPerThread = fileList.size() / THREAD_COUNT;
std::vector<std::thread> threads;
for (int i = 0; i < THREAD_COUNT; ++i)
{
std::size_t startIdx = i * filesPerThread;
std::size_t endIdx = (i == THREAD_COUNT - 1) ? fileList.size() : (startIdx + filesPerThread);
threads.emplace_back([&fileList, startIdx, endIdx]()
{
for (std::size_t j = startIdx; j < endIdx; ++j) {
ThreadDecodeFile(fileList[j].c_str());
} });
}
for (auto &thread : threads)
{
thread.join();
}
}
#if defined(_WIN64) || defined(_WIN32)
void GetFiles(const string &imagePath, vector<string> &files)
{
intptr_t hFile = 0;
struct _finddata_t fileinfo;
size_t len = strlen(imagePath.c_str());
string seachPath = imagePath + "\\*";
if ((hFile = _findfirst(seachPath.c_str(), &fileinfo)) != -1)
{
do
{
if ((fileinfo.attrib & _A_SUBDIR))
{
}
else
{
string strFilePath = imagePath + "\\" + fileinfo.name;
files.push_back(strFilePath);
}
} while (_findnext(hFile, &fileinfo) == 0);
_findclose(hFile);
}
}
#else
void GetFiles(const string &imagePath, vector<string> &files)
{
DIR *dir;
struct dirent *ptr;
if ((dir = opendir(imagePath.c_str())) == NULL)
{
return;
}
while ((ptr = readdir(dir)) != NULL)
{
if (strcmp(ptr->d_name, ".") == 0 || strcmp(ptr->d_name, "..") == 0)
continue;
if (ptr->d_type == 8)
{
string strFilePath = imagePath + "/" + ptr->d_name;
files.push_back(strFilePath);
}
}
}
#endif
int main(int argc, const char *argv[])
{
int errorCode = 0;
char szErrorMsg[256];
cout << "***************************************************************" << endl;
cout << "Welcome to Dynamsoft Barcode Reader MultiThreadDecoding Sample" << endl;
cout << "***************************************************************" << endl;
cout << "Hints: Please input 'Q' or 'q' to quit the application." << endl;
// 1.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", szErrorMsg, 256);
if (errorCode != ErrorCode::EC_OK && errorCode != ErrorCode::EC_LICENSE_WARNING)
{
cout << "License initialization failed: ErrorCode: " << errorCode << ", ErrorString: " << szErrorMsg << endl;
}
else
{
while (1)
{
cout << ">> Input your image folder's full path:" << endl;
string imagePath;
getline(cin, imagePath);
if (imagePath.empty())
{
cout << "Empty input, please try again." << endl;
continue;
}
if (imagePath == "q" || imagePath == "Q")
break;
if (imagePath.length() >= 2 && imagePath.front() == '"' && imagePath.back() == '"')
imagePath = imagePath.substr(1, imagePath.length() - 2);
vector<string> vecFiles;
GetFiles(imagePath, vecFiles);
if (vecFiles.empty())
{
cout << "No image files found, please try again." << endl;
continue;
}
MultiThreadDecoding(vecFiles);
cout << "Finish." << endl;
}
}
return 0;
}