Question

alright, I'm new to android so this maybe stupid. But I'm developing a app to get gyro data during camera shutter open and close (so basically gyro starts with camera capture button and ends in camera ShutterCallBack()). The questions is it seems that gyroscope starts with mainactivity not with onClick(). Here is the code:

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);
    }

}
Was it helpful?

Solution

The only solution I have now is to set a flag in onSensorChanged(). Function will start to record sensor outputs when flag==true.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top