2013-05-31 3 views
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); 
    } 

} 

답변

0

내가 지금 가지고있는 유일한 해결책은 onSensorChanged에 플래그를 설정하는 것입니다(). flag == true이면 함수는 센서 출력을 기록하기 시작합니다.