-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathqrcam.cpp
More file actions
72 lines (60 loc) · 1.45 KB
/
qrcam.cpp
File metadata and controls
72 lines (60 loc) · 1.45 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
/** @file qrcam.cpp
* Reading QR code using webcam
* and printing at terminal
* only once.
*/
#include <iostream>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <unistd.h>
#include <zbar.h>
#include "qrmosq.h"
using namespace cv;
using namespace std;
using namespace zbar;
//int main(void)
string qrMqtt::qrcam()
{
string qrdata;
VideoCapture cap(0);
if (!cap.isOpened())
{
cerr << "Cannot open Webcam\n";
//return -1;
throw "#FAILED";
}
ImageScanner scanner; // zbar::ImageScanner
scanner.set_config(ZBAR_NONE, ZBAR_CFG_ENABLE, 1);
while (1)
{
Mat frame; // Mat - opencv n-dimension array class.
bool bSuccess = cap.read(frame);
if (!bSuccess)
{
cout << "cannot read a frame video\n";
break;
}
Mat gray;
cvtColor(frame, gray, CV_BGR2GRAY); // convert frame array to grey
// as transform RGB space to GRAY.
int width = frame.cols;
int height = frame.rows;
uchar *raw = (uchar *)gray.data;
Image image(width, height, "Y800", raw, width * height); // zbar::Image::Image()
// Y800 = 8 bit monochrome format.
// GRAY also accepted.
int n = scanner.scan(image);
for (Image::SymbolIterator symbol = image.symbol_begin();
symbol != image.symbol_end(); ++symbol)
{
qrdata = symbol->get_data();
cout << "Main Data ->" << qrdata << endl;
}
if (!qrdata.empty())
{
break;
}
usleep(100000);
}
return qrdata;
}