-
Notifications
You must be signed in to change notification settings - Fork 5
/
VideoDecoding.cpp
175 lines (162 loc) · 5.36 KB
/
VideoDecoding.cpp
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
#include <opencv2/core.hpp>
#include <opencv2/videoio.hpp>
#include <opencv2/highgui.hpp>
#include <iostream>
#include <vector>
#include <chrono>
// Include headers of DynamsoftCaptureVisionRouter SDK
#include <iostream>
#include <string>
#include "../../Include/DynamsoftCaptureVisionRouter.h"
#include "../../Include/DynamsoftUtility.h"
using namespace std;
using namespace dynamsoft::license;
using namespace dynamsoft::cvr;
using namespace dynamsoft::dbr;
using namespace dynamsoft::utility;
using namespace cv;
#if defined(_WIN64) || defined(_WIN32)
#ifdef _WIN64
#pragma comment(lib, "../../Distributables/Lib/Windows/x64/DynamsoftCorex64.lib")
#pragma comment(lib, "../../Distributables/Lib/Windows/x64/DynamsoftLicensex64.lib")
#pragma comment(lib, "../../Distributables/Lib/Windows/x64/DynamsoftCaptureVisionRouterx64.lib")
#pragma comment(lib, "../../Distributables/Lib/Windows/x64/DynamsoftUtilityx64.lib")
#else
#pragma comment(lib, "../../Distributables/Lib/Windows/x86/DynamsoftCorex86.lib")
#pragma comment(lib, "../../Distributables/Lib/Windows/x86/DynamsoftLicensex86.lib")
#pragma comment(lib, "../../Distributables/Lib/Windows/x86/DynamsoftCaptureVisionRouterx86.lib")
#pragma comment(lib, "../../Distributables/Lib/Windows/x86/DynamsoftUtilityx86.lib")
#endif
#endif
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;
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;
}
}
}
cout << endl;
}
};
class MyVideoFetcher : public CImageSourceAdapter
{
public:
MyVideoFetcher(){};
~MyVideoFetcher(){};
bool HasNextImageToFetch() const override
{
return true;
}
void MyAddImageToBuffer(const CImageData *img, bool bClone = true)
{
AddImageToBuffer(img, bClone);
}
};
int main()
{
cout << "Opening camera..." << endl;
VideoCapture capture(0); // open the first camera
if (!capture.isOpened())
{
cerr << "ERROR: Can't initialize camera capture" << endl;
cout << "Press any key to quit..." << endl;
cin.ignore();
return 1;
}
int iRet = -1;
char szErrorMsg[256];
// Initialize license.
// The string "DLS2eyJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSJ9" here is a free public trial license. Note that network connection is required for this license to work.
// If you don't have a license yet, you can request a trial from https://www.dynamsoft.com/customer/license/trialLicense?product=dbr&utm_source=samples&package=c_cpp
iRet = CLicenseManager::InitLicense("DLS2eyJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSJ9", szErrorMsg, 256);
if (iRet != ErrorCode::EC_OK && iRet != ErrorCode::EC_LICENSE_CACHE_USED)
{
cout << "License initialization failed: ErrorCode: " << iRet << ", ErrorString: " << szErrorMsg << endl;
}
else
{
int errorCode = 1;
char errorMsg[512] = {0};
CCaptureVisionRouter *cvr = new CCaptureVisionRouter;
MyVideoFetcher *fetcher = new MyVideoFetcher();
fetcher->SetMaxImageCount(100);
fetcher->SetBufferOverflowProtectionMode(BOPM_UPDATE);
fetcher->SetColourChannelUsageType(CCUT_AUTO);
cvr->SetInput(fetcher);
CMultiFrameResultCrossFilter *filter = new CMultiFrameResultCrossFilter;
filter->EnableResultCrossVerification(CRIT_BARCODE, true);
filter->EnableResultDeduplication(CRIT_BARCODE, true);
filter->SetDuplicateForgetTime(CRIT_BARCODE, 5000);
cvr->AddResultFilter(filter);
CCapturedResultReceiver *capturedReceiver = new MyCapturedResultReceiver;
cvr->AddResultReceiver(capturedReceiver);
errorCode = cvr->StartCapturing(CPresetTemplate::PT_READ_BARCODES, false, errorMsg, 512);
if (errorCode != EC_OK)
{
cout << "error:" << errorMsg << endl;
}
else
{
for (int i = 1;; ++i)
{
auto start = std::chrono::high_resolution_clock::now();
Mat frame;
capture.read(frame);
if (frame.empty())
{
cerr << "ERROR: Can't grab camera frame." << endl;
break;
}
CFileImageTag tag(nullptr, 0, 0);
tag.SetImageId(i);
CImageData data(frame.rows * frame.step.p[0],
frame.data,
capture.get(CAP_PROP_FRAME_WIDTH),
capture.get(CAP_PROP_FRAME_HEIGHT),
frame.step.p[0],
IPF_RGB_888,
0,
&tag);
fetcher->MyAddImageToBuffer(&data);
imshow("Frame", frame);
auto finish = std::chrono::high_resolution_clock::now();
auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(finish - start);
int usingTime = elapsed.count();
int key = 1;
if (usingTime < 40)
key = 40 - usingTime;
key = waitKey(key);
if (key == 27 /*ESC*/)
break;
}
cvr->StopCapturing(false, true);
}
capture.release();
delete cvr, cvr = NULL;
delete fetcher, fetcher = NULL;
delete filter, filter = NULL;
delete capturedReceiver, capturedReceiver = NULL;
}
cout << "Press any key to quit..." << endl;
cin.ignore();
return 0;
}