RAPP Platform API
 All Classes Namespaces Files Functions Variables Typedefs
qr_detection.hpp
Go to the documentation of this file.
1 #ifndef RAPP_SERVICE_CLOUD_QRDETECTOR
2 #define RAPP_SERVICE_CLOUD_QRDETECTOR
3 #include "includes.ihh"
4 namespace rapp {
5 namespace cloud {
14 {
15 public:
23  const std::shared_ptr<rapp::object::picture> image,
24  const std::string token,
25  std::function<void(std::vector<rapp::object::qr_code>)> callback
26  )
27  : asio_service_http(token), delegate__(callback)
28  {
29  assert(image);
30  std::string boundary = random_boundary();
31  std::string fname = random_boundary()+"."+image->type();
32  boost::property_tree::ptree tree;
33  tree.put("file", fname);
34  std::stringstream ss;
35  boost::property_tree::write_json(ss, tree, false);
36  post_ = "--" + boundary + "\r\n"
37  + "Content-Disposition: form-data; name=\"json\"\r\n\r\n"
38  + ss.str() + "\r\n";
39  post_ += "--"+boundary+"\r\n"
40  + "Content-Disposition: form-data; name=\"file_uri\"; filename=\""+fname+"\"\r\n"
41  + "Content-Type: image/"+image->type()+"\r\n"
42  + "Content-Transfer-Encoding: binary\r\n\r\n";
43  // Append binary data
44  auto imagebytes = image->bytearray();
45  post_.insert(post_.end(), imagebytes.begin(), imagebytes.end());
46  post_ += "\r\n";
47  post_ += "--"+boundary+"--";
48  header_ = "POST /hop/qr_detection HTTP/1.1\r\n";
49  header_ += "Content-Type: multipart/form-data; boundary="+boundary+"\r\n\r\n";
50  callback_ = std::bind(&qr_detection::handle_reply, this, std::placeholders::_1);
51  }
52 
53 private:
57  void handle_reply(std::string json)
58  {
59  std::stringstream ss(json);
60  std::vector<rapp::object::qr_code> qrCodes;
61  try {
62  boost::property_tree::ptree tree;
63  boost::property_tree::read_json(ss, tree);
64  for (auto child : tree.get_child("qr_centers")) {
65  std::tuple<float,float,std::string> qrcode;
66  for (auto iter = child.second.begin(); iter != child.second.end(); ++iter) {
67  if (iter->first == "x") {
68  std::get<0>(qrcode) = iter->second.get_value<float>();
69  }
70  else if (iter->first == "y") {
71  std::get<1>(qrcode) = iter->second.get_value<float>();
72  }
73  else if (iter->first == "message") {
74  std::get<2>(qrcode) = iter->second.get_value<std::string>();
75  }
76  }
77  qrCodes.push_back(rapp::object::qr_code(std::get<0>(qrcode),
78  std::get<1>(qrcode),
79  std::get<2>(qrcode)));
80  }
81  // Check for Errors returned by the platform
82  for (auto child : tree.get_child("error")) {
83  const std::string value = child.second.get_value<std::string>();
84  if (!value.empty()) {
85  std::cerr << "qr_detection JSON error: " << value << std::endl;
86  }
87  }
88  }
89  catch(boost::property_tree::json_parser::json_parser_error & je) {
90  std::cerr << "qr_detection::handle_reply Error parsing: "
91  << je.filename() << " on line: " << je.line() << std::endl;
92  std::cerr << je.message() << std::endl;
93  }
94  delegate__(qrCodes);
95  }
96 
98  std::function<void(std::vector<rapp::object::qr_code>)> delegate__;
99 };
100 }
101 }
102 #endif
std::function< void(std::vector< rapp::object::qr_code >)> delegate__
The callback called upon completion of receiving the detected faces.
std::string header_
Header that will be used.
std::string random_boundary() const
Create a random boundary for the multipart/form in HTTP.
qr_detection(const std::shared_ptr< rapp::object::picture > image, const std::string token, std::function< void(std::vector< rapp::object::qr_code >)> callback)
Constructor.
std::string post_
Actual post Data.
Asynchronous Service which will request the cloud to detect QR codes.
std::function< void(std::string)> callback_
Callback Handler - use with std::bind or boost variant.
void handle_reply(std::string json)
handle the rapp-platform JSON reply
base class for asynchronous http websockets used for connecting to cloud services ...