1
+ #include < opencv2/highgui/highgui.hpp>
2
+ #include < opencv2/imgproc/imgproc.hpp>
3
+ #include < zbar.h>
4
+ #include < iostream>
5
+
6
+ using namespace cv ;
7
+ using namespace std ;
8
+ using namespace zbar ;
9
+
10
+ // g++ main.cpp /usr/local/include/ /usr/local/lib/ -lopencv_highgui.2.4.8 -lopencv_core.2.4.8
11
+
12
+ int main (int argc, char * argv[])
13
+ {
14
+ VideoCapture cap (0 ); // open the video camera no. 0
15
+
16
+ // cap.set(CV_CAP_PROP_FRAME_WIDTH,800);
17
+ // cap.set(CV_CAP_PROP_FRAME_HEIGHT,640);
18
+
19
+ if (!cap.isOpened ()) // if not success, exit program
20
+ {
21
+ cout << " Cannot open the video cam" << endl;
22
+ return -1 ;
23
+ }
24
+
25
+
26
+ ImageScanner scanner;
27
+ scanner.set_config (ZBAR_NONE, ZBAR_CFG_ENABLE, 1 );
28
+
29
+ double dWidth = cap.get (CV_CAP_PROP_FRAME_WIDTH); // get the width of frames of the video
30
+ double dHeight = cap.get (CV_CAP_PROP_FRAME_HEIGHT); // get the height of frames of the video
31
+
32
+ cout << " Frame size : " << dWidth << " x " << dHeight << endl;
33
+
34
+ namedWindow (" MyVideo" ,CV_WINDOW_AUTOSIZE); // create a window called "MyVideo"
35
+
36
+ while (1 )
37
+ {
38
+ Mat frame;
39
+
40
+ bool bSuccess = cap.read (frame); // read a new frame from video
41
+
42
+ if (!bSuccess) // if not success, break loop
43
+ {
44
+ cout << " Cannot read a frame from video stream" << endl;
45
+ break ;
46
+ }
47
+
48
+ Mat grey;
49
+ cvtColor (frame,grey,CV_BGR2GRAY);
50
+
51
+ int width = frame.cols ;
52
+ int height = frame.rows ;
53
+ uchar *raw = (uchar *)grey.data ;
54
+ // wrap image data
55
+ Image image (width, height, " Y800" , raw, width * height);
56
+ // scan the image for barcodes
57
+ int n = scanner.scan (image);
58
+ // extract results
59
+ for (Image::SymbolIterator symbol = image.symbol_begin ();
60
+ symbol != image.symbol_end ();
61
+ ++symbol) {
62
+ vector<Point> vp;
63
+ // do something useful with results
64
+ cout << " decoded " << symbol->get_type_name () << " symbol \" " << symbol->get_data () << ' "' <<" " << endl;
65
+ int n = symbol->get_location_size ();
66
+ for (int i=0 ;i<n;i++){
67
+ vp.push_back (Point (symbol->get_location_x (i),symbol->get_location_y (i)));
68
+ }
69
+ RotatedRect r = minAreaRect (vp);
70
+ Point2f pts[4 ];
71
+ r.points (pts);
72
+ for (int i=0 ;i<4 ;i++){
73
+ line (frame,pts[i],pts[(i+1 )%4 ],Scalar (255 ,0 ,0 ),3 );
74
+ }
75
+ // cout<<"Angle: "<<r.angle<<endl;
76
+ }
77
+
78
+ imshow (" MyVideo" , frame); // show the frame in "MyVideo" window
79
+
80
+ if (waitKey (30 ) == 27 ) // wait for 'esc' key press for 30ms. If 'esc' key is pressed, break loop
81
+ {
82
+ cout << " esc key is pressed by user" << endl;
83
+ break ;
84
+ }
85
+ }
86
+ return 0 ;
87
+
88
+ }
0 commit comments