-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathmain.cpp
More file actions
498 lines (440 loc) · 14.9 KB
/
main.cpp
File metadata and controls
498 lines (440 loc) · 14.9 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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
#include <stdio.h>
#include <string>
#include <vector>
#include <filesystem>
#include <algorithm>
#if defined(_WIN32) || defined(_WIN64)
#include <windows.h>
#include <conio.h>
#include <io.h>
#else
#include <cstring>
#include <dirent.h>
#include <sys/time.h>
#endif
#include <fstream>
#include <streambuf>
#include <iostream>
#include <sstream>
#include "DynamsoftCaptureVisionRouter.h"
#include "DynamsoftUtility.h"
using namespace std;
using namespace dynamsoft::license;
using namespace dynamsoft::cvr;
using namespace dynamsoft::dbr;
using namespace dynamsoft::utility;
using namespace dynamsoft::basic_structures;
class MyCapturedResultReceiver : public CCapturedResultReceiver
{
virtual void OnDecodedBarcodesReceived(CDecodedBarcodesResult *pResult) override
{
if (pResult->GetErrorCode() != EC_OK)
{
cout << "Error: " << pResult->GetErrorString() << endl;
}
else
{
auto tag = pResult->GetOriginalImageTag();
if (tag)
{
cout << "ImageID:" << tag->GetImageId() << endl;
CFileImageTag *fileTag = (CFileImageTag *)tag;
cout << "Page number:" << fileTag->GetPageNumber() << endl;
}
int count = pResult->GetItemsCount();
cout << "Decoded " << count << " barcodes" << endl;
for (int i = 0; i < count; i++)
{
const CBarcodeResultItem *barcodeResultItem = pResult->GetItem(i);
if (barcodeResultItem != NULL)
{
cout << "Result " << i + 1 << endl;
cout << "Barcode Format: " << barcodeResultItem->GetFormatString() << endl;
cout << "Barcode Text: " << barcodeResultItem->GetText() << endl;
CQuadrilateral location = barcodeResultItem->GetLocation();
CPoint *points = location.points;
for (int j = 0; j < 4; j++)
{
cout << "Point " << j + 1 << ": (" << points[j][0] << ", " << points[j][1] << ")" << endl;
}
}
}
}
cout << endl;
}
};
class MyImageSourceStateListener : public CImageSourceStateListener
{
private:
CCaptureVisionRouter *m_router;
public:
MyImageSourceStateListener(CCaptureVisionRouter *router)
{
m_router = router;
}
virtual void OnImageSourceStateReceived(ImageSourceState state)
{
if (state == ISS_EXHAUSTED)
m_router->StopCapturing();
}
};
bool GetImagePath(char *pImagePath)
{
std::string input;
while (true)
{
std::cout << "\n>> Step 1: Input your image file's full path or directory path:\n";
std::getline(std::cin, input);
// Trim whitespace and remove surrounding quotes if present
input.erase(0, input.find_first_not_of(" \t\n\r\"\'")); // Trim leading
input.erase(input.find_last_not_of(" \t\n\r\"\'") + 1); // Trim trailing
// Exit if user inputs 'q' or 'Q'
if (input == "q" || input == "Q")
{
return true; // Exit flag
}
// Copy input to pImagePath ensuring not to exceed buffer size
if (input.length() < 512)
{
strncpy(pImagePath, input.c_str(), 511);
pImagePath[input.length()] = '\0';
}
else
{
input = input.substr(0, 511); // Truncate if too long
strncpy(pImagePath, input.c_str(), 511);
pImagePath[511] = '\0';
}
// Check if file or directory exists
if (filesystem::exists(pImagePath))
{
return false; // Path is valid
}
std::cout << "Please input a valid file or directory path.\n";
}
}
bool endsWith(const std::string &str, const std::string &suffix)
{
// Check if the suffix length is greater than the string length
if (suffix.size() > str.size())
{
return false;
}
// Compare the end of the string with the suffix
return str.compare(str.size() - suffix.size(), suffix.size(), suffix) == 0;
}
// Structure to hold processing statistics
struct ProcessingStats
{
int totalImages = 0;
int imagesWithBarcodes = 0;
int totalBarcodes = 0;
vector<string> failedFiles;
};
// Check if a file is a supported image format
bool isSupportedImageFile(const string &filePath)
{
string extension = filesystem::path(filePath).extension().string();
transform(extension.begin(), extension.end(), extension.begin(), ::tolower);
return extension == ".jpg" || extension == ".jpeg" || extension == ".png" ||
extension == ".bmp" || extension == ".tif" || extension == ".tiff" ||
extension == ".gif" || extension == ".pdf";
}
// Get all supported image files from a directory
vector<string> getImageFilesFromDirectory(const string &dirPath)
{
vector<string> imageFiles;
try
{
for (const auto &entry : filesystem::recursive_directory_iterator(dirPath))
{
if (entry.is_regular_file() && isSupportedImageFile(entry.path().string()))
{
imageFiles.push_back(entry.path().string());
}
}
}
catch (const filesystem::filesystem_error &e)
{
cout << "Error accessing directory: " << e.what() << endl;
}
return imageFiles;
}
// Process a single image file and return the number of barcodes found
int processImageFile(CCaptureVisionRouter *cvr, const string &filePath)
{
CCapturedResultArray *captureResultArray = cvr->CaptureMultiPages(filePath.c_str(), CPresetTemplate::PT_READ_BARCODES);
int totalBarcodes = 0;
int count = captureResultArray->GetResultsCount();
for (int i = 0; i < count; i++)
{
const CCapturedResult *result = captureResultArray->GetResult(i);
if (result->GetErrorCode() != 0)
{
continue;
}
CDecodedBarcodesResult *barcodeResult = result->GetDecodedBarcodesResult();
if (barcodeResult != nullptr && barcodeResult->GetErrorCode() == 0)
{
int itemCount = barcodeResult->GetItemsCount();
totalBarcodes += itemCount;
}
if (barcodeResult)
barcodeResult->Release();
}
captureResultArray->Release();
return totalBarcodes;
}
// Display detailed results for a single image (original behavior)
void displayDetailedResults(CCaptureVisionRouter *cvr, const string &filePath)
{
cout << "Processing file: " << filePath << endl;
CCapturedResultArray *captureResultArray = cvr->CaptureMultiPages(filePath.c_str(), CPresetTemplate::PT_READ_BARCODES);
int count = captureResultArray->GetResultsCount();
for (int i = 0; i < count; i++)
{
const CCapturedResult *result = captureResultArray->GetResult(i);
result->GetOriginalImageTag()->GetImageId();
cout << ">>>>>>>>>>>>>>>>> Image " << i + 1 << ":" << endl;
cout << result->GetErrorString() << endl;
if (result->GetErrorCode() != 0)
continue;
CDecodedBarcodesResult *barcodeResult = result->GetDecodedBarcodesResult();
if (barcodeResult != nullptr && barcodeResult->GetErrorCode() == 0)
{
int itemCount = barcodeResult->GetItemsCount();
cout << "Decoded " << itemCount << " barcodes" << endl;
for (int j = 0; j < itemCount; j++)
{
const CBarcodeResultItem *barcodeResultItem = barcodeResult->GetItem(j);
cout << "Result " << j + 1 << endl;
cout << "Barcode Format: " << barcodeResultItem->GetFormatString() << endl;
cout << "Barcode Text: " << barcodeResultItem->GetText() << endl;
CQuadrilateral location = barcodeResultItem->GetLocation();
CPoint *points = location.points;
for (int k = 0; k < 4; k++)
{
cout << "Point " << k + 1 << ": (" << points[k][0] << ", " << points[k][1] << ")" << endl;
}
}
}
if (barcodeResult)
barcodeResult->Release();
}
captureResultArray->Release();
}
int main(int argc, char *argv[])
{
const char *version = CBarcodeReaderModule::GetVersion();
printf("*************************************************\r\n");
printf("Welcome to Dynamsoft Barcode v%s Demo \r\n", version);
printf("*************************************************\r\n");
printf("Supports both single files and directories.\r\n");
printf("Usage: %s [file_or_directory1] [file_or_directory2] ...\r\n", argc > 0 ? argv[0] : "barcode_scanner");
printf("Or run without arguments for interactive mode.\r\n");
printf("Hints: Please input 'Q' or 'q' to quit the application.\r\n");
int iRet = -1;
char szErrorMsg[256];
// Initialize license.
// Request a trial from https://www.dynamsoft.com/customer/license/trialLicense/?product=dcv&package=cross-platform
iRet = CLicenseManager::InitLicense("DLS2eyJoYW5kc2hha2VDb2RlIjoiMjAwMDAxLTE2NDk4Mjk3OTI2MzUiLCJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSIsInNlc3Npb25QYXNzd29yZCI6IndTcGR6Vm05WDJrcEQ5YUoifQ==", szErrorMsg, 256);
if (iRet != EC_OK)
{
cout << szErrorMsg << endl;
}
int errorCode = 0;
char errorMsg[512] = {0};
CCaptureVisionRouter *cvr = new CCaptureVisionRouter;
errorCode = cvr->InitSettingsFromFile("Templates/DBR-PresetTemplates.json", errorMsg, 512);
if (errorCode != EC_OK)
{
cout << "error:" << errorMsg << endl;
return -1;
}
char pszImageFile[512] = {0};
bool bExit = false;
CImageSourceStateListener *listener = new MyImageSourceStateListener(cvr);
CFileFetcher *fileFetcher = new CFileFetcher();
cvr->SetInput(fileFetcher);
CCapturedResultReceiver *capturedReceiver = new MyCapturedResultReceiver;
cvr->AddResultReceiver(capturedReceiver);
cvr->AddImageSourceStateListener(listener);
// Check if command line arguments are provided
if (argc > 1)
{
// Command line mode - process all provided arguments
ProcessingStats totalStats;
for (int argIndex = 1; argIndex < argc; argIndex++)
{
string inputPath = argv[argIndex];
if (filesystem::is_directory(inputPath))
{
// Process directory
cout << "\n=== Processing Directory: " << inputPath << " ===" << endl;
vector<string> imageFiles = getImageFilesFromDirectory(inputPath);
ProcessingStats dirStats;
dirStats.totalImages = imageFiles.size();
if (imageFiles.empty())
{
cout << "No supported image files found in directory." << endl;
continue;
}
cout << "Found " << imageFiles.size() << " image files. Processing..." << endl;
for (const string &filePath : imageFiles)
{
try
{
int barcodeCount = processImageFile(cvr, filePath);
if (barcodeCount > 0)
{
dirStats.imagesWithBarcodes++;
dirStats.totalBarcodes += barcodeCount;
cout << filesystem::path(filePath).filename().string()
<< " (" << barcodeCount << " barcodes)" << endl;
}
else
{
cout << filesystem::path(filePath).filename().string()
<< " (no barcodes)" << endl;
}
}
catch (const exception &e)
{
dirStats.failedFiles.push_back(filePath);
cout << "✗ " << filesystem::path(filePath).filename().string()
<< " (error: " << e.what() << ")" << endl;
}
}
// Display directory statistics
cout << "\n--- Directory Statistics ---" << endl;
cout << "Total images processed: " << dirStats.totalImages << endl;
cout << "Images with barcodes: " << dirStats.imagesWithBarcodes << endl;
cout << "Images without barcodes: " << (dirStats.totalImages - dirStats.imagesWithBarcodes - dirStats.failedFiles.size()) << endl;
if (!dirStats.failedFiles.empty())
{
cout << "Failed to process: " << dirStats.failedFiles.size() << endl;
}
cout << "Total barcodes found: " << dirStats.totalBarcodes << endl;
if (dirStats.totalImages > 0)
{
cout << "Success rate: " << (double(dirStats.imagesWithBarcodes) / dirStats.totalImages * 100.0) << "%" << endl;
}
// Add to total stats
totalStats.totalImages += dirStats.totalImages;
totalStats.imagesWithBarcodes += dirStats.imagesWithBarcodes;
totalStats.totalBarcodes += dirStats.totalBarcodes;
totalStats.failedFiles.insert(totalStats.failedFiles.end(),
dirStats.failedFiles.begin(), dirStats.failedFiles.end());
}
else if (filesystem::is_regular_file(inputPath) && isSupportedImageFile(inputPath))
{
// Process single file with detailed output
cout << "\n=== Processing File: " << inputPath << " ===" << endl;
displayDetailedResults(cvr, inputPath);
// Update total stats
totalStats.totalImages++;
int barcodeCount = processImageFile(cvr, inputPath);
if (barcodeCount > 0)
{
totalStats.imagesWithBarcodes++;
totalStats.totalBarcodes += barcodeCount;
}
}
else
{
cout << "Warning: '" << inputPath << "' is not a valid file or directory, or not a supported image format." << endl;
}
}
// Display overall statistics if multiple inputs were processed
if (argc > 2)
{
cout << "\n=== Overall Statistics ===" << endl;
cout << "Total images processed: " << totalStats.totalImages << endl;
cout << "Images with barcodes: " << totalStats.imagesWithBarcodes << endl;
cout << "Images without barcodes: " << (totalStats.totalImages - totalStats.imagesWithBarcodes - totalStats.failedFiles.size()) << endl;
if (!totalStats.failedFiles.empty())
{
cout << "Failed to process: " << totalStats.failedFiles.size() << endl;
}
cout << "Total barcodes found: " << totalStats.totalBarcodes << endl;
if (totalStats.totalImages > 0)
{
cout << "Overall success rate: " << (double(totalStats.imagesWithBarcodes) / totalStats.totalImages * 100.0) << "%" << endl;
}
}
}
else
{
// Interactive mode - original behavior with directory support
while (1)
{
bExit = GetImagePath(pszImageFile);
if (bExit)
break;
string inputPath = string(pszImageFile);
if (filesystem::is_directory(inputPath))
{
// Process directory in interactive mode
cout << "\nProcessing directory: " << inputPath << endl;
vector<string> imageFiles = getImageFilesFromDirectory(inputPath);
ProcessingStats dirStats;
dirStats.totalImages = imageFiles.size();
if (imageFiles.empty())
{
cout << "No supported image files found in directory." << endl;
continue;
}
cout << "Found " << imageFiles.size() << " image files. Processing..." << endl;
for (const string &filePath : imageFiles)
{
try
{
int barcodeCount = processImageFile(cvr, filePath);
if (barcodeCount > 0)
{
dirStats.imagesWithBarcodes++;
dirStats.totalBarcodes += barcodeCount;
cout << "✓ " << filesystem::path(filePath).filename().string()
<< " (" << barcodeCount << " barcodes)" << endl;
}
else
{
cout << "✗ " << filesystem::path(filePath).filename().string()
<< " (no barcodes)" << endl;
}
}
catch (const exception &e)
{
dirStats.failedFiles.push_back(filePath);
cout << "✗ " << filesystem::path(filePath).filename().string()
<< " (error: " << e.what() << ")" << endl;
}
}
// Display directory statistics
cout << "\n--- Directory Statistics ---" << endl;
cout << "Total images processed: " << dirStats.totalImages << endl;
cout << "Images with barcodes: " << dirStats.imagesWithBarcodes << endl;
cout << "Images without barcodes: " << (dirStats.totalImages - dirStats.imagesWithBarcodes - dirStats.failedFiles.size()) << endl;
if (!dirStats.failedFiles.empty())
{
cout << "Failed to process: " << dirStats.failedFiles.size() << endl;
}
cout << "Total barcodes found: " << dirStats.totalBarcodes << endl;
if (dirStats.totalImages > 0)
{
cout << "Success rate: " << (double(dirStats.imagesWithBarcodes) / dirStats.totalImages * 100.0) << "%" << endl;
}
}
else
{
// Process single file with detailed output (original behavior)
displayDetailedResults(cvr, inputPath);
}
}
}
delete cvr;
delete listener;
delete capturedReceiver;
delete fileFetcher;
return 0;
}