Face Detection
Firstly, we can create our main layout having a button to take a picture using camera and name it the following,
Main.axml
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="fill_parent"
  5. android:layout_height="match_parent"
  6. android:padding="10dip">
  7. <TextView
  8. android:layout_width="fill_parent"
  9. android:layout_height="wrap_content"
  10. android:text="Click Take a Picture button"
  11. android:gravity="center_horizontal"
  12. android:layout_weight="1.0" />
  13. <Button
  14. android:layout_width="wrap_content"
  15. android:layout_height="wrap_content"
  16. android:id="@+id/take_picture"
  17. android:layout_margin="5dip"
  18. android:text="Take Picture"
  19. android:layout_gravity="center_horizontal" />
  20. </LinearLayout>
Now we need a layout named detectlayout.axml having an ImageView to display the picture which we had captured using camera and a Button to detect faces in that Image.
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent">
  6. <ImageView
  7. android:layout_width="fill_parent"
  8. android:layout_height="fill_parent"
  9. android:id="@+id/image_view"
  10. android:layout_weight="1.0"
  11. android:src="@android:drawable/ic_menu_report_image" />
  12. <Button
  13. android:layout_width="wrap_content"
  14. android:layout_height="wrap_content"
  15. android:id="@+id/detect_face"
  16. android:text="Detect Face"
  17. android:layout_gravity="center_horizontal" />
  18. </LinearLayout>
Now let's start coding.
Firstly, we want to open an activity to take a picture.
  1. private void openCamera()
  2. {
  3. Intent intent = new Intent (Android.Provider.MediaStore.ActionImageCapture);
  4. StartActivityForResult (intent, TAKE_PICTURE_CODE);
  5. }
    Now we want to Process the captured picture and display it on the detectlayout.axml.
    1. private void processCameraImage(Intent intent)
    2. {
    3. //Change layout to main Layout
    4. SetContentView(Resource.Layout.detectlayout);
    5. Button detect_face = FindViewById<Button> (Resource.Id.detect_face);
    6. detect_face.Click += detect_face_Clicked;
    7. ImageView image_view = FindViewById<ImageView> (Resource.Id.image_view);
    8. //Set image
    9. image_view.SetImageBitmap(cameraBitmap);
    10. }
    Add our main ingredient, here we detecting the faces on the picture and drawing a square to mark those faces.
    1. private void detectFaces(){
    2. //first check if picture has been taken
    3. if(null != cameraBitmap){
    4. //get width of a picture
    5. int width = cameraBitmap.Width;
    6. //get height of a picture
    7. int height = cameraBitmap.Height;
    8. //Initialize a facedetector with the picture dimensions and the max number of faces to check
    9. FaceDetector detector = new FaceDetector(width, height, MainActivity.MAX_FACES);
    10. //Create an array of faces with the number of max faces to check
    11. FaceDetector.Face[] faces = new FaceDetector.Face[MainActivity.MAX_FACES];
    12. //create a main bitmap
    13. Bitmap bitmap565 = Bitmap.CreateBitmap(width, height, Bitmap.Config.Rgb565);
    14. //create a dither paint
    15. Paint ditherPaint = new Paint();
    16. //create a draw paint
    17. Paint drawPaint = new Paint();
    18. //set true dither to dither paint variable
    19. ditherPaint.Dither = true;
    20. //set color red for the square
    21. drawPaint.Color = Color.Green;
    22. //set stroke to style
    23. drawPaint.SetStyle(Paint.Style.Stroke);
    24. //set stroke width
    25. drawPaint.StrokeWidth = 2;
    26. //Create a canvas
    27. Canvas canvas = new Canvas();
    28. //set bitmap to canvas
    29. canvas.SetBitmap(bitmap565);
    30. //draw bitmap to canvas
    31. canvas.DrawBitmap(cameraBitmap, 0, 0, ditherPaint);
    32. //get a number of faces detected
    33. int facesFound = detector.FindFaces(bitmap565, faces);
    34. //mid face point
    35. PointF midPoint = new PointF();
    36. //eye distance variable
    37. float eyeDistance = 0.0f;
    38. //confidence variable
    39. float confidence = 0.0f;
    40. //print numbre of faces found
    41. System.Console.WriteLine ("Number of faces found: " + facesFound);
    42. //check if found at least one face
    43. if(facesFound > 0)
    44. {
    45. //for each face draw a red squeare
    46. for(int index=0; index<facesFound; ++index){
    47. // get midpoint of a face
    48. faces[index].GetMidPoint(midPoint);
    49. //get eye distance
    50. eyeDistance = faces[index].EyesDistance();
    51. //get confidence
    52. confidence = faces [index].Confidence ();
    53. //print all parameters
    54. System.Console.WriteLine ("Confidence: " + confidence +
    55. ", Eye distance: " + eyeDistance +
    56. ", Mid Point: (" + midPoint.X + ", " + midPoint.Y + ")");
    57. //draw a square in the picture
    58. canvas.DrawRect((int)midPoint.X - eyeDistance ,
    59. (int)midPoint.Y- eyeDistance ,
    60. (int)midPoint.X + eyeDistance,
    61. (int)midPoint.Y + eyeDistance, drawPaint);
    62. }
    63. }
    64. //get imageview from layout
    65. ImageView imageView = (ImageView)FindViewById(Resource.Id.image_view);
    66. //set image with the red squares to imageview
    67. imageView.SetImageBitmap(bitmap565);
    68. }
    69. }
      Now the stage is set for the show, only we need to do is just rearrange the code together in a logical way.
      Go through the attachment for the complete project.