检测到脸部时拍照

时间:2014-12-11 19:48:07

标签: android camera android-camera face-detection

我有以下代码,我想在检测到脸部时自动只拍一张照片。 我已经实现了自动拍摄照片但是它需要很多照片而没有时间处理它们因为它不断检测到脸部。如何让它每隔x分钟搜索一次,或每隔x分钟拍照?提前谢谢。

FaceDetectionListener faceDetectionListener
= new FaceDetectionListener(){

    @Override
    public void onFaceDetection(Face[] faces, Camera camera) {

        if (faces.length == 0){
            prompt.setText(" No Face Detected! ");
        }else{
            //prompt.setText(String.valueOf(faces.length) + " Face Detected :) ");
              try{
                camera.takePicture(myShutterCallback,myPictureCallback_RAW, myPictureCallback_JPG);

               }
               catch(Exception e){

               }
        }


    }};

1 个答案:

答案 0 :(得分:2)

FaceDetectionListener faceDetectionListener
= new FaceDetectionListener(){

    private boolean processing = false;

    public void setProcessing(boolean processing) {
        this.processing = processing;
    }

    @Override
    public void onFaceDetection(Face[] faces, Camera camera) {
        if (processing) return;

        if (faces.length == 0){
            prompt.setText(" No Face Detected! ");
        }else{
            //prompt.setText(String.valueOf(faces.length) + " Face Detected :) ");
              try{
                   camera.takePicture(myShutterCallback,myPictureCallback_RAW, myPictureCallback_JPG);
                   processing = true;
               }
               catch(Exception e){

               }
        }


    }};

然后,您可以在myShutterCallback中执行您想要的任何处理,并致电faceDetectionListener.setProcessing(false)拍摄另一张照片。这样可以保证一次只能拍摄一张照片。