Face Detection using Android Camera Preview
I have been working on making a perfect Camera Preview interface with burst capability, touch focus, face detection, frame processing, pinch zoom and whatever that I could think of.
Android provides its own face detection API within Android.Camera given that your device supports it. Nexus 4 seems to support it, but Nexus 7 does not. This face detection is hardware based (probably qualcom chip).It’s quite accurate and works real time. Anyway, moving on to the implementation part.
It’s really easy to implement dace detection. All I had to do was create a Camera.FaceDetectionListener and attach it to the Camera in Camera.PreviewCallback.
Implementation
Things to remember:
- Check if face detection is supported by the device or not using Camara.Parameters
- When calling Camera.startFaceDetection() function, check if face detection is not already running.
- Android.Hardware.Camera gives face coordinates based on its sensor values which range from (-1000, -1000) to (1000, 1000). First one being top left corner and the latter one being bottom right corner. So now you must resample those coordinates based on your preview size or canvas size.
A class that implements Camera.PreviewCallback
I implented a custom view(based on View) on top of the Preview Surface to show rectangles drawn around the faces.
P.S. Working on an app which corrects selfies.
Playing around with Android UI
Articles focusing on Android UI - playing around with ViewPagers, CoordinatorLayout, meaningful motions and animations, implementing difficult customized views, etc.