我正在尝试在Android中实现一个应用程序。 Android需要做的是拍照并发布。 我的问题是转换sensor_msgs.Image类型的信息。 信息位于位图中。
我正在尝试转换它:
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;
}
目前没有必要使用标题。 问题出在sensor_msgs.Image的setData方法中:
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.869: E/AndroidRuntime(9550): at android.app.ActivityThread.deliverResults(ActivityThread.java:3178)
06-20 12:40:04.869: E/AndroidRuntime(9550): ... 11 more
任何人都知道为什么我不能这样做,或者我该怎么做?
谢谢。
圣保罗
答案 0 :(得分:0)
已经解决了运输问题。 在这一刻,我已经可以携带一张照片并在另一个节点中看到它。 为此,我在Android中使用了两个ROS节点(发布/订阅者)。 我通过两种方法解决了:CompressedImage和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);
}
i.setImageBitmap(...)用于比较图像是否相等。 使用sensor_msgs.CompressImage图像是相同的。但是使用sensor_msgs.Image不是。我怀疑问题在于图像的编码。
如果有人可以提供帮助,我将不胜感激。
祝你好运
圣保罗