Android相机代码

我正在试图制作一款使用相机的Android应用程序。 但不知何故这个代码不起作用。 任何帮助将不胜感激。 提前致谢。

这是我的应用程序的代码。

aractivity.java package com.tanuj.myarapp;

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<=width && size.height<=height) {
        if (result==null) {
        result=size;
        }
        else {
        int resultArea=result.width*result.height;
        int newArea=size.width*size.height;
        if (newArea>resultArea) {
        result=size;
        }
        }
        }
        }
        return(result);
        }


           }

清单文件<manifest xmlns:android =“http://schemas.android.com/apk/res/android”package =“com.tanuj.myarapp”android:versionCode =“1”android:versionName =“1.0”>

       <uses-sdk
       android:minSdkVersion="8"
       android:targetSdkVersion="15" />

    <application
      android:icon="@drawable/ic_launcher"
      android:label="@string/app_name"
     android:theme="@style/AppTheme" >
       <activity
        android:name=".Aractivity"
        android:label="@string/title_activity_aractivity" 
        android:screenOrientation = "landscape"
        android:configChanges = "keyboardHidden|orientation"
        >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>
<uses-feature android:name="android.hardware.camera" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

 </manifest>

activity_aractivity.xml(布局)

     <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"
     >

       <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" >
        </android.view.SurfaceView>
    </RelativeLayout>


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'
链接地址: http://www.djcxy.com/p/90821.html

上一篇: Android camera code

下一篇: Android how to use Environment.getExternalStorageDirectory()