Starting with rosjava and image messages

This post is a mini-tutorial based on the basic rosjava tutorials whose code can be found in [ Revision c53f30aa6fb6: /]

Firstly you must known the main concepts of ROS: nodes, topics, messages, etc. After this I recommend you to install the latest rosjava_core stack: Then, to begin with rosjava is also recommendable to have a look to

After this you should make sure that you have compiled the java source code for the sensor_msgs/Image. To do this, you should understand before how the rosjava_bootstrap works. This package makes possible the java source code generation for any kind of messages. You can realize if you have the sensor message compiled checking if you have the folder for sensor_msgs:

cd  rospack find rosjava_bootstrap/target/classes/org/ros/message/sensor_msgs.

If not, as it was my case then you should generate the source code. We must compile the most essential message types for the correct building of rosjava: std_msgs, sensor_msgs and rosgraph_msgs

geus@geus-vaio:/opt/ros/external/packages/rosjava_core/rosjava_bootstrap$ rosrun rosjava_bootstrap std_msgs sensor_msgs rosgraph_msgs geometry_msgs -o src/main/java/
generating messages for package [std_msgs]
generated messages for package [std_msgs] to [src/main/java/org/ros/message/std_msgs]
generating messages for package [sensor_msgs]
generated messages for package [sensor_msgs] to [src/main/java/org/ros/message/sensor_msgs]
generating messages for package [rosgraph_msgs]
generated messages for package [rosgraph_msgs] to [src/main/java/org/ros/message/rosgraph_msgs]
generated messages for package [geometry_msgs] to [src/main/java/org/ros/message/geometry_msgs]

Check if the java sourcecode is compiled:

geus@geus-vaio:/opt/ros/external/packages/rosjava_core/rosjava_bootstrap$ ls src/main/java/org/ros/message/   rosgraph_msgs/ sensor_msgs/   std_msgs/

As we can see the sensor_msgs is available:

geus@geus-vaio:/opt/ros/external/packages/rosjava_core/rosjava_bootstrap$ ls src/main/java/org/ros/message/sensor_msgs/  

After this we can regenerate rosjava_bootstrap with:

rosmake rosjava_bootstrap rosjava

Now we can use the sensor_msg/Image type. We can use as a template of the [rosjava_tutorial_pubsub package] in the same rosjava_core stack. Here you are some example code for the subscriber:

public class Listener implements NodeMain {

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

  public void onStart(Node node) {
    final Log log = node.getLog();
    Subscriber<org.ros.message.sensor_msgs.image> subscriber =
        node.newSubscriber("chatter", "sensor_msgs/Image");
    subscriber.addMessageListener(new MessageListener<org.ros.message.sensor_msgs.image>() {
      public void onNewMessage(org.ros.message.sensor_msgs.Image message) {"I heard: \"" + + "\"");

  public void onShutdown(Node node) {

  public void onShutdownComplete(Node node) {

Then to launch it, we can follow the instruction explained on ROS wiki. Working over this tutorial code the only we have to do is recompiling the project and launch it with rosjava_bootstrap:

$ rosmake rosjava_tutorial_pubsub
$ rosrun rosjava_bootstrap rosjava_tutorial_pubsub org.ros.tutorials.pubsub.Listener

Enjoy! I also did it as my first rosjava test 🙂

2 Responses to Starting with rosjava and image messages

  1. Felix Krull says:

    Thank you for the tutorial.
    Did you continue to work on the message? What do I do with the data, once I got them?
    Like converting it to a java.awt.Image?


  2. geus says:

    I don’t know very well the java.awt.Image API. However, as I said in You should handle the sensor_msg/Image datastructure and copy the data to your java.awt.Image. See this:

    $ 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

    How the data field should be interpreted depends (of course) on the values of other fields like: height, width, encoding and step parameter. To understand this values in a better way take a look to this Image-Talker c++ code:

        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;

    Please if you make any progresses on this stuff… share them with us!! we want to know what will happen 🙂


Introduce tus datos o haz clic en un icono para iniciar sesión:

Logo de

Estás comentando usando tu cuenta de Cerrar sesión /  Cambiar )

Google+ photo

Estás comentando usando tu cuenta de Google+. Cerrar sesión /  Cambiar )

Imagen de Twitter

Estás comentando usando tu cuenta de Twitter. Cerrar sesión /  Cambiar )

Foto de Facebook

Estás comentando usando tu cuenta de Facebook. Cerrar sesión /  Cambiar )


Conectando a %s

A %d blogueros les gusta esto: