-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathmain.cpp
More file actions
88 lines (72 loc) · 2.37 KB
/
Copy pathmain.cpp
File metadata and controls
88 lines (72 loc) · 2.37 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
#include <iostream>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <zbar.h>
using namespace cv;
using namespace std;
using namespace zbar;
int main (int argc, char *argv[])
{
VideoCapture cap(0); // open video capture device #0
// cap.set(CV_CAP_PROP_FRAME_WIDTH,800);
// cap.set(CV_CAP_PROP_FRAME_HEIGHT,640);
if (!cap.isOpened()) // if not success, exit program
{
cout << "Unable to open video capture device." << endl;
return EXIT_FAILURE;
}
ImageScanner scanner;
scanner.set_config(ZBAR_NONE, ZBAR_CFG_ENABLE, 1);
double dWidth = cap.get(CV_CAP_PROP_FRAME_WIDTH); // get video frame width
double dHeight = cap.get(CV_CAP_PROP_FRAME_HEIGHT); // get video frame height
cout << "Frame size: " << dWidth << " x " << dHeight << endl;
namedWindow("Video", CV_WINDOW_AUTOSIZE); // create window
while (1)
{
Mat frame;
if (!cap.read(frame)) // read frame from video or break loop
{
cout << "Unable to read frame from video stream." << endl;
break;
}
Mat grey;
cvtColor(frame, grey, CV_BGR2GRAY);
int width = frame.cols;
int height = frame.rows;
uchar *raw = (uchar *) grey.data;
// wrap image data
Image image(width, height, "Y800", raw, width * height);
// scan the image for barcodes
int n = scanner.scan(image);
// extract results
for (Image::SymbolIterator symbol = image.symbol_begin();
symbol != image.symbol_end(); ++symbol)
{
vector<Point> vp;
// do something useful with results
cout << "decoded " << symbol->get_type_name() << " symbol \""
<< symbol->get_data() << '"' << " " << endl;
int n = symbol->get_location_size();
for (int i = 0; i < n; i++) {
vp.push_back(
Point(symbol->get_location_x(i), symbol->get_location_y(i)));
}
RotatedRect r = minAreaRect(vp);
Point2f pts[4];
r.points(pts);
for (int i = 0; i < 4; i++)
{
line(frame, pts[i], pts[(i + 1) % 4], Scalar(255, 0, 0), 3);
}
// cout<<"Angle: "<<r.angle<<endl;
}
imshow("Video", frame); // show the frame in "Video" window
if (waitKey(30) == 27) // wait for 'esc' key press for 30ms. If 'esc' key is
// pressed, break loop
{
cout << "esc key is pressed by user" << endl;
break;
}
}
return EXIT_SUCCESS;
}