0

好吧,我是 android 新手,所以这可能很愚蠢。但是我正在开发一个应用程序来在相机快门打开和关闭期间获取陀螺仪数据(所以基本上陀螺仪从相机捕捉按钮开始并在相机 ShutterCallBack() 结束)。问题是陀螺仪似乎从 mainactivity 而不是 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);
    }

}
4

1 回答 1

1

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

于 2013-06-07T02:28:02.193 回答