MainActivity.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.android_tutorial_camera;
00018 
00019 import android.hardware.Camera;
00020 import android.os.Bundle;
00021 import android.view.MotionEvent;
00022 import android.view.Window;
00023 import android.view.WindowManager;
00024 import android.widget.Toast;
00025 import org.ros.address.InetAddressFactory;
00026 import org.ros.android.RosActivity;
00027 import org.ros.android.view.camera.RosCameraPreviewView;
00028 import org.ros.node.NodeConfiguration;
00029 import org.ros.node.NodeMainExecutor;
00030 import android.util.Log;
00031 import java.io.IOException;
00032 
00037 public class MainActivity extends RosActivity {
00038 
00039   private int cameraId;
00040   private RosCameraPreviewView rosCameraPreviewView;
00041 
00042   public MainActivity() {
00043     super("CameraTutorial", "CameraTutorial");
00044   }
00045 
00046   @Override
00047   protected void onCreate(Bundle savedInstanceState) {
00048     super.onCreate(savedInstanceState);
00049     requestWindowFeature(Window.FEATURE_NO_TITLE);
00050     getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
00051     setContentView(R.layout.main);
00052     rosCameraPreviewView = (RosCameraPreviewView) findViewById(R.id.ros_camera_preview_view);
00053   }
00054 
00055   @Override
00056   public boolean onTouchEvent(MotionEvent event) {
00057     if (event.getAction() == MotionEvent.ACTION_UP) {
00058       int numberOfCameras = Camera.getNumberOfCameras();
00059       final Toast toast;
00060       if (numberOfCameras > 1) {
00061         cameraId = (cameraId + 1) % numberOfCameras;
00062         rosCameraPreviewView.releaseCamera();
00063         rosCameraPreviewView.setCamera(Camera.open(cameraId));
00064         toast = Toast.makeText(this, "Switching cameras.", Toast.LENGTH_SHORT);
00065       } else {
00066         toast = Toast.makeText(this, "No alternative cameras to switch to.", Toast.LENGTH_SHORT);
00067       }
00068       runOnUiThread(new Runnable() {
00069         @Override
00070         public void run() {
00071           toast.show();
00072         }
00073       });
00074     }
00075     return true;
00076   }
00077 
00078   @Override
00079   protected void init(NodeMainExecutor nodeMainExecutor) {
00080     cameraId = 0;
00081 
00082     rosCameraPreviewView.setCamera(Camera.open(cameraId));
00083     try {
00084       java.net.Socket socket = new java.net.Socket(getMasterUri().getHost(), getMasterUri().getPort());
00085       java.net.InetAddress local_network_address = socket.getLocalAddress();
00086       socket.close();
00087       NodeConfiguration nodeConfiguration =
00088               NodeConfiguration.newPublic(local_network_address.getHostAddress(), getMasterUri());
00089       nodeMainExecutor.execute(rosCameraPreviewView, nodeConfiguration);
00090     } catch (IOException e) {
00091         // Socket problem
00092         Log.e("Camera Tutorial", "socket error trying to get networking information from the master uri");
00093     }
00094 
00095   }
00096 }


android_core
Author(s): Damon Kohler
autogenerated on Thu Jun 6 2019 21:20:07