title: Xamarin.Forms(Android) Face Detecter
body: <p>I want to do face detection in xamarin.forms,</p>
  
  <p>I found the necessary codes but not xamarin.forms.</p>
  
  <p>these codes are for xamarin.android only.</p>
  
  <p>I want to set these codes in xamarin.forms.</p>
  
  <p>How to do this with dependency service?</p>
  
  <p>Can anyone with this information help me?</p>
  
  <p>My Code</p>
  
  <pre><code>public class MainActivity : Activity
  {
      //Intent code for camera activity
      private static int TAKE_PICTURE_CODE = 100;
  
      //Max Faces to detect in a picture
      private static int MAX_FACES = 5;
  
      //Bitmap of a picture taken
      private Bitmap cameraBitmap = null;
  
      protected override void OnCreate (Bundle bundle)
      {
          base.OnCreate (bundle);
  
          // Set our view from the "main" layout resource
          SetContentView (Resource.Layout.Main);
  
          Button take_picture = FindViewById&lt;Button&gt; (Resource.Id.take_picture);
  
          take_picture.Click  += take_picture_Clicked;
  
      }
  
      void take_picture_Clicked (object sender, EventArgs args )
      {
          //call OpenCamera() Event
          openCamera();
      }
  
  
  
      //OnActivityResult
      protected override void OnActivityResult (int requestCode, Result resultCode, Intent data)
      {
          base.OnActivityResult (requestCode, resultCode, data);
          //check if method return takepicturecode
          if(TAKE_PICTURE_CODE == requestCode){
              //Call a method to process image data
              processCameraImage(data);
          }
      }
      /// Open an activity for take a picture
      private void openCamera()
      {
          Intent intent = new Intent (Android.Provider.MediaStore.ActionImageCapture);
          StartActivityForResult (intent, TAKE_PICTURE_CODE);
      }
  
      /// &lt;summary&gt;
      /// Process picture taken an change UI to detect face
      /// &lt;/summary&gt;
      /// &lt;param name="intent"&gt;Intent.&lt;/param&gt;
      private void processCameraImage(Intent intent)
      {
          //Change layout to main Layout
          SetContentView(Resource.Layout.detectlayout);
  
          Button detect_face = FindViewById&lt;Button&gt; (Resource.Id.detect_face);
  
          detect_face.Click += detect_face_Clicked;
  
          ImageView image_view = FindViewById&lt;ImageView&gt; (Resource.Id.image_view);
  
          //Set image 
          image_view.SetImageBitmap(cameraBitmap);
      }
  
      void detect_face_Clicked (object sender, EventArgs args )
      {
          //Detect Face
          detectFaces();
      }
  </code></pre>
  
  <p>detect faces on a picture and draw a square in each face</p>
  
  <pre><code>private void detectFaces(){  
  //first check if picture has been taken  
  if(null != cameraBitmap){  
      //get width of a picture  
      int width = cameraBitmap.Width;  
      //get height of a picture  
      int height = cameraBitmap.Height;  
      //Initialize a facedetector with the picture dimensions and the max number of faces to check  
      FaceDetector detector = new FaceDetector(width, height, MainActivity.MAX_FACES);  
      //Create an array of faces with the number of max faces to check  
      FaceDetector.Face[] faces = new FaceDetector.Face[MainActivity.MAX_FACES];  
  
      //create a main bitmap  
      Bitmap bitmap565 = Bitmap.CreateBitmap(width, height, Bitmap.Config.Rgb565);  
      //create a dither paint  
      Paint ditherPaint = new Paint();  
      //create a draw paint  
      Paint drawPaint = new Paint();  
  
      //set true dither to dither paint variable  
      ditherPaint.Dither = true;  
      //set color red for the square  
      drawPaint.Color = Color.Green;  
      //set stroke to style  
      drawPaint.SetStyle(Paint.Style.Stroke);  
      //set stroke width  
      drawPaint.StrokeWidth = 2;  
  
      //Create a canvas  
      Canvas canvas = new Canvas();  
      //set bitmap to canvas  
      canvas.SetBitmap(bitmap565);  
      //draw bitmap to canvas  
      canvas.DrawBitmap(cameraBitmap, 0, 0, ditherPaint);  
  
      //get a number of faces detected  
      int facesFound = detector.FindFaces(bitmap565, faces);  
      //mid face point  
      PointF midPoint = new PointF();  
      //eye distance variable  
      float eyeDistance = 0.0f;  
      //confidence variable  
      float confidence = 0.0f;  
      //print numbre of faces found  
      System.Console.WriteLine ("Number of faces found: " + facesFound);  
  
      //check if found at least one face  
      if(facesFound &gt; 0)  
      {  
          //for each face draw a red squeare  
          for(int index=0; index&lt;facesFound; ++index){  
              // get midpoint of a face  
              faces[index].GetMidPoint(midPoint);  
              //get eye distance  
              eyeDistance = faces[index].EyesDistance();  
              //get confidence  
              confidence = faces [index].Confidence ();  
              //print all parameters  
              System.Console.WriteLine ("Confidence: " + confidence +   
                  ", Eye distance: " + eyeDistance +   
                  ", Mid Point: (" + midPoint.X + ", " + midPoint.Y + ")");  
              //draw a square in the picture  
              canvas.DrawRect((int)midPoint.X - eyeDistance ,   
                  (int)midPoint.Y- eyeDistance ,   
                  (int)midPoint.X + eyeDistance,   
                  (int)midPoint.Y + eyeDistance, drawPaint);  
          }  
      }  
  
      //get imageview from layout  
      ImageView imageView = (ImageView)FindViewById(Resource.Id.image_view);  
      //set image with the red squares to imageview  
      imageView.SetImageBitmap(bitmap565);  
  }  
  </code></pre>
  
  <p>}   </p>