title: Android Studio - draw face detection on camera preview (Real time face detection)
body: <p>I created a basic app that display the frontal camera of the phone. 
  I need to use the Google Servies face detector to draw rectangle around the face 
  (on the FrameLayout, that contain the camera preview data).
  I know the basic usage of the face detector and i know how to detect face in <strong>image view</strong>. Someone can Help me to apply the face detection on the frame layout of my app? Thanks a lot.</p>
  
  <p>My code:</p>
  
  <pre><code>public class MainActivity extends AppCompatActivity {
  
  private ImageSurfaceView mImageSurfaceView;
  private Camera camera;
  private FrameLayout cameraPreviewLayout;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
  
      cameraPreviewLayout = (FrameLayout)findViewById(R.id.camera_preview);
  
      camera = checkDeviceCamera();
  
      mImageSurfaceView = new ImageSurfaceView(MainActivity.this, camera);
      mImageSurfaceView.draw(canvas);
      cameraPreviewLayout.addView(mImageSurfaceView);
  }
  
  
  private Camera checkDeviceCamera() {
      Camera mCamera = null;
      try {
          mCamera = Camera.open(Camera.CameraInfo.CAMERA_FACING_FRONT); // set the cameras to the front (selfi)
      } catch (Exception e) {
          e.printStackTrace();
      }
      return mCamera;
  } 
  }
  </code></pre>
  
  <p>ImageSurfaceView Class:</p>
  
  <pre><code>public class ImageSurfaceView extends SurfaceView implements   SurfaceHolder.Callback {
  
  private Camera camera;
  private SurfaceHolder surfaceHolder;
  
  public ImageSurfaceView(Context context, Camera camera) {
      super(context);
      this.camera = camera;
  
      this.surfaceHolder = getHolder();
      this.surfaceHolder.addCallback(this);
  }
  
  @Override
  public void surfaceCreated(SurfaceHolder surfaceHolder) {
      try {
          this.camera.setPreviewDisplay(surfaceHolder);
          this.camera.startPreview();
      } catch (IOException e) {
          e.printStackTrace();
      }
  }
  
  @Override
  public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {
  }
  
  @Override
  public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
      this.camera.startPreview();
      this.camera.release();
  }  
  </code></pre>