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:


            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");



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

<View android:layout_width="fill_parent"


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

Thanks for any ideas / help.


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 =; 
    catch (IOException t) {
      //Do Something here   

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



Camera preview is not displayed if you omit



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


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



All Articles