CompressedImagePublisher.java
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2011 Google Inc.
00003  * 
00004  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
00005  * use this file except in compliance with the License. You may obtain a copy of
00006  * the License at
00007  * 
00008  * http://www.apache.org/licenses/LICENSE-2.0
00009  * 
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
00012  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
00013  * License for the specific language governing permissions and limitations under
00014  * the License.
00015  */
00016 
00017 package org.ros.android.view.camera;
00018 
00019 import com.google.common.base.Preconditions;
00020 
00021 import android.graphics.ImageFormat;
00022 import android.graphics.Rect;
00023 import android.graphics.YuvImage;
00024 import android.hardware.Camera.Size;
00025 import org.jboss.netty.buffer.ChannelBufferOutputStream;
00026 import org.ros.internal.message.MessageBuffers;
00027 import org.ros.message.Time;
00028 import org.ros.namespace.NameResolver;
00029 import org.ros.node.ConnectedNode;
00030 import org.ros.node.topic.Publisher;
00031 
00037 class CompressedImagePublisher implements RawImageListener {
00038 
00039   private final ConnectedNode connectedNode;
00040   private final Publisher<sensor_msgs.CompressedImage> imagePublisher;
00041   private final Publisher<sensor_msgs.CameraInfo> cameraInfoPublisher;
00042 
00043   private byte[] rawImageBuffer;
00044   private Size rawImageSize;
00045   private YuvImage yuvImage;
00046   private Rect rect;
00047   private ChannelBufferOutputStream stream;
00048 
00049   public CompressedImagePublisher(ConnectedNode connectedNode) {
00050     this.connectedNode = connectedNode;
00051     NameResolver resolver = connectedNode.getResolver().newChild("camera");
00052     imagePublisher =
00053         connectedNode.newPublisher(resolver.resolve("image/compressed"),
00054             sensor_msgs.CompressedImage._TYPE);
00055     cameraInfoPublisher =
00056         connectedNode.newPublisher(resolver.resolve("camera_info"), sensor_msgs.CameraInfo._TYPE);
00057     stream = new ChannelBufferOutputStream(MessageBuffers.dynamicBuffer());
00058   }
00059 
00060   @Override
00061   public void onNewRawImage(byte[] data, Size size) {
00062     Preconditions.checkNotNull(data);
00063     Preconditions.checkNotNull(size);
00064     if (data != rawImageBuffer || !size.equals(rawImageSize)) {
00065       rawImageBuffer = data;
00066       rawImageSize = size;
00067       yuvImage = new YuvImage(rawImageBuffer, ImageFormat.NV21, size.width, size.height, null);
00068       rect = new Rect(0, 0, size.width, size.height);
00069     }
00070 
00071     Time currentTime = connectedNode.getCurrentTime();
00072     String frameId = "camera";
00073 
00074     sensor_msgs.CompressedImage image = imagePublisher.newMessage();
00075     image.setFormat("jpeg");
00076     image.getHeader().setStamp(currentTime);
00077     image.getHeader().setFrameId(frameId);
00078 
00079     Preconditions.checkState(yuvImage.compressToJpeg(rect, 20, stream));
00080     image.setData(stream.buffer().copy());
00081     stream.buffer().clear();
00082 
00083     imagePublisher.publish(image);
00084 
00085     sensor_msgs.CameraInfo cameraInfo = cameraInfoPublisher.newMessage();
00086     cameraInfo.getHeader().setStamp(currentTime);
00087     cameraInfo.getHeader().setFrameId(frameId);
00088 
00089     cameraInfo.setWidth(size.width);
00090     cameraInfo.setHeight(size.height);
00091     cameraInfoPublisher.publish(cameraInfo);
00092   }
00093 }


android_core
Author(s): Damon Kohler
autogenerated on Thu Aug 27 2015 12:11:33