0
$\begingroup$

Rosanswers logo

I would like to learn how to publish an image message with ROSJava.


Originally posted by Juan Antonio Breña Moral on ROS Answers with karma: 274 on 2012-02-05

Post score: 1

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

Here you are an example. I've tested it and it works.

public class Talker implements NodeMain {

  @Override
  public GraphName getDefaultNodeName() {
    return new GraphName("rosjava_tutorial_pubsub/talker");
  }

  @Override
  public void onStart(final Node node) {
    final Publisher publisher =
        node.newPublisher("chatter", "sensor_msgs/Image");
    // This CancellableLoop will be canceled automatically when the Node shuts
    // down.
    node.executeCancellableLoop(new CancellableLoop() {
     

      @Override
      protected void setup() {
        
      }

      @Override
      protected void loop() throws InterruptedException {
        org.ros.message.sensor_msgs.Image str = new org.ros.message.sensor_msgs.Image();
        publisher.publish(str);
        Thread.sleep(1000);
      }
    });
  }

  @Override
  public void onShutdown(Node node) {
  }

  @Override
  public void onShutdownComplete(Node node) {
  }
}

Of course you have to make sure that your javabootstrap.jar contains the sensor_msg/Image class. If not, here you can find a tutorial about how you can add it. You also should understand how to fill all the fields of the sensor_msgs/Image message. There are many documentation about this. You can start from here:

$ rosmsg show sensor_msgs/Image 
Header header
  uint32 seq
  time stamp
  string frame_id
uint32 height
uint32 width
string encoding
uint8 is_bigendian
uint32 step
uint8[] data

Some time ago I did a code in c++ that may be useful for you (I may test it tomorrow on rosjava):

    image_msg->header.stamp = ros::Time::now();
    image_msg->height = dwa_data->config.resolution_height;
    image_msg->width = dwa_data->config.resolution_width;
    image_msg->encoding = "rgb8"; //sensor_msgs::image_encodings::MONO16;
    image_msg->step = image_msg->width * 3;
    image_msg->data.resize(image_msg->height * image_msg->step);

    for (int i = 0; iconfig.resolution_width*dwa_data->config.resolution_height ; i++)
    {
      //ROS_INFO("index %d",i);
      int offset = 3 * i;
      ...
        image_msg->data[offset] = 255;
        image_msg->data[offset + 1] = 0;
        image_msg->data[offset + 2] = i%255;
      ...
    }

Originally posted by Pablo Iñigo Blasco with karma: 2982 on 2012-02-07

This answer was ACCEPTED on the original site

Post score: 3

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.