Merge pull request #1722 from StevenPuttemans:feature_1631_second
[profile/ivi/opencv.git] / samples / cpp / video_dmtx.cpp
1 /*
2  * starter_video.cpp
3  *
4  *  Created on: Nov 23, 2010
5  *      Author: Ethan Rublee
6  *
7  * A starter sample for using opencv, get a video stream and display the images
8  * Use http://datamatrix.kaywa.com/  to generate datamatrix images using strings of length 3 or less.
9  * easy as CV_PI right?
10  */
11 #include "opencv2/highgui/highgui.hpp"
12 #include <opencv2/objdetect/objdetect.hpp>
13 #include <opencv2/imgproc/imgproc.hpp>
14 #include <iostream>
15 #include <vector>
16 #include <stdio.h>
17
18 using namespace cv;
19 using namespace std;
20
21 //hide the local functions in an anon namespace
22 namespace
23 {
24     void help(char** av)
25     {
26         cout << "\nThis program justs gets you started reading images from video\n"
27         "Usage:\n./" << av[0] << " <video device number>\n" << "q,Q,esc -- quit\n"
28         << "space   -- save frame\n\n"
29         << "\tThis is a starter sample, to get you up and going in a copy pasta fashion\n"
30         << "\tThe program captures frames from a camera connected to your computer.\n"
31         << "\tTo find the video device number, try ls /dev/video* \n"
32         << "\tYou may also pass a video file, like my_vide.avi instead of a device number"
33         << "\n"
34         << "DATA:\n"
35         << "Generate a datamatrix from  from http://datamatrix.kaywa.com/  \n"
36         << "  NOTE: This only handles strings of len 3 or less\n"
37         << "  Resize the screen to be large enough for your camera to see, and it should find an read it.\n\n"
38         << endl;
39     }
40
41     int process(VideoCapture& capture)
42     {
43         int n = 0;
44         char filename[200];
45         string window_name = "video | q or esc to quit";
46         cout << "press space to save a picture. q or esc to quit" << endl;
47         namedWindow(window_name, CV_WINDOW_KEEPRATIO); //resizable window;
48         Mat frame;
49         for (;;)
50         {
51             capture >> frame;
52             if (frame.empty())
53                 break;
54             cv::Mat gray;
55             cv::cvtColor(frame,gray,COLOR_RGB2GRAY);
56             vector<string> codes;
57             Mat corners;
58             findDataMatrix(gray, codes, corners);
59             drawDataMatrixCodes(frame, codes, corners);
60             imshow(window_name, frame);
61             char key = (char) waitKey(5); //delay N millis, usually long enough to display and capture input
62             switch (key)
63             {
64                 case 'q':
65                 case 'Q':
66                 case 27: //escape key
67                     return 0;
68                 case ' ': //Save an image
69                     sprintf(filename, "filename%.3d.jpg", n++);
70                     imwrite(filename, frame);
71                     cout << "Saved " << filename << endl;
72                     break;
73                 default:
74                     break;
75             }
76         }
77         return 0;
78     }
79
80 }
81
82 int main(int ac, char** av)
83 {
84
85     if (ac != 2)
86     {
87         help(av);
88         return 1;
89     }
90     std::string arg = av[1];
91     VideoCapture capture(arg); //try to open string, this will attempt to open it as a video file
92     if (!capture.isOpened()) //if this fails, try to open as a video camera, through the use of an integer param
93         capture.open(atoi(arg.c_str()));
94     if (!capture.isOpened())
95     {
96         cerr << "Failed to open a video device or video file!\n" << endl;
97         help(av);
98         return 1;
99     }
100     return process(capture);
101 }