按下按钮后启动陀螺仪

时间:2013-05-31 06:47:08

标签: android camera gyroscope

好吧,我是android新手,所以这可能是愚蠢的。但我正在开发一款应用程序,以便在相机快门打开和关闭期间获取陀螺仪数据(因此,陀螺仪首先开始使用相机捕捉按钮,然后以相机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);
    }

}

1 个答案:

答案 0 :(得分:1)

我现在唯一的解决方案是在onSensorChanged()中设置一个标志。当flag == true时,函数将开始记录传感器输出。