Visual Servoing Platform  version 3.3.0 under development (2020-02-17)
record_helper.cpp
1 #include <visp3/core/vpIoTools.h>
2 #include <visp3/core/vpDisplay.h>
3 #include <visp3/io/vpImageIo.h>
4 
5 #include "record_helper.h"
6 
15 bool record_helper(const std::string &opt_seqname, int opt_record_mode,
16  const vpImage<unsigned char> &I)
17 {
18  static bool start_record = false;
19  static std::string text_record_mode = std::string("Record mode: ") + (opt_record_mode ? std::string("single") : std::string("continuous"));
20 
21  if (! opt_seqname.empty()) {
22  if (! opt_record_mode) { // continuous
23  if (start_record) {
24  vpDisplay::displayText(I, 20, 10, "Left click: stop recording", vpColor::red);
25  }
26  else {
27  vpDisplay::displayText(I, 20, 10, "Left click: start recording", vpColor::red);
28  }
29  }
30  else {
31  vpDisplay::displayText(I, 20, 10, "Left click: record image", vpColor::red);
32  }
33  vpDisplay::displayText(I, 40, 10, "Right click: quit", vpColor::red);
34  }
35  else {
36  vpDisplay::displayText(I, 20, 10, "Click to quit", vpColor::red);
37  }
38 
39  if (! opt_seqname.empty()) {
40  vpDisplay::displayText(I, 60, 10, text_record_mode, vpColor::red);
41  }
43  if (vpDisplay::getClick(I, button, false)) {
44  if (! opt_seqname.empty()) { // Recording requested
45  if (button == vpMouseButton::button1) { // enable/disable recording
46  start_record = !start_record;
47  }
48  else if (button == vpMouseButton::button3) { // quit
49  return true;
50  }
51  }
52  else { // any button to quit
53  return true;
54  }
55  }
56  if (start_record) {
57  static unsigned int counter = 1;
58  char filename[FILENAME_MAX];
59  sprintf(filename, opt_seqname.c_str(), counter);
60  {
61  // check if parent folder exists. Create otherwise
62  static bool parent_exists = false;
63  if (! parent_exists) {
64  std::string parent = vpIoTools::getParent(filename);
65  if (! parent.empty()) {
66  if (! vpIoTools::checkDirectory(parent)) {
68  }
69  }
70  parent_exists = true;
71  }
72  }
73 
74  counter ++;
75  std::string text = std::string("Save: ") + std::string(filename);
76  vpDisplay::displayText(I, 80, 10, text, vpColor::red);
77  std::cout << text << std::endl;
78  vpImageIo::write(I, filename);
79  if (opt_record_mode == 1) { // single shot mode
80  start_record = false;
81  }
82  }
83 
84  return false;
85 }
static void makeDirectory(const std::string &dirname)
Definition: vpIoTools.cpp:572
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
static void displayText(const vpImage< unsigned char > &I, const vpImagePoint &ip, const std::string &s, const vpColor &color)
static std::string getParent(const std::string &pathname)
Definition: vpIoTools.cpp:1473
static const vpColor red
Definition: vpColor.h:179
static bool checkDirectory(const std::string &dirname)
Definition: vpIoTools.cpp:422
static void write(const vpImage< unsigned char > &I, const std::string &filename)
Definition: vpImageIo.cpp:445