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.
This package is the primary API for controlling device cameras. It can be used to take pictures or videos when you are building a camera application. Camera. This class is the older deprecated API for controlling device cameras. SurfaceView.
Both CameraX and Camera2 support Android 5.0 (API level 21) and higher. Camera2 is the low-level Android camera package that replaces the deprecated Camera class. Camera2 provides in-depth controls for complex use cases, but requires you to manage device-specific configurations.
Go to SETTINGS > APPS & NOTIFICATIONS (select, “See all Apps”) > scroll to CAMERA > STORAGE > Tap, “Clear Data”. Next, check to see if the camera is working fine. If you launch your device's Camera app and get an error message that says “another app is using the camera”, don't panic.
Apps could be secretly accessing your smartphone's microphone and camera to spy on you, or collect data to serve you targeted ads. To protect yourself, you can download an app that lets you know when the microphone or camera are turned on. You can also invest in some hardware to block out the microphone and camera.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With