Lisa Anne
Lisa Anne

Reputation: 4595

How is it possible to use Android Camera APIs in non-Camera applications?

I would like to use some Android 4 APIs in a non-Camera application.

The API includes some very nice Face Detection classes, including the Camera.Face class available since API 14. I would like to apply the same Face Detection classes in order to implement face detection on images saved on the device. I would prefer to use this to process pictures stored on the device itself (ex: social tagging, face image manipulation, etc.)

I require guidance on how to accomplish this re-use task.

Upvotes: 11

Views: 1315

Answers (3)

iTech
iTech

Reputation: 18440

If what you need is to detect faces in images stored on the device, you can definitely do this without hacking the source code of android!

There is a FaceDetector API that is available under the package android.media since API 1, which accepts Bitmap as input (formatted in 565 format) and give you the position of faces in that picture.

Here are the steps you need:

1- Load the Bitmap and convert it to 565 format (assuming you have facesPicture file under your drawable resources)

Bitmap originalBitmap = 
            BitmapFactory.decodeResource(getResources(),R.drawable.facesPicture);

Bitmap bitmap = originalBitmap .copy(Bitmap.Config.RGB_565, true);

originalBitmap .recycle(); // allow the GC to collect this object

2- Define Face array to hold the detected faces information and initialize the FaceDetector

int MAX_FACES = 20; // assuming that the image will have maximum 20 faces

FaceDetector.Face[] faces = new FaceDetector.Face[MAX_FACES];

FaceDetector faceDetector = 
             new FaceDetector(bitmap.getWidth(), bitmap.getHeight(), MAX_FACES);

3- Search for the faces and process results

int facesCount = faceDetector.findFaces(bitmap, faces);

for(int i=0; i<facesCount; i++) {
    FaceDetector.Face face = faces[i];

    float detectionConfidence = face.confidence(); // over 0.3 is OK

    PointF eyesMidPoint = new PointF();

    face.getMidPoint(eyesMidPoint);

    float eyesDistance = face.eyesDistance();

    float rotationX = face.pose(FaceDetector.Face.EULER_X);

    float rotationY = face.pose(FaceDetector.Face.EULER_Y);

    float rotationZ = face.pose(FaceDetector.Face.EULER_Z);

    // Do something with these values

}

You can download a complete project example here which is explained in this article Face Detection with Android APIs

If you want something more advanced you should consider using OpenCV

Upvotes: 6

a.bertucci
a.bertucci

Reputation: 12142

Are you aware of FaceDetector class? It's there since API v1 and it works quite well. If you need something more advanced you can always use some dedicated frameworks, like OpenCV (provided with some Java bindings as well, in order to be easily integrated within Android apps)

You can check a couple of (little dated, but still useful) articles I made sometime ago with a colleague:

  1. Face Detection on Andriod Part-I
  2. Face Detection on Andriod Part-II

Upvotes: 2

NathanTempelman
NathanTempelman

Reputation: 1387

The FaceDetectionListener is what you'd want to use to detect faces, but it only listens on the camera. That's its only native function. If you really want use it on pictures on the user's device, I'd suggest just downloading the source code for the camera API and adapting the method you want to your needs.

You can find the source for all stock android code here: https://android.googlesource.com/

Good luck!

Upvotes: 3

Related Questions