Is SurfaceView required for face detection?

Using API 14, I created an Activity that uses face recognition successfully (I'm a bit new to this Face Detection stuff).

However, I don't want to show the camera preview; I just want to know when the user's face is in front of the camera. I added some buttons to make the SurfaceView Visible / Invisible, and I found that face recognition stops working when it is NOT POSSIBLE or GONE.

Is there a way to enable face detection without requiring a SurfaceView in the layout?

This is how I coded it:

            mCamera.setPreviewDisplay(mSurfaceHolder);
            mCamera.startPreview();

            if(mCamera.getParameters().getMaxNumDetectedFaces() >0) {
                mCamera.setFaceDetectionListener(new Camera.FaceDetectionListener() {
                    @Override public void onFaceDetection(Face[] faces, Camera camera) {

                        if(faces.length > 0) {
                            System.out.println("Found someone");
                        }

                    }
                });
                mCamera.startFaceDetection();
            }

      

To hide the surface view, I added a black view. :-)

<View android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#000"/>

      

I had a sane view and haven't found much code yet using startFaceDetection ().

Thanks for any ideas / help.

+3


source to share


2 answers


You have to use a dummy SurfaceTexture for your own purposes.

  • Just create a SurfaceTexture object by passing in any integer like

     mSurfaceTexture = new SurfaceTexture(1);
    
          

  • Now open the camera and follow these steps:

    mCamera = Camera.open(Camera.CameraInfo.CAMERA_FACING_FRONT); 
     try{ 
      mCamera.setPreviewTexture(mSurfaceTexture);
    } 
    catch (IOException t) {
      //Do Something here   
    }
    
          



3) You can do everything else in the same way, for example using face detection.

+1


source


Camera preview is not displayed if you omit

setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

      

I haven't tested this with face detection yet (device # 4.0 and emulator don't support it yet). But it should work.



Link: https://groups.google.com/forum/?fromgroups#!topic/android-developers/EzBgJRetaCo

You can also try to use setPreviewTexture (SurfaceTexture st) instead of setPreviewDisplay and use SurfaceTexture which you can control.

0


source







All Articles