Go to the documentation of this file.00001 #include "cv_camera/capture.h"
00002 #include <sstream>
00003
00004 namespace cv_camera
00005 {
00006
00007 namespace enc = sensor_msgs::image_encodings;
00008
00009 Capture::Capture(ros::NodeHandle& node,
00010 const std::string& topic_name,
00011 int32_t buffer_size,
00012 const std::string& frame_id) :
00013 node_(node),
00014 it_(node_),
00015 topic_name_(topic_name),
00016 buffer_size_(buffer_size),
00017 frame_id_(frame_id),
00018 info_manager_(node_, frame_id)
00019 {
00020 }
00021
00022 void Capture::open(int32_t device_id)
00023 {
00024 cap_.open(device_id);
00025 if (!cap_.isOpened()) {
00026 std::stringstream stream;
00027 stream << "device_id " << device_id << " cannot be opened";
00028 throw DeviceError(stream.str());
00029 }
00030 pub_ = it_.advertiseCamera(topic_name_, buffer_size_);
00031
00032 std::string url;
00033 if (node_.getParam("camera_info_url", url))
00034 {
00035 if (info_manager_.validateURL(url))
00036 {
00037 info_manager_.loadCameraInfo(url);
00038 }
00039 }
00040 }
00041
00042 void Capture::open()
00043 {
00044 open(0);
00045 }
00046
00047 void Capture::openFile(const std::string& file_path)
00048 {
00049 cap_.open(file_path);
00050 if (!cap_.isOpened()) {
00051 std::stringstream stream;
00052 stream << "file " << file_path << " cannot be opened";
00053 throw DeviceError(stream.str());
00054 }
00055 pub_ = it_.advertiseCamera(topic_name_, buffer_size_);
00056
00057 std::string url;
00058 if (node_.getParam("camera_info_url", url))
00059 {
00060 if (info_manager_.validateURL(url))
00061 {
00062 info_manager_.loadCameraInfo(url);
00063 }
00064 }
00065 }
00066
00067 bool Capture::capture()
00068 {
00069 if(cap_.read(bridge_.image)) {
00070 ros::Time now = ros::Time::now();
00071 bridge_.encoding = enc::BGR8;
00072 bridge_.header.stamp = now;
00073 bridge_.header.frame_id = frame_id_;
00074
00075 info_ = info_manager_.getCameraInfo();
00076 if (info_.height == 0) {
00077 info_.height = bridge_.image.rows;
00078 }
00079 if (info_.width == 0) {
00080 info_.width = bridge_.image.cols;
00081 }
00082 info_.header.stamp = now;
00083 info_.header.frame_id = frame_id_;
00084
00085 return true;
00086 }
00087 return false;
00088 }
00089
00090 void Capture::publish()
00091 {
00092 pub_.publish(*getImageMsgPtr(), info_);
00093 }
00094
00095 }