title: Messages size of sensor_msgs.image/compressedImage type will changed when publish/subscriber in ROS-Android?
body: <p>I need to get the <strong>preview-image-data</strong> from <strong><em>Android-phone-camera</em></strong> and publish it by <strong><em>ROS</em></strong>, and here is my sample-code:</p>
  
  <pre><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 to get yuvImage!-----");
          }
      }
      else{
          Log.v("Log_Tag", "-----Failed to get the preview frame!-----");
      }
  }
  </code></pre>
  
  <p>And then, I had subscribed the <strong><em>topic</em></strong>, just to check if the messages had been published completely and correctly. Just like the following code did:</p>
  
  <pre><code>@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&lt;CompressedImage&gt;() {
          @Override
          public void onNewMessage(final CompressedImage compressedImage) {
              byte[] receivedImageBytes = compressedImage.getData().array();
              if(receivedImageBytes != null &amp;&amp; receivedImageBytes.length != 0) {
                  System.out.println("-----Subscribe(+46?): " + receivedImageBytes.length + "-----");
  
                  // decode bitmap from byte[] with a strange number of offset and necessary
                  Bitmap bmp = BitmapFactory.decodeByteArray(receivedImageBytes, offset, receivedImageBytes.length - offset);
                  ...    
              }
          }
      });
  }
  </code></pre>
  
  <p>I'm so confused about the number of <strong><em>offset</em></strong>. It's means the size of image-bytes had changed after packaged and published by ROS, and if I don't set the offset there're will be wrong to decode a bitmap. And more strangely, sometimes the number of offset had a change too.</p>
  
  <p>I don't know why, and I had read some articles about the <strong><em>jpg</em></strong> structure, and suspect that it's maybe the <strong><em>head-information</em></strong> of the jpg byte  messages. However, this problem just happen in <strong><em>ros-android</em></strong> scene.</p>
  
  <p>Anyone have a good idea about this? </p>
  
  <hr>
  
  <p>OK! I know that the question I asked and problem described previously is terrible, that's why it got 2 negative-points. I'm so sorry about these, and I have to make up my fault by telling you guys more information, and making the problem more clear now.</p>
  
  <p>At first, forget about all the code I pasted before. The problem happened in my <strong><em>ros-android</em></strong> project. In this project, I need to send the sensor messages of compressed image type to ros-server and get the processed image-bytes(jpg format) in publish/subscribe way. And in theory, the size of the image-bytes should be same, and in fact, this had been proved in my <strong><em>ros-C</em></strong> and <strong><em>ros-C#</em></strong> project under same conditions. </p>
  
  <p>However, they're different in ros-android, it's get bigger! For this reason, I can't just decode a bitmap from the image bytes which I had subscribed, and I have to leave out the excrescent bytes with a <strong><em>offset</em></strong> in the image byte array. I don't know why this happened in ros-android or ros-java, and what's these adding part.</p>
  
  <p>I can't find the reason from my code, that's why I pasted my code in detail for you. I really need help! Thanks in advance!</p>
  
  <p><a href="https://i.stack.imgur.com/7vdPz.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/7vdPz.png" alt="Logcat"></a></p>