forked from Dynamsoft/barcode-reader-c-cpp-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVideoDecoding.cpp
146 lines (117 loc) · 4.17 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
#include <opencv2/core.hpp>
#include <opencv2/videoio.hpp>
#include <opencv2/highgui.hpp>
#include <iostream>
#include <vector>
// Include headers of DynamsoftBarcodeReader SDK
#include "../../../include/DynamsoftBarcodeReader.h"
#include "../../../include/DynamsoftCommon.h"
using namespace cv;
using namespace std;
using std::cerr;
using std::endl;
using std::cin;
// Link libraries of DynamsoftBarcodeReader SDK for Windows.
#if defined(_WIN64) || defined(_WIN32)
#ifdef _WIN64
#pragma comment(lib, "../../../lib/Windows/x64/DBRx64.lib")
#else
#pragma comment(lib, "../../../lib/Windows/x86/DBRx86.lib")
#endif
#endif
using namespace std;
using namespace dynamsoft::dbr;
/// <summary>
/// This is the callback function to process the decoded barcode result
/// It will be triggled when barcodes are detected in a video frame
/// </summary>
void textResultCallback(int frameId, TextResultArray * barcodeResults, void * pUser)
{
cout << endl << "frame = " << frameId << ", " << barcodeResults->resultsCount << " Total barcode(s) found: " << endl;
for (int iIndex = 0; iIndex < barcodeResults->resultsCount; iIndex++)
{
cout << "Result " << (iIndex + 1) << ":" << endl;
// Get format of each barcode
cout << " Barcode Format: " << barcodeResults->results[iIndex]->barcodeFormatString << endl;
// Get text result of each barcode
cout << " Barcode Text: " << barcodeResults->results[iIndex]->barcodeText << endl;
}
// 5. Free the memory allocated for text results
CBarcodeReader::FreeTextResults(&barcodeResults);
}
/// <summary>
/// This is the callback function to process the error returned
/// It will be triggled when error occurs when decoding a video frame
/// </summary>
void errorCallback(int frameId, int errorCode, void* pUser)
{
if (errorCode != DBR_OK)
{
cout << endl << "frame = " << frameId << ", failed to read barcode: " << CBarcodeReader::GetErrorString(errorCode) << endl;
return;
}
}
int main()
{
Mat frame;
cout << "Opening camera..." << endl;
VideoCapture capture(0); // open the first camera
if (!capture.isOpened())
{
cerr << "ERROR: Can't initialize camera capture" << endl;
//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 = CBarcodeReader::InitLicense("DLS2eyJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSJ9", szErrorMsg, 256);
if (iRet != DBR_OK)
{
cout << szErrorMsg << endl;
}
// Create an instance of Barcode Reader
CBarcodeReader dbr;
// Set the callback functions to process the barcode result and error returned by Dynamsoft Barcode Reader SDK
dbr.SetTextResultCallback(textResultCallback, NULL);
dbr.SetErrorCallback(errorCallback, NULL);
// Initialize a group of parameters for video decoding
FrameDecodingParameters parameters;
dbr.InitFrameDecodingParameters(¶meters);
capture >> frame;
parameters.width = capture.get(CAP_PROP_FRAME_WIDTH);
parameters.height = capture.get(CAP_PROP_FRAME_HEIGHT);
parameters.stride = frame.step.p[0];
parameters.imagePixelFormat = IPF_RGB_888;
//parameters.maxQueueLength = 10;
//parameters.maxResultQueueLength = 10;
//parameters.region.regionMeasuredByPercentage = 1;
//parameters.region.regionTop = 0;
//parameters.region.regionBottom = 100;
//parameters.region.regionLeft = 0;
//parameters.region.regionRight = 100;
//parameters.threshold =0.01;
//parameters.fps = 0;
// Start a new thread to decode barcodes from the inner frame queue
iRet = dbr.StartFrameDecodingEx(parameters, "");
for(;;)
{
// Read the next frame from camera
capture >> frame;
if (frame.empty())
{
cerr << "ERROR: Can't grab camera frame." << endl;
break;
}
// Append a frame image buffer to the inner frame queue
dbr.AppendFrame(frame.data);
imshow("Frame", frame);
int key = waitKey(40);
if (key == 27/*ESC*/)
break;
}
// Stop the frame decoding thread created by StartFrameDecodingEx
dbr.StopFrameDecoding();
return 0;
}