added usage on how to actually run video_dmtx.cpp
[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   std::vector<DataMatrixCode> codes;
44   int n = 0;
45   char filename[200];
46   string window_name = "video | q or esc to quit";
47   cout << "press space to save a picture. q or esc to quit" << endl;
48   namedWindow(window_name, CV_WINDOW_KEEPRATIO); //resizable window;
49   Mat frame;
50   for (;;)
51   {
52     capture >> frame;
53     if (frame.empty())
54       continue;
55     cv::Mat gray;
56     cv::cvtColor(frame,gray,CV_RGB2GRAY);
57     findDataMatrix(gray, codes);
58     drawDataMatrixCodes(codes, frame);
59     imshow(window_name, frame);
60     char key = (char) waitKey(5); //delay N millis, usually long enough to display and capture input
61     switch (key)
62     {
63     case 'q':
64     case 'Q':
65     case 27: //escape key
66       return 0;
67     case ' ': //Save an image
68       sprintf(filename, "filename%.3d.jpg", n++);
69       imwrite(filename, frame);
70       cout << "Saved " << filename << endl;
71       break;
72     default:
73       break;
74     }
75   }
76   return 0;
77 }
78
79 }
80
81 int main(int ac, char** av)
82 {
83
84   if (ac != 2)
85   {
86     help(av);
87     return 1;
88   }
89   std::string arg = av[1];
90   VideoCapture capture(arg); //try to open string, this will attempt to open it as a video file
91   if (!capture.isOpened()) //if this fails, try to open as a video camera, through the use of an integer param
92     capture.open(atoi(arg.c_str()));
93   if (!capture.isOpened())
94   {
95     cerr << "Failed to open a video device or video file!\n" << endl;
96     help(av);
97     return 1;
98   }
99   return process(capture);
100 }