capture.cpp
Go to the documentation of this file.
00001 // Copyright [2015] Takashi Ogura<t.ogura@gmail.com>
00002 
00003 #include "cv_camera/capture.h"
00004 #include <sstream>
00005 #include <string>
00006 
00007 namespace cv_camera
00008 {
00009 
00010 namespace enc = sensor_msgs::image_encodings;
00011 
00012 Capture::Capture(ros::NodeHandle& node,
00013                  const std::string& topic_name,
00014                  int32_t buffer_size,
00015                  const std::string& frame_id) :
00016     node_(node),
00017     it_(node_),
00018     topic_name_(topic_name),
00019     buffer_size_(buffer_size),
00020     frame_id_(frame_id),
00021     info_manager_(node_, frame_id)
00022 {
00023 }
00024 
00025 void Capture::loadCameraInfo()
00026 {
00027   std::string url;
00028   if (node_.getParam("camera_info_url", url))
00029   {
00030     if (info_manager_.validateURL(url))
00031     {
00032       info_manager_.loadCameraInfo(url);
00033     }
00034   }
00035 
00036   for (int i = 0; ; ++i)
00037   {
00038     int code = 0;
00039     double value = 0.0;
00040     std::stringstream stream;
00041     stream << "property_" << i << "_code";
00042     const std::string param_for_code = stream.str();
00043     stream.str("");
00044     stream << "property_" << i << "_value";
00045     const std::string param_for_value = stream.str();
00046     if (!node_.getParam(param_for_code, code) || !node_.getParam(param_for_value, value))
00047     {
00048       break;
00049     }
00050     if (!cap_.set(code, value))
00051     {
00052       ROS_ERROR_STREAM("Setting with code " << code << " and value " << value << " failed"
00053                        << std::endl);
00054     }
00055   }
00056 }
00057 
00058 void Capture::open(int32_t device_id)
00059 {
00060   cap_.open(device_id);
00061   if(!cap_.isOpened())
00062   {
00063     std::stringstream stream;
00064     stream << "device_id" << device_id << " cannot be opened";
00065     throw DeviceError(stream.str());
00066   }
00067   pub_ = it_.advertiseCamera(topic_name_, buffer_size_);
00068 
00069   loadCameraInfo();
00070 }
00071 
00072 void Capture::open(const std::string& device_path)
00073 {
00074   cap_.open(device_path, cv::CAP_V4L);
00075   if (!cap_.isOpened())
00076   {
00077     throw DeviceError("device_path " + device_path + " cannot be opened");
00078   }
00079   pub_ = it_.advertiseCamera(topic_name_, buffer_size_);
00080 
00081   loadCameraInfo();
00082 }
00083 
00084 void Capture::open()
00085 {
00086   open(0);
00087 }
00088 
00089 void Capture::openFile(const std::string& file_path)
00090 {
00091   cap_.open(file_path);
00092   if (!cap_.isOpened())
00093   {
00094     std::stringstream stream;
00095     stream << "file " << file_path << " cannot be opened";
00096     throw DeviceError(stream.str());
00097   }
00098   pub_ = it_.advertiseCamera(topic_name_, buffer_size_);
00099 
00100   std::string url;
00101   if (node_.getParam("camera_info_url", url))
00102   {
00103     if (info_manager_.validateURL(url))
00104     {
00105       info_manager_.loadCameraInfo(url);
00106     }
00107   }
00108 }
00109 
00110 bool Capture::capture()
00111 {
00112   if (cap_.read(bridge_.image))
00113   {
00114     ros::Time now = ros::Time::now();
00115     bridge_.encoding = enc::BGR8;
00116     bridge_.header.stamp = now;
00117     bridge_.header.frame_id = frame_id_;
00118 
00119     info_ = info_manager_.getCameraInfo();
00120     if (info_.height == 0)
00121     {
00122       info_.height = bridge_.image.rows;
00123     }
00124     if (info_.width == 0)
00125     {
00126       info_.width = bridge_.image.cols;
00127     }
00128     info_.header.stamp = now;
00129     info_.header.frame_id = frame_id_;
00130 
00131     return true;
00132   }
00133   return false;
00134 }
00135 
00136 void Capture::publish()
00137 {
00138   pub_.publish(*getImageMsgPtr(), info_);
00139 }
00140 
00141 bool Capture::setPropertyFromParam(int property_id, const std::string &param_name)
00142 {
00143   if (cap_.isOpened())
00144   {
00145     double value = 0.0;
00146     if (node_.getParam(param_name, value))
00147     {
00148       ROS_INFO("setting property %s = %lf", param_name.c_str(), value);
00149       return cap_.set(property_id, value);
00150     }
00151   }
00152   return true;
00153 }
00154 
00155 }  // namespace cv_camera


cv_camera
Author(s): Takashi Ogura
autogenerated on Thu Oct 5 2017 02:35:21