停止拍照

时间:2014-05-21 15:31:55

标签: android timer onpause

我的问题是关于每个人制作图片的代码" x"秒并将它们保存在SD卡中。我不想永远拍照,所以应用程序中有一个按钮,它应该停止拍照。事情是我不知道如何让它停止,我应该停止计时器还是有任何其他选择来阻止它?

提前致谢!

我附上了我正在使用的代码:

public class MainActivity extends Activity {
    private Camera mCamera;
    private Preview mCameraPreview;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mCamera = getCameraInstance();
        mCameraPreview = new Preview(this, mCamera);
        FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
        preview.addView(mCameraPreview);

        Button captureButton = (Button) findViewById(R.id.button_capture);
        Button pauseButton = (Button) findViewById(R.id.button_pause);

        captureButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Timer timer = new Timer();
                timer.schedule(new TimerTask() {
                    @Override
                    public void run() {
                        mCamera.startPreview();
                        mCamera.takePicture(null,null,mPicture);
                    }
                }, 0, 2000);
            }
        });
    }

    private Camera getCameraInstance() {
        Camera camera = null;
        try {
            camera = Camera.open();
        } catch (Exception e) {
            // cannot get camera or does not exist
        }
        return camera;
    }

    PictureCallback mPicture = new PictureCallback() {
        @Override
        public void onPictureTaken(byte[] data, Camera camera) {
            File pictureFile = getOutputMediaFile();
            if (pictureFile == null) {
                return;
            }
            try {
                FileOutputStream fos = new FileOutputStream(pictureFile);
                fos.write(data);
                fos.close();
            } catch (FileNotFoundException e) {

            } catch (IOException e) {
            }
        }

    };

    private static File getOutputMediaFile() {
        File mediaStorageDir = new File(
                Environment
                        .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
                "FOTOS");
        if (!mediaStorageDir.exists()) {
            if (!mediaStorageDir.mkdirs()) {
                Log.d("FOTOS", "failed to create directory");
                return null;
            }
        }
        // Create a media file name
        String timeStamp = new SimpleDateFormat("HHmmss_ddMMyyyy")
                .format(new Date());
        File mediaFile;
        mediaFile = new File(mediaStorageDir.getPath() + File.separator
                + "IMG_" + timeStamp + ".jpg");

        return mediaFile;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

2 个答案:

答案 0 :(得分:0)

将您在onClick中创建的计时器存储在类级别变量中。然后,当用户单击停止按钮时,使用timer.cancel()取消定时器。

答案 1 :(得分:0)

以下是答案:

public class MainActivity extends Activity {
    private Camera mCamera = null;
    private Preview mCameraPreview = null;
    private Timer mTimer = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);
       mCamera = getCameraInstance();
       mCameraPreview = new Preview(this, mCamera);
       FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
       preview.addView(mCameraPreview);

       Button captureButton = (Button) findViewById(R.id.button_capture);
       Button pauseButton = (Button) findViewById(R.id.button_pause);

       captureButton.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
             if (mTimer != null) mTimer.cancel();
             mTimer = new Timer();
             mTimer.schedule(new TimerTask() {
                @Override
                public void run() {
                    mCamera.startPreview();
                    mCamera.takePicture(null,null,mPicture);
                }
             }, 0, 2000);
          }
       });
       pauseButton.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
            if (mTimer != null) mTimer.cancel();
          }
       });
    }

    private Camera getCameraInstance() {
       Camera camera = null;
       try {
          camera = Camera.open();
       } catch (Exception e) {
          // cannot get camera or does not exist
       }
       return camera;
    }

    PictureCallback mPicture = new PictureCallback() {
       @Override
       public void onPictureTaken(byte[] data, Camera camera) {
          File pictureFile = getOutputMediaFile();
          if (pictureFile == null) {
             return;
          }
          try {
             FileOutputStream fos = new FileOutputStream(pictureFile);
             fos.write(data);
             fos.close();
          } catch (FileNotFoundException e) {

          } catch (IOException e) {
          }
       }

    };

    private static File getOutputMediaFile() {
       File mediaStorageDir = new File(
             Environment
                    .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
             "FOTOS");
       if (!mediaStorageDir.exists()) {
          if (!mediaStorageDir.mkdirs()) {
             Log.d("FOTOS", "failed to create directory");
             return null;
          }
       }
       // Create a media file name
       String timeStamp = new SimpleDateFormat("HHmmss_ddMMyyyy")
             .format(new Date());
       File mediaFile;
       mediaFile = new File(mediaStorageDir.getPath() + File.separator
             + "IMG_" + timeStamp + ".jpg");

       return mediaFile;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
       // Inflate the menu; this adds items to the action bar if it is present.
       getMenuInflater().inflate(R.menu.main, menu);
       return true;
    }

}