满足条件时捕获图像

时间:2014-07-11 19:27:52

标签: android android-camera

全心全意,

我正在开发一个应用程序,我需要在满足某些条件时捕获图像。到目前为止,我有一个SurfaceView预览后置摄像头,我的所有条件都完成了。我希望在满足某些条件时让相机拍照。当用户使用该应用时,将会拍摄多张照片。因此预览将继续,直到满足所有条件。有关如何做到这一点的任何建议?谢谢!

: 我找到了一些代码并开始研究它。

Camera.ShutterCallback myShutterCallback = new Camera.ShutterCallback() {
    public void onShutter() {
      // TODO bl
    }
  };

  PictureCallback myPictureCallback_RAW = new PictureCallback() {

    public void onPictureTaken(byte[] arg0, Camera arg1) {
        // TODO Auto-generated method stub

    }
  };
  PictureCallback myPictureCallback_JPG = new PictureCallback(){

    @Override
    public void onPictureTaken(byte[] data, Camera camera) {
            File imagesFolder = new File(Environment.getExternalStorageDirectory(), "/ImagesFromCamera");
            imagesFolder.mkdirs(); 
            //Unix time stamp for unique file naming. 
            long unixTime = System.currentTimeMillis() / 1000L; 
            String fileName = "image"+ unixTime +".jpg";
            File output = new File(imagesFolder, fileName);
            try {
                FileOutputStream fos = new FileOutputStream(output);
                fos.write(data);
                fos.close();
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            // Notify the user when the image is taken. 
                Toast.makeText(CustomCameraActivity.this, 
                        "Image saved.", 
                        Toast.LENGTH_SHORT).show();
            //Continue the Preview as it will stop once an image is taken.  
            camera.startPreview();
            }
    };

            //Will call the camera to take a photo.
        camera.takePicture(myShutterCallback, myPictureCallback_RAW, myPictureCallback_JPG);

1 个答案:

答案 0 :(得分:0)

此时,您需要在满足条件时在相机上调用takePicture()。然后在startPreview()回调的第一行中onPictureTaken(),您必须提供该回调才能让相机继续播放。

当满足所有条件时,在离开使用相机的活动release()之前,请务必onPause()相机。因为这可能会导致自己在应用程序或其他程序的其他负载上出现问题。