Android Question Set Camera FaceDetectionListener callback from B4A ?

JordiCP

Expert
Licensed User
Longtime User
Is it possible to add a FaceDetectionListener callback through Reflection (or other ways) using the camera library?
I am able to get and set parameters using Reflection and CameraEx class, but no idea on know how to do it for setting callbacks (if possible)

I know it is only available from API 14, but it would be nice, on devices supporting it, to have access to it.


Thanks.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
It was a bit more difficult than I expected. The image must be formatted in RGB_565. So I added another bitmap with a canvas to convert it:
You should call PrepareFaceDetection once with the correct width and height and then you can use it multiple times:
B4X:
Sub Process_Globals
   Private mbmp As Bitmap
   Private faces() As Object
   Private faceDetector As JavaObject
End Sub

Sub Globals
   Private cvs As Canvas
End Sub

Sub Activity_Create(FirstTime As Boolean)
   Dim bmp As Bitmap = LoadBitmap(File.DirAssets, "IMG_1516.JPG")
   If FirstTime Then
     PrepareFaceDetection(bmp.Width, bmp.Height, 3)
   End If
   cvs.Initialize2(mbmp)
   Dim num As Int = FindFaces(bmp)
   For i = 0 To num - 1
     Dim f As JavaObject = faces(i)
     GetMidPoint(f)
   Next
End Sub

Sub GetMidPoint(Face As JavaObject)
   Dim p As JavaObject
   p.InitializeNewInstance("android.graphics.PointF", Null)
   Face.RunMethod("getMidPoint", Array As Object(p))
   Log("x: " & p.GetField("x"))
   Log("y: " & p.GetField("y"))
End Sub

Sub PrepareFaceDetection(Width As Int, Height As Int, MaxNumberOfFaces As Int)
   Dim jo As JavaObject
   jo.InitializeStatic("android.graphics.Bitmap")
   mbmp = jo.RunMethod("createBitmap", Array As Object(Width, Height, "RGB_565"))
   Dim ar, face As JavaObject
   ar.InitializeStatic("java.lang.reflect.Array")
   face.InitializeStatic("android.media.FaceDetector$Face")
   faces = ar.RunMethod("newInstance", Array As Object(face, MaxNumberOfFaces))
   faceDetector.InitializeNewInstance("android.media.FaceDetector", Array As Object(Width, Height, MaxNumberOfFaces))
End Sub

Sub FindFaces(bmp As Bitmap) As Int
   Dim destRect As Rect
   destRect.Initialize(0, 0, mbmp.Width, mbmp.Height)
   cvs.DrawBitmap(bmp, Null, destRect)
   Return faceDetector.RunMethod("findFaces", Array As Object(mbmp, faces))
End Sub
 
Upvote 0

JordiCP

Expert
Licensed User
Longtime User
Thank you Erel for your time!

I like the way it is programmed with JavaObjects

My question was about the android.hardware.Camera.FaceDetectionListener interface, but, to be honest, this approach is better since it is more general as it does not depend on the API version.:)

Thanks again
 
Upvote 0

frankinator

Member
Licensed User
Longtime User
I would love to see more documentation on this topic (face and object recognition).
I have some great ideas for using cheap tablets and android sticks. I looked for more posts and surprisingly it seems to be a fairly limited topic so far.

Cool stuff, thanks for this one!
 
Upvote 0
Top