-
Notifications
You must be signed in to change notification settings - Fork 432
/
Copy pathmain.cpp
266 lines (186 loc) · 7.05 KB
/
main.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
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
/*
* Copyright 2010-2011 Alessandro Francescon
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <string>
#include <exception>
#include <stdlib.h>
#include <zxing/common/Counted.h>
#include <zxing/Binarizer.h>
#include <zxing/MultiFormatReader.h>
#include <zxing/Result.h>
#include <zxing/ReaderException.h>
#include <zxing/common/GlobalHistogramBinarizer.h>
#include <zxing/Exception.h>
#include <zxing/common/IllegalArgumentException.h>
#include <zxing/BinaryBitmap.h>
#include <zxing/DecodeHints.h>
#include <zxing/qrcode/QRCodeReader.h>
#include <zxing/MultiFormatReader.h>
#include <zxing/MatSource.h>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
using namespace std;
using namespace zxing;
using namespace zxing::qrcode;
using namespace cv;
#if CV_MAJOR_VERSION >= 4
#ifndef CV_CAP_PROP_FRAME_WIDTH
#define CV_CAP_PROP_FRAME_WIDTH CAP_PROP_FRAME_WIDTH
#endif
#ifndef CV_CAP_PROP_FRAME_HEIGHT
#define CV_CAP_PROP_FRAME_HEIGHT CAP_PROP_FRAME_HEIGHT
#endif
#ifndef CV_BGR2GRAY
#define CV_BGR2GRAY COLOR_BGR2GRAY
#endif
#endif
void printUsage(char** argv) {
// Print usage
cout << "Usage: " << argv[0] << " [-d <DEVICE>] [-w <CAPTUREWIDTH>] [-h <CAPTUREHEIGHT>]" << endl
<< "Read QR code from given video device." << endl
<< endl;
}
Point toCvPoint(Ref<ResultPoint> resultPoint) {
return Point(resultPoint->getX(), resultPoint->getY());
}
int main(int argc, char** argv) {
int deviceId = 0;
int captureWidth = 640;
int captureHeight = 480;
bool multi = false;
for (int j = 0; j < argc; j++) {
// Get arg
string arg = argv[j];
if (arg.compare("-d") == 0) {
if ((j + 1) < argc) {
// Set device id
deviceId = atoi(argv[++j]);
} else {
// Log
cerr << "Missing device id after -d" << endl;
printUsage(argv);
return 1;
}
} else if (arg.compare("-w") == 0) {
if ((j + 1) < argc) {
// Set capture width
captureWidth = atoi(argv[++j]);
} else {
// Log
cerr << "Missing width after -w" << endl;
printUsage(argv);
return 1;
}
} else if (arg.compare("-h") == 0) {
if ((j + 1) < argc) {
// Set capture height
captureHeight = atoi(argv[++j]);
} else {
// Log
cerr << "Missing height after -h" << endl;
printUsage(argv);
return 1;
}
} else if (arg.compare("-m") == 0) {
// Set multi to true
multi = true;
}
}
// Log
cout << "Capturing from device " << deviceId << "..." << endl;
// Open video captire
VideoCapture videoCapture(deviceId);
if (!videoCapture.isOpened()) {
// Log
cerr << "Open video capture failed on device id: " << deviceId << endl;
return -1;
}
if (!videoCapture.set(CV_CAP_PROP_FRAME_WIDTH, captureWidth)) {
// Log
cerr << "Failed to set frame width: " << captureWidth << " (ignoring)" << endl;
}
if (!videoCapture.set(CV_CAP_PROP_FRAME_HEIGHT, captureHeight)) {
// Log
cerr << "Failed to set frame height: " << captureHeight << " (ignoring)" << endl;
}
// The captured image and its grey conversion
Mat image, grey;
// Open output window
namedWindow("ZXing", cv::WINDOW_AUTOSIZE);
// Stopped flag will be set to -1 from subsequent wayKey() if no key was pressed
int stopped = -1;
while (stopped == -1) {
// Capture image
bool result = videoCapture.read(image);
if (result) {
// Convert to grayscale
cvtColor(image, grey, CV_BGR2GRAY);
try {
// Create luminance source
Ref<LuminanceSource> source = MatSource::create(grey);
// Search for QR code
Ref<Reader> reader;
if (multi) {
reader.reset(new MultiFormatReader);
} else {
reader.reset(new QRCodeReader);
}
Ref<Binarizer> binarizer(new GlobalHistogramBinarizer(source));
Ref<BinaryBitmap> bitmap(new BinaryBitmap(binarizer));
Ref<Result> result(reader->decode(bitmap, DecodeHints(DecodeHints::TRYHARDER_HINT)));
// Get result point count
int resultPointCount = result->getResultPoints()->size();
for (int j = 0; j < resultPointCount; j++) {
// Draw circle
circle(image, toCvPoint(result->getResultPoints()[j]), 10, Scalar( 110, 220, 0 ), 2);
}
// Draw boundary on image
if (resultPointCount > 1) {
for (int j = 0; j < resultPointCount; j++) {
// Get start result point
Ref<ResultPoint> previousResultPoint = (j > 0) ? result->getResultPoints()[j - 1] : result->getResultPoints()[resultPointCount - 1];
// Draw line
line(image, toCvPoint(previousResultPoint), toCvPoint(result->getResultPoints()[j]), Scalar( 110, 220, 0 ), 2, 8 );
// Update previous point
previousResultPoint = result->getResultPoints()[j];
}
}
if (resultPointCount > 0) {
// Draw text
putText(image, result->getText()->getText(), toCvPoint(result->getResultPoints()[0]), FONT_HERSHEY_PLAIN, 1, Scalar( 110, 220, 0 ));
}
} catch (const ReaderException& e) {
cerr << e.what() << " (ignoring)" << endl;
} catch (const zxing::IllegalArgumentException& e) {
cerr << e.what() << " (ignoring)" << endl;
} catch (const zxing::Exception& e) {
cerr << e.what() << " (ignoring)" << endl;
} catch (const std::exception& e) {
cerr << e.what() << " (ignoring)" << endl;
}
// Show captured image
imshow("ZXing", image);
// Wait a key for 1 millis
stopped = waitKey(1);
} else {
// Log
cerr << "video capture failed" << endl;
}
}
// Release video capture
videoCapture.release();
return 0;
}