adding c++ interface to the datamtrix codes of j.
[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  * easy as CV_PI right?
9  */
10 #include "opencv2/highgui/highgui.hpp"
11 #include <opencv2/objdetect/objdetect.hpp>
12 #include <opencv2/imgproc/imgproc.hpp>
13 #include <iostream>
14 #include <vector>
15 #include <stdio.h>
16
17 using namespace cv;
18 using namespace std;
19
20 //hide the local functions in an anon namespace
21 namespace
22 {
23 void help(char** av)
24 {
25   cout << "\nThis program justs gets you started reading images from video\n"
26     "Usage:\n./" << av[0] << " <video device number>\n" << "q,Q,esc -- quit\n"
27       << "space   -- save frame\n\n"
28       << "\tThis is a starter sample, to get you up and going in a copy pasta fashion\n"
29       << "\tThe program captures frames from a camera connected to your computer.\n"
30       << "\tTo find the video device number, try ls /dev/video* \n"
31       << "\tYou may also pass a video file, like my_vide.avi instead of a device number"
32       << endl;
33 }
34
35 int process(VideoCapture& capture)
36 {
37   std::vector<DataMatrixCode> codes;
38   int n = 0;
39   char filename[200];
40   string window_name = "video | q or esc to quit";
41   cout << "press space to save a picture. q or esc to quit" << endl;
42   namedWindow(window_name, CV_WINDOW_KEEPRATIO); //resizable window;
43   Mat frame;
44   for (;;)
45   {
46     capture >> frame;
47     if (frame.empty())
48       continue;
49     cv::Mat gray;
50     cv::cvtColor(frame,gray,CV_RGB2GRAY);
51     findDataMatrix(gray, codes);
52     drawDataMatrixCodes(codes, frame);
53     imshow(window_name, frame);
54     char key = (char) waitKey(5); //delay N millis, usually long enough to display and capture input
55     switch (key)
56     {
57     case 'q':
58     case 'Q':
59     case 27: //escape key
60       return 0;
61     case ' ': //Save an image
62       sprintf(filename, "filename%.3d.jpg", n++);
63       imwrite(filename, frame);
64       cout << "Saved " << filename << endl;
65       break;
66     default:
67       break;
68     }
69   }
70   return 0;
71 }
72
73 }
74
75 int main(int ac, char** av)
76 {
77
78   if (ac != 2)
79   {
80     help(av);
81     return 1;
82   }
83   std::string arg = av[1];
84   VideoCapture capture(arg); //try to open string, this will attempt to open it as a video file
85   if (!capture.isOpened()) //if this fails, try to open as a video camera, through the use of an integer param
86     capture.open(atoi(arg.c_str()));
87   if (!capture.isOpened())
88   {
89     cerr << "Failed to open a video device or video file!\n" << endl;
90     help(av);
91     return 1;
92   }
93   return process(capture);
94 }