0
好吧,我是新來的機器人,所以這可能是愚蠢的。但我正在開發一個應用程序,以在相機快門打開和關閉期間獲取陀螺儀數據(所以基本上陀螺儀以相機捕捉按鈕開始並以相機ShutterCallBack()結束)。問題在於,陀螺儀似乎不是以onClick()開始的主要活動。 下面是代碼:點擊按鈕後啓動陀螺儀
public class PreviewDemo extends Activity implements SensorEventListener{
private SensorManager mSensorManager;
private CameraPreview mPreview;
public static final int MEDIA_TYPE_IMAGE = 1;
public FrameLayout preview;
public long starttime;
public long endtime;
public long elapsed,elapsed1;
public long timestamp;
public List li;
public float TimeOld;
public int ii;
public ArrayList<String> lis = new ArrayList<String>();
public static final int TIME_CONSTANT = 30;
public static final float FILTER_COEFFICIENT = 0.98f;
//private Sensor gyroscope;
private float[] gyros = new float[3];
//@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.preview);
mSensorManager= (SensorManager) getSystemService(SENSOR_SERVICE);
mPreview = new CameraPreview(this);
FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
preview.addView(mPreview);
Button captureButton = (Button) findViewById(R.id.button_capture);
captureButton.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
starttime=System.currentTimeMillis();
mSensorManager.registerListener(PreviewDemo.this,
mSensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE),
SensorManager.SENSOR_DELAY_NORMAL);
mPreview.mCamera.takePicture(mShutter, rPicture, mPicture);
Log.d("starttime", "timenow"+starttime);
}
}
);
}
private ShutterCallback mShutter = new ShutterCallback() {public void onShutter(){
endtime=System.currentTimeMillis()-starttime;Log.d("endtime", "timenow"+endtime);onStop();}};
private PictureCallback rPicture = new PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
elapsed1=System.currentTimeMillis()-starttime;
Log.d("elapsed1", "timenow"+elapsed1);
}
};
private PictureCallback mPicture = new PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
long elapsed2=System.currentTimeMillis()-starttime;
Log.d("elapsed2", "timenow"+elapsed2);
File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);
if (pictureFile == null){
}
try {
FileOutputStream fos = new FileOutputStream(pictureFile);
fos.write(data);
fos.flush();
fos.close();
} catch (FileNotFoundException e) {
} catch (IOException e) {
}
mPreview.mCamera.stopPreview();
mPreview.mCamera.release();
mPreview.mCamera = null;
mPreview.surfaceCreated(mPreview.getHolder());
//preview.addView(mPreview);
}
};
private File getOutputMediaFile(int type){
long dtMili = System.currentTimeMillis();
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date(dtMili));
File mediaFile;
if (type == MEDIA_TYPE_IMAGE){
mediaFile = new File("/storage/sdcard0/testphotos/IMG_"+ timeStamp + ".jpg");
} else {
return null;
}
return mediaFile;
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
}
@Override
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() == Sensor.TYPE_GYROSCOPE) {
//synchronized (this) {
float TimeNew = event.timestamp;
float delay = (long)((TimeNew - TimeOld)/1000000);
TimeOld = TimeNew;
Log.d("Test", "gyrofrequency"+delay + " ms");
ii+=1;
Log.d("Test", "gyrosamples"+ii);
float[] values = event.values;
float x = values[0];
float y = values[1];
float z = values[2];
if ((event.timestamp - timestamp)/1000000 < 20) {
return;
}
timestamp = event.timestamp;
Log.d("adf","accele"+x);
}
}
@Override
protected void onStop() {
mSensorManager.unregisterListener(this,mSensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE));
super.onStop();
}
@Override
protected void onResume() {
super.onResume();
mSensorManager.registerListener(this,
mSensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE),
SensorManager.SENSOR_DELAY_NORMAL);
}
@Override
protected void onPause() {
// unregister listener
super.onPause();
mSensorManager.unregisterListener(this);
}
}