Ask Your Question

How to publish an image message with ROSJava?n

asked 2012-02-05 09:12:25 -0500

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

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted

answered 2012-02-07 10:52:13 -0500

updated 2012-02-07 18:24:46 -0500

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

public class Talker implements NodeMain {

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

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

      protected void setup() {


      protected void loop() throws InterruptedException {
        org.ros.message.sensor_msgs.Image str = new org.ros.message.sensor_msgs.Image();

  public void onShutdown(Node node) {

  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; i<dwa_data->config.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;
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools


Asked: 2012-02-05 09:12:25 -0500

Seen: 1,218 times

Last updated: Feb 07 '12