ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

Create sensor_msgs.Image in Android

asked 2013-06-20 02:12:14 -0500

paulofinseca gravatar image

updated 2014-01-28 17:16:59 -0500

ngrennan gravatar image

Hello all.

I'm trying to implement an application in Android. What Android needs to do is take a photo, and publish it. My problem is to convert the information for type sensor_msgs.Image. The information is in a Bitmap.

I'm trying to convert this:

public void updateMessage(Bitmap object_data){
    Image image = node.getTopicMessageFactory().newFromType(Image._TYPE);
    image.setHeight(object_data.getHeight());
    image.setWidth(object_data.getWidth());
    image.setStep(object_data.getRowBytes());
    image.setEncoding("rgb8");

    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    object_data.compress(Bitmap.CompressFormat.PNG, 100, stream);
    image.setData(ChannelBuffers.copiedBuffer(stream.toByteArray()));

    msg = image;
}

For now the Header is not necessary. The problem is in the setData method of sensor_msgs.Image:

06-20 12:40:04.869: E/AndroidRuntime(9550): FATAL EXCEPTION: main
06-20 12:40:04.869: E/AndroidRuntime(9550): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=102, result=-1, data=Intent { (has extras) }} to activity {org.ros.android.android_tutorial_pubsub/org.ros.android.android_tutorial_pubsub.RaceMain}: java.lang.IllegalArgumentException
06-20 12:40:04.869: E/AndroidRuntime(9550):     at android.app.ActivityThread.deliverResults(ActivityThread.java:3182)
06-20 12:40:04.869: E/AndroidRuntime(9550):     at android.app.ActivityThread.handleSendResult(ActivityThread.java:3225)
06-20 12:40:04.869: E/AndroidRuntime(9550):     at android.app.ActivityThread.access$1100(ActivityThread.java:140)
06-20 12:40:04.869: E/AndroidRuntime(9550):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1275)
06-20 12:40:04.869: E/AndroidRuntime(9550):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-20 12:40:04.869: E/AndroidRuntime(9550):     at android.os.Looper.loop(Looper.java:137)
06-20 12:40:04.869: E/AndroidRuntime(9550):     at android.app.ActivityThread.main(ActivityThread.java:4898)
06-20 12:40:04.869: E/AndroidRuntime(9550):     at java.lang.reflect.Method.invokeNative(Native Method)
06-20 12:40:04.869: E/AndroidRuntime(9550):     at java.lang.reflect.Method.invoke(Method.java:511)
06-20 12:40:04.869: E/AndroidRuntime(9550):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
06-20 12:40:04.869: E/AndroidRuntime(9550):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
06-20 12:40:04.869: E/AndroidRuntime(9550):     at dalvik.system.NativeStart.main(Native Method)
06-20 12:40:04.869: E/AndroidRuntime(9550): Caused by: java.lang.IllegalArgumentException
06-20 12:40:04.869: E/AndroidRuntime(9550):     at com.google.common.base.Preconditions.checkArgument(Preconditions.java:76)
06-20 12:40:04.869: E/AndroidRuntime(9550):     at org.ros.internal.message.field.ChannelBufferField.setValue(ChannelBufferField.java:55)
06-20 12:40:04.869: E/AndroidRuntime(9550):     at org.ros.internal.message.MessageProxyInvocationHandler.invoke(MessageProxyInvocationHandler.java:46)
06-20 12:40:04.869: E/AndroidRuntime(9550):     at org.ros.internal.message.$Proxy3.setData(Native Method)
06-20 12:40:04.869: E/AndroidRuntime(9550):     at org.ros.android.android_tutorial_pubsub.services.ImagePublisher.updateMessage(ImagePublisher.java:70)
06-20 12:40:04.869: E/AndroidRuntime(9550):     at org.ros.android.android_tutorial_pubsub.RaceMain.onActivityResult(RaceMain.java:178)
06-20 12:40:04.869: E/AndroidRuntime(9550):     at android.app.Activity.dispatchActivityResult(Activity.java:5390)
06-20 12:40:04 ...
(more)
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2013-06-25 13:57:37 -0500

paulofinseca gravatar image

updated 2013-06-25 14:00:14 -0500

Already solved the transport problem. In this moment, i can already carry a picture and see it in another node. For this, I'm using two ROS nodes in Android (publish / subscriver). I solved through two methods: CompressedImage and Image:

public void updateCompressedImage(byte[] data, int w, int h, ImageView i){
    Preconditions.checkNotNull(data);

    Time currentTime = node.getCurrentTime();
    String frameId = "camera";

    CompressedImage image = node.getTopicMessageFactory().newFromType(CompressedImage._TYPE);
    image.setFormat("jpeg");
    image.getHeader().setStamp(currentTime);
    image.getHeader().setFrameId(frameId);

    try {
        stream.write(data);
    } catch (IOException e) {
        e.printStackTrace();
    }

    image.setData(stream.buffer().copy());

    i.setImageBitmap(BitmapFactory.decodeByteArray(stream.buffer().array(),
            0, stream.buffer().array().length));
    stream.buffer().clear();

    publisher.publish(image);

}

public void updateImage(byte[] data, int w, int h, int s, ImageView i){
    Preconditions.checkNotNull(data);

    Image image = node.getTopicMessageFactory().newFromType(Image._TYPE);
    image.setHeight(h);
    image.setWidth(w);
    image.setStep(s);
    image.setEncoding("rgb8");

    Time currentTime = node.getCurrentTime();
    String frameId = "android_camera";
    image.getHeader().setStamp(currentTime);
    image.getHeader().setFrameId(frameId);

    try {
        stream.write(data);
    } catch (IOException e) {
        e.printStackTrace();
        throw new RosRuntimeException(e);
    }

    image.setData(stream.buffer().copy());
    i.setImageBitmap(BitmapFactory.decodeByteArray(stream.buffer().array(),
            0, stream.buffer().array().length));
    stream.buffer().clear();

    publisher.publish(image);
}

The stream is:

ChannelBufferOutputStream stream = new ChannelBufferOutputStream(MessageBuffers.dynamicBuffer());

The i.setImageBitmap (...) is to compare if the images are equal. Using sensor_msgs.CompressImage images are equal. But using sensor_msgs.Image are not. I suspect the problem is in the encoding of the image.

If anyone can help I would appreciate.

Best regards

Paulo

edit flag offensive delete link more

Question Tools

Stats

Asked: 2013-06-20 02:12:14 -0500

Seen: 1,678 times

Last updated: Jun 25 '13