
- <?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>
- <?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>
- 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)
- {
- //Change layout to main Layout
- 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);
- //Set image
- 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(){
- //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 > 0)
- {
- //for each face draw a red squeare
- for(int index=0; index<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);
- }
- }

Mark AndersonPosted Aug 13, 2021, 12:13 PM
Will this also compile to run on ios?
muthu karunarathnaPosted Jul 25, 2017, 6:09 AM
This is not working because camerabitmap is null always. So you need to set it as follow. protected override void OnActivityResult (int requestCode, Result resultCode, Intent data) { base.OnActivityResult (requestCode, resultCode, data); //check if method return takepicturecode Bitmap photo = (Bitmap)data.Extras.Get("data"); cameraBitmap = photo; if (TAKE_PICTURE_CODE == requestCode){ //Call a method to process image data processCameraImage(photo); } } Also you need to change the parameter in processCameraImage from intent to bitmap private void processCameraImage(Bitmap intent) { //Change layout to main Layout 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); //Set image image_view.SetImageBitmap(intent); }
Arvind ChourasiyaPosted Jun 2, 2016, 10:08 AM
Hello Vaikesh in second screen camera not opening.only button showing detect face after clicking that button nothing happening.It should open camera in second screen also or not?
Arthur AricayaPosted Mar 5, 2016, 10:53 PM
thank for this..but when i clicked detect faces nothing happens...
Ankur MistryPosted Nov 29, 2015, 4:42 AM
Very nice article
Banketeshvar NarayanPosted Nov 18, 2015, 4:27 AM
Nice Share
Praveen KumarPosted Nov 17, 2015, 9:31 AM
Very Nice
Sibeesh VenuPosted Nov 17, 2015, 3:59 AM
Nice Share
Ankit BansalPosted Nov 17, 2015, 12:45 AM
nice one..
Humayun Kabir MamunPosted Nov 17, 2015, 12:13 AM
Nice...
Vaikesh K PPosted Nov 16, 2015, 4:21 AM
Thanks Santhakumar Munuswamy
Santhakumar MunuswamyPosted Nov 16, 2015, 3:59 AM
Good one