#include #include #include #include #include #include #include #include using namespace std::chrono_literals; namespace j7s { class ImagePublisher : public rclcpp::Node { public: ImagePublisher(const rclcpp::NodeOptions& options); private: std::shared_ptr> m_publisher; std::shared_ptr> m_compressedPublisher; rclcpp::TimerBase::SharedPtr m_timer; std::tuple draw_image(); }; }; namespace j7s { ImagePublisher::ImagePublisher(const rclcpp::NodeOptions& options): Node("image_publisher", options), m_publisher(this->create_publisher("image", 1)), m_compressedPublisher(this->create_publisher("image/compressed", 1)), m_timer{} { m_timer = this->create_timer(10ms, [this]() -> void { const auto [image, compressedImage] = draw_image(); m_publisher->publish(*image); m_compressedPublisher->publish(*compressedImage); }); } std::tuple ImagePublisher::draw_image() { cv::Mat mat = cv::Mat::zeros(200, 200, CV_8UC3); std_msgs::msg::Header header; header.stamp = this->now(); cv_bridge::CvImage image(header, "mono8", mat); return std::tuple(image.toImageMsg(), image.toCompressedImageMsg()); } }; #include RCLCPP_COMPONENTS_REGISTER_NODE(j7s::ImagePublisher)