CompressedImageView.java
Go to the documentation of this file.
00001 package org.ros.android.android_image_view;
00002 
00003 import org.jboss.netty.buffer.ChannelBuffer;
00004 import org.ros.message.MessageListener;
00005 import org.ros.namespace.GraphName;
00006 import org.ros.node.ConnectedNode;
00007 import org.ros.node.Node;
00008 import org.ros.node.NodeMain;
00009 import org.ros.node.topic.Subscriber;
00010 
00011 import android.content.Context;
00012 import android.graphics.Bitmap;
00013 import android.graphics.BitmapFactory;
00014 import android.util.AttributeSet;
00015 import android.view.MotionEvent;
00016 import android.view.ViewGroup;
00017 import android.widget.ImageView;
00018 
00019 public class CompressedImageView extends ImageView implements NodeMain {
00020 
00021         private String topicName;
00022         private String messageType;
00023         private Bitmap showBitmap;
00024         private float aspect;
00025         private TouchEventTalker talker ;
00026 
00027         public CompressedImageView(Context context) {
00028                 super(context);
00029         }
00030         
00031         public void setTalker( TouchEventTalker talker ){
00032                 this.talker = talker ;
00033         }
00034 
00035         public CompressedImageView(Context context, AttributeSet attrs) {
00036                 super(context, attrs);
00037         }
00038 
00039         public CompressedImageView(Context context, AttributeSet attrs, int defStyle) {
00040                 super(context, attrs, defStyle);
00041         }
00042 
00043         public void setTopicName(String topicName) {
00044                 this.topicName = topicName;
00045         }
00046 
00047         public void setMessageType(String messageType) {
00048                 this.messageType = messageType;
00049         }
00050 
00051         @Override
00052         public GraphName getDefaultNodeName() {
00053                 return GraphName.of("tap_on_image/compressed_image_view");
00054         }
00055 
00056         @Override
00057         public void onStart(ConnectedNode connectedNode) {
00058                 Subscriber<sensor_msgs.CompressedImage> subscriber = connectedNode
00059                                 .newSubscriber(topicName, messageType);
00060                 subscriber
00061                                 .addMessageListener(new MessageListener<sensor_msgs.CompressedImage>() {
00062                                         @Override
00063                                         public void onNewMessage(
00064                                                         final sensor_msgs.CompressedImage message) {
00065                                                 System.out.println( "receive message" ) ;
00066                                                 post(new Runnable() {
00067                                                         @Override
00068                                                         public void run() {
00069                                                                 ChannelBuffer buffer = message.getData();
00070                                                                 byte[] data = buffer.array();
00071                                                                 if (CompressedImageView.this.showBitmap != null) {
00072                                                                         CompressedImageView.this.showBitmap
00073                                                                                         .recycle();
00074                                                                 }
00075                                                                 CompressedImageView.this.showBitmap = BitmapFactory
00076                                                                                 .decodeByteArray(data,
00077                                                                                                 buffer.arrayOffset(),
00078                                                                                                 buffer.readableBytes());
00079                                                                 CompressedImageView.this.setBitmap();
00080                                                                 setImageBitmap(CompressedImageView.this.showBitmap);
00081                                                         }
00082                                                 });
00083                                                 postInvalidate();
00084                                         }
00085                                 });
00086         }
00087 
00088         public void setBitmap() {
00089                 setBitmap(this.showBitmap);
00090         }
00091 
00092         public void setBitmap(Bitmap bmp) {
00093                 this.showBitmap = bmp ;
00094                 float aspect = 1.0f * bmp.getWidth() / bmp.getHeight();
00095                 if (Math.abs(this.aspect - aspect) > 0.1) {
00096                         this.aspect = aspect;
00097                         float rate = //1.0f ;
00098                                         Math.min(1.0f * MainActivity.width / bmp.getWidth(),
00099                                         1.0f * MainActivity.height / bmp.getHeight());
00100                         ViewGroup.LayoutParams param = CompressedImageView.this
00101                                         .getLayoutParams();
00102                         param.width = (int) (rate * bmp.getWidth());
00103                         param.height = (int) (rate * bmp.getHeight());
00104                         this.setLayoutParams(param);
00105                 }
00106                 setImageBitmap(bmp);
00107                 postInvalidate();
00108         }
00109 
00110         @Override
00111         public boolean onTouchEvent(MotionEvent e) {
00112                 System.out.println(e.getX() + ":" + e.getY());
00113                 if ( this.talker != null ){
00114                         this.talker.publish( e.getX(), e.getY(), this.getWidth(), this.getHeight(), e.getAction() ) ;
00115                 }
00116                 return true;
00117         }
00118 
00119         @Override
00120         public void onShutdown(Node node) {
00121         }
00122 
00123         @Override
00124         public void onShutdownComplete(Node node) {
00125         }
00126 
00127         @Override
00128         public void onError(Node node, Throwable throwable) {
00129         }
00130 }


jsk_android_apps
Author(s): Kazuto Murase
autogenerated on Thu Jun 6 2019 18:03:46