Messages size of sensor_msgs.image/compressedImage type will changed when publish/subscriber in ROS-Android? -


i need preview-image-data android-phone-camera , publish ros, , here sample-code:

@override public void onpreviewframe(byte[] data, camera camera) {     if(data != null){         camera.size size = camera.getparameters().getpreviewsize();         yuvimage yuvimage = new yuvimage(data, imageformat.nv21, size.width, size.height, null);          if(yuvimage != null){             bytearrayoutputstream baos = new bytearrayoutputstream();             channelbufferoutputstream stream = new channelbufferoutputstream(messagebuffers.dynamicbuffer());              yuvimage.compresstojpeg(new rect(0, 0, yuvimage.getwidth(), yuvimage.getheight()), 80, baos);             yuvimage = null;              stream.buffer().writebytes(baos.tobytearray());             try{                 baos.flush();                 baos.close();                 baos = null;             }             catch(ioexception e){                 e.printstacktrace();             }              // compressedimage type             sensor_msgs.compressedimage compressedimage = compressedimagepublisher.newmessage();             compressedimage.getheader().setframeid("xxx");    // frame id              time curtime = connectednode.getcurrenttime();                 compressedimage.getheader().setstamp(curtime);    // time              compressedimage.setformat("jpeg");                // format             compressedimage.setdata(stream.buffer().copy());  // data              stream.buffer().clear();             try {                 stream.flush();                 stream.close();                 stream = null;             }             catch (ioexception e) {                 e.printstacktrace();             }              // publish             system.out.println("-----publish: " + compressedimage.getdata().array().length + "-----");             compressedimagepublisher.publish(compressedimage);             compressedimage = null;             system.gc();         }         else{             log.v("log_tag", "-----failed yuvimage!-----");         }     }     else{         log.v("log_tag", "-----failed preview frame!-----");     } } 

and then, had subscribed topic, check if messages had been published , correctly. following code did:

@override public void onstart(connectednode node) {     this.connectednode = node;      // publisher     this.compressedimagepublisher = connectednode.newpublisher(topic_name, sensor_msgs.compressedimage._type);     // subscriber     this.compressedimagesubscriber = connectednode.newsubscriber(topic_name, sensor_msgs.compressedimage._type);     compressedimagesubscriber.addmessagelistener(new messagelistener<compressedimage>() {         @override         public void onnewmessage(final compressedimage compressedimage) {             byte[] receivedimagebytes = compressedimage.getdata().array();             if(receivedimagebytes != null && receivedimagebytes.length != 0) {                 system.out.println("-----subscribe(+46?): " + receivedimagebytes.length + "-----");                  // decode bitmap byte[] strange number of offset , necessary                 bitmap bmp = bitmapfactory.decodebytearray(receivedimagebytes, offset, receivedimagebytes.length - offset);                 ...                 }         }     }); } 

i'm confused number of offset. it's means size of image-bytes had changed after packaged , published ros, , if don't set offset there're wrong decode bitmap. , more strangely, number of offset had change too.

i don't know why, , had read articles jpg structure, , suspect it's maybe head-information of jpg byte messages. however, problem happen in ros-android scene.

anyone have idea this?


ok! know question asked , problem described terrible, that's why got 2 negative-points. i'm sorry these, , have make fault telling guys more information, , making problem more clear now.

at first, forget code pasted before. problem happened in ros-android project. in project, need send sensor messages of compressed image type ros-server , processed image-bytes(jpg format) in publish/subscribe way. , in theory, size of image-bytes should same, , in fact, had been proved in ros-c , ros-c# project under same conditions.

however, they're different in ros-android, it's bigger! reason, can't decode bitmap image bytes had subscribed, , have leave out excrescent bytes offset in image byte array. don't know why happened in ros-android or ros-java, , what's these adding part.

i can't find reason code, that's why pasted code in detail you. need help! in advance!

logcat

maybe need check api first before ask question here. this's simple question if had check properties of channelbuffer in api, cause property arrayoffset had told me answer of question!


well, sake of more cautious , of guys need clarification of answer, have make explanation in details!

at first, have that, still don't know how channelbuffer package data of image bytes array, that's means still don't know why there're should have arrayoffset before array data. why can't data? there important reasons being of arrayoffset? sake of safety, or efficiency? don't know, can't find answer api. i'm tired of question now, , i'm tired of whether guys make negative point of question or not, let go!

hark subject, problem can solved in way:

int offset = compressedimage.getdata().arrayoffset(); bitmap bmp = bitmapfactory.decodebytearray(receivedimagebytes, offset, receivedimagebytes.length - offset); 

ok! i'm still hope have knowledge of can tell me why, i'll appreciate of this! if guys tired of question me, let's vote close it, i'll appreciate too! anyway!


Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -