Firstly, we can create our main layout having a button to take picture using camera and name it the following:
Main.axml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="match_parent"
- android:padding="10dip">
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="Click Take a Picture button"
- android:gravity="center_horizontal"
- android:layout_weight="1.0" />
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:id="@+id/take_picture"
- android:layout_margin="5dip"
- android:text="Take Picture"
- android:layout_gravity="center_horizontal" />
- </LinearLayout>
Now we need a layout named
detectlayout.axml having a ImageView to display the picture which we had captured using camera and a Button to detect faces in that Image.
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <ImageView
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:id="@+id/image_view"
- android:layout_weight="1.0"
- android:src="@android:drawable/ic_menu_report_image" />
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:id="@+id/detect_face"
- android:text="Detect Face"
- android:layout_gravity="center_horizontal" />
- </LinearLayout>
Now let's start coding.
Firstly, we want to open an activity to take picture.
- private void openCamera()
- {
- Intent intent = new Intent (Android.Provider.MediaStore.ActionImageCapture);
- StartActivityForResult (intent, TAKE_PICTURE_CODE);
- }
Now we want to Process the captured picture and display it on the
detectlayout.axml.
- private void processCameraImage(Intent intent)
- {
-
- SetContentView(Resource.Layout.detectlayout);
-
- Button detect_face = FindViewById<Button> (Resource.Id.detect_face);
-
- detect_face.Click += detect_face_Clicked;
-
- ImageView image_view = FindViewById<ImageView> (Resource.Id.image_view);
-
-
- image_view.SetImageBitmap(cameraBitmap);
- }
Add our main ingredient, here we detecting the faces on the picture and drawing a square to mark those faces.
- private void detectFaces(){
-
- if(null != cameraBitmap){
-
- int width = cameraBitmap.Width;
-
- int height = cameraBitmap.Height;
-
- FaceDetector detector = new FaceDetector(width, height, MainActivity.MAX_FACES);
-
- FaceDetector.Face[] faces = new FaceDetector.Face[MainActivity.MAX_FACES];
-
-
- Bitmap bitmap565 = Bitmap.CreateBitmap(width, height, Bitmap.Config.Rgb565);
-
- Paint ditherPaint = new Paint();
-
- Paint drawPaint = new Paint();
-
-
- ditherPaint.Dither = true;
-
- drawPaint.Color = Color.Green;
-
- drawPaint.SetStyle(Paint.Style.Stroke);
-
- drawPaint.StrokeWidth = 2;
-
-
- Canvas canvas = new Canvas();
-
- canvas.SetBitmap(bitmap565);
-
- canvas.DrawBitmap(cameraBitmap, 0, 0, ditherPaint);
-
-
- int facesFound = detector.FindFaces(bitmap565, faces);
-
- PointF midPoint = new PointF();
-
- float eyeDistance = 0.0f;
-
- float confidence = 0.0f;
-
- System.Console.WriteLine ("Number of faces found: " + facesFound);
-
-
- if(facesFound > 0)
- {
-
- for(int index=0; index<facesFound; ++index){
-
- faces[index].GetMidPoint(midPoint);
-
- eyeDistance = faces[index].EyesDistance();
-
- confidence = faces [index].Confidence ();
-
- System.Console.WriteLine ("Confidence: " + confidence +
- ", Eye distance: " + eyeDistance +
- ", Mid Point: (" + midPoint.X + ", " + midPoint.Y + ")");
-
- canvas.DrawRect((int)midPoint.X - eyeDistance ,
- (int)midPoint.Y- eyeDistance ,
- (int)midPoint.X + eyeDistance,
- (int)midPoint.Y + eyeDistance, drawPaint);
- }
- }
-
-
- ImageView imageView = (ImageView)FindViewById(Resource.Id.image_view);
-
- imageView.SetImageBitmap(bitmap565);
- }
- }
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.