Implementing Face Detection in Android*

By Roman P Khatko, Published: 10/28/2013, Last Updated: 10/28/2013

Download sample code

Face Detection Sample [PDF 206KB]


Face detection is an important functionality for many categories of mobile applications. It can provide additional search capabilities in photo catalogs, social applications, etc.

Face detection is also a first step in implementing face recognition functionality.

This article will review a standard Android API to detect faces on a saved image.


Android* SDK contains an API for Face Detection: class. This class detects faces on the image. To detect faces call findFaces method of FaceDetector class. findFaces method returns a number of detected faces and fills the FaceDetector.Faces[] array. Please note, that findFaces method supports only bitmaps in RGB_565 format at this time.

Each instance of the FaceDetector.Face class contains the following information:

  • Confidence that it’s actually a face – a float value between 0 and 1.
  • Distance between the eyes – in pixels.
  • Position (x, y) of the mid-point between the eyes.
  • Pose rotations (X, Y, Z).

Unfortunately, it doesn’t contain a framing rectangle that includes the detected face.

Sample code

Here is sample source code for face detection. This sample code enables a custom View that shows a saved image from an SD Card and draws transparent red circles on the detected faces.

Source code:

class Face_Detection_View extends View {
        private static final int MAX_FACES = 10;
        private static final String IMAGE_FN = "face.jpg";
        private Bitmap background_image;
        private FaceDetector.Face[] faces;
        private int face_count;
        // preallocate for onDraw(...)
        private PointF tmp_point = new PointF();
        private Paint tmp_paint = new Paint();
        public Face_Detection_View(Context context) {
                // Load an image from SD Card
                updateImage(Environment.getExternalStorageDirectory() + "/" + IMAGE_FN);
        public void updateImage(String image_fn) {
                // Set internal configuration to RGB_565
                BitmapFactory.Options bitmap_options = new BitmapFactory.Options();
                bitmap_options.inPreferredConfig = Bitmap.Config.RGB_565;
                background_image = BitmapFactory.decodeFile(image_fn, bitmap_options);
                FaceDetector face_detector = new FaceDetector(
                                background_image.getWidth(), background_image.getHeight(),
                faces = new FaceDetector.Face[MAX_FACES];
                // The bitmap must be in 565 format (for now).
                face_count = face_detector.findFaces(background_image, faces);
                Log.d("Face_Detection", "Face Count: " + String.valueOf(face_count));
        public void onDraw(Canvas canvas) {
                canvas.drawBitmap(background_image, 0, 0, null);
                for (int i = 0; i < face_count; i++) {
                        FaceDetector.Face face = faces[i];
                        canvas.drawCircle(tmp_point.x, tmp_point.y, face.eyesDistance(),

This sample will not scale the picture to fit on the screen for simplicity. In real applications you will need to scale the picture or scale the FaceDetector.Face attributes to match the provided screen space.


Android SDK provides a standard API for face detection on a saved image. To detect a face in the camera preview frame consider using the Camera.FaceDetectionListener class.

Unfortunately, Android SDK doesn’t support API to implement face recognition functionality.


FaceDetector class:

FaceDetector.Face class:

Camera.FaceDetectionListener class:

Intel, the Intel logo, Atom, and Core are trademarks of Intel Corporation in the U.S. and/or other countries. Copyright © 2013 Intel Corporation. All rights reserved. *Other names and brands may be claimed as the property of others.

Product and Performance Information


Intel's compilers may or may not optimize to the same degree for non-Intel microprocessors for optimizations that are not unique to Intel microprocessors. These optimizations include SSE2, SSE3, and SSSE3 instruction sets and other optimizations. Intel does not guarantee the availability, functionality, or effectiveness of any optimization on microprocessors not manufactured by Intel. Microprocessor-dependent optimizations in this product are intended for use with Intel microprocessors. Certain optimizations not specific to Intel microarchitecture are reserved for Intel microprocessors. Please refer to the applicable product User and Reference Guides for more information regarding the specific instruction sets covered by this notice.

Notice revision #20110804