title: Android camera code
body: <p>I am trying to make an android application which uses camera. but somehow this code is not working. Any help would be highly appreciated. Thanks in Advance.</p>
  
  <p>Here is the code of my application.</p>
  
  <p>aractivity.java
      package com.tanuj.myarapp;</p>
  
  <pre><code>import android.location.Location;
  import android.location.LocationListener;  
  import android.location.LocationManager;
   import android.os.Bundle;
  import android.app.Activity;
  import android.hardware.Camera;
  import android.hardware.Sensor;
  import android.hardware.SensorEvent;
  import android.hardware.SensorEventListener;
  import android.hardware.SensorManager;
   import android.util.Log;
  import android.view.Menu;
  import android.view.MenuItem;
  import android.view.SurfaceHolder;
  import android.view.SurfaceView;
  import android.support.v4.app.NavUtils;
  
  public class Aractivity extends Activity {
  
  SurfaceView cameraPreview;
  SurfaceHolder previewHolder;
  Camera camera;
  boolean inPreview;
  final static String TAG = "PAAR";
  SensorManager sensorManager;
  int orientationSensor;
  int accelerometerSensor;
  float headingAngle;
  float pitchAngle;
  float rollAngle;
  float xAxis;
  float yAxis;
  float zAxis;
  LocationManager locationManager;
  double latitude;
  double longitude;
  double altitude;
  
      final SensorEventListener sensorEventListener = new SensorEventListener() {
          public void onSensorChanged(SensorEvent sensorEvent) {
          if (sensorEvent.sensor.getType() == Sensor.TYPE_ORIENTATION)
          {
          headingAngle = sensorEvent.values[0];
          pitchAngle = sensorEvent.values[1];
          rollAngle = sensorEvent.values[2];
          Log.d(TAG, "Heading: " + String.valueOf(headingAngle));
          Log.d(TAG, "Pitch: " + String.valueOf(pitchAngle));
          Log.d(TAG, "Roll: " + String.valueOf(rollAngle));
          }
          else if (sensorEvent.sensor.getType() == Sensor.TYPE_ACCELEROMETER)
          {
          xAxis = sensorEvent.values[0];
          yAxis = sensorEvent.values[1];
          zAxis = sensorEvent.values[2];
          Log.d(TAG, "X Axis: " + String.valueOf(xAxis));
          Log.d(TAG, "Y Axis: " + String.valueOf(yAxis));
          Log.d(TAG, "Z Axis: " + String.valueOf(zAxis));
          }
          }
      public void onAccuracyChanged (Sensor senor, int accuracy) {
      //Not used
      }
      };
      LocationListener locationListener = new LocationListener() {
          public void onLocationChanged(Location location) {
          latitude = location.getLatitude();
          longitude = location.getLongitude();
          altitude = location.getAltitude();
          Log.d(TAG, "Latitude: " + String.valueOf(latitude));
          Log.d(TAG, "Longitude: " + String.valueOf(longitude));
          Log.d(TAG, "Altitude: " + String.valueOf(altitude));
          }
          public void onProviderDisabled(String arg0) {
          // TODO Auto-generated method stub
          }
          public void onProviderEnabled(String arg0) {
          // TODO Auto-generated method stub
          }
          public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
          // TODO Auto-generated method stub
          }
          };
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_aractivity);
       inPreview = false;
      cameraPreview = (SurfaceView)findViewById(R.id.cameraPreview);
      previewHolder = cameraPreview.getHolder();
      previewHolder.addCallback(surfaceCallback);
      previewHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
      sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
      orientationSensor = Sensor.TYPE_ORIENTATION;
      accelerometerSensor = Sensor.TYPE_ACCELEROMETER;
  
          sensorManager.registerListener(sensorEventListener,
                  sensorManager.getDefaultSensor(orientationSensor),
                  SensorManager.SENSOR_DELAY_NORMAL);
          sensorManager.registerListener(sensorEventListener, sensorManager
                  .getDefaultSensor(accelerometerSensor), SensorManager.SENSOR_DELAY_NORMAL);
          locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
          locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 2,
          locationListener);
  
  }
  
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
      getMenuInflater().inflate(R.menu.activity_aractivity, menu);
      return true;
  }
  @Override
  public void onResume() {
  super.onResume();
  locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000,
          2, locationListener);
  sensorManager.registerListener(sensorEventListener, sensorManager
          .getDefaultSensor(orientationSensor), SensorManager.SENSOR_DELAY_NORMAL);
  sensorManager.registerListener(sensorEventListener, sensorManager
          .getDefaultSensor(accelerometerSensor), SensorManager.SENSOR_DELAY_NORMAL);
  camera=Camera.open();
  }
  @Override
  public void onPause() {
  if (inPreview) {
  camera.stopPreview();
  }
  locationManager.removeUpdates(locationListener);
  sensorManager.unregisterListener(sensorEventListener);
  
  camera.release();
  camera=null;
  inPreview=false;
  super.onPause();
  }
  SurfaceHolder.Callback surfaceCallback=new SurfaceHolder.Callback() {
      public void surfaceCreated(SurfaceHolder holder) {
      try {
      camera.setPreviewDisplay(previewHolder);
      }
      catch (Throwable t) {
      Log.e("ProAndroidAR2Activity", "Exception i");
      }
      }
      public void surfaceChanged(SurfaceHolder holder, int format, int width, int
              height) {
              Camera.Parameters parameters=camera.getParameters();
              Camera.Size size=getBestPreviewSize(width, height, parameters);
              if (size!=null) {
              parameters.setPreviewSize(size.width, size.height);
              camera.setParameters(parameters);
              camera.startPreview();
              inPreview=true;     
              }
      }
      public void surfaceDestroyed(SurfaceHolder holder) {
          /*camera.stopPreview();
          camera.release();
          camera=null;
          */
          }
  };
  private   Camera.Size getBestPreviewSize(int width, int height,
          Camera.Parameters parameters) {
          Camera.Size result=null;
          for (Camera.Size size : parameters.getSupportedPreviewSizes()) {
          if (size.width&lt;=width &amp;&amp; size.height&lt;=height) {
          if (result==null) {
          result=size;
          }
          else {
          int resultArea=result.width*result.height;
          int newArea=size.width*size.height;
          if (newArea&gt;resultArea) {
          result=size;
          }
          }
          }
          }
          return(result);
          }
  
  
             }
  </code></pre>
  
  <p>manifest file
          &lt; manifest xmlns:android="http://schemas.android.com/apk/res/android"
           package="com.tanuj.myarapp"
           android:versionCode="1"
           android:versionName="1.0" 
          ></p>
  
  <pre><code>       &lt;uses-sdk
         android:minSdkVersion="8"
         android:targetSdkVersion="15" /&gt;
  
      &lt;application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
       android:theme="@style/AppTheme" &gt;
         &lt;activity
          android:name=".Aractivity"
          android:label="@string/title_activity_aractivity" 
          android:screenOrientation = "landscape"
          android:configChanges = "keyboardHidden|orientation"
          &gt;
          &lt;intent-filter&gt;
              &lt;action android:name="android.intent.action.MAIN" /&gt;
  
              &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
          &lt;/intent-filter&gt;
      &lt;/activity&gt;
  &lt;/application&gt;
  &lt;uses-feature android:name="android.hardware.camera" /&gt;
  &lt;uses-permission android:name="android.permission.CAMERA" /&gt;
  &lt;uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /&gt;
  
   &lt;/manifest&gt;
  </code></pre>
  
  <p>activity_aractivity.xml(layout)</p>
  
  <pre><code>     &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
       xmlns:tools="http://schemas.android.com/tools"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       &gt;
  
         &lt;android.view.SurfaceView
          xmlns:android="http://schemas.android.com/apk/res/android"
          android:id="@+id/cameraPreview"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent" &gt;
          &lt;/android.view.SurfaceView&gt;
      &lt;/RelativeLayout&gt;
  
  
  output of logcat
        '11-10 17:56:04.153: W/IInputConnectionWrapper(386): showStatusIcon on inactive         InputConnection
         11-10 17:56:16.732: D/er(386): in open()
         11-10 17:56:17.442: D/er(386): inOnresune'
  </code></pre>