0

Im using one of api face tracker sample and i modified code a bit to see if it will work according to my idea for app. what i want to do is i want to add face masks after detecting face.

What i've done so far, i've added a sample mask drawable and when i detect a face instead of drawing face points i draw drawable in face tracking rectangle. now its showing that mask in preview on the face but when i try to capture that image it only capture frame from camera not with graphic overlay i added mask on. is there any way i can capture from camera with that mask on it ?

Image being saved

image being displayed on mobile screen

Usman Ghauri
  • 931
  • 1
  • 7
  • 26

1 Answers1

1

I have achieved something similar to this in a project i worked on in the past but no longer have access to the project.

when you call your capture method you need to store a reference to the position of the face.

Im not sure how much control the vision api gives you over the camera so you either:

take the picture and before saving the file add the mask resource on top of the returned bitmap.

or

load the saved file add the mask resource on top of the it.

I will have a look around later for some code if it will help.

Edit Rotate Bitmap

bitmap = android.provider.MediaStore.Images.Media
                .getBitmap(cr, selectedImage);
ExifInterface exif = new ExifInterface("/storage/emulated/0/Pic.jpg");     
String exifOrientation = exif.getAttribute(ExifInterface.TAG_ORIENTATION);
int i = Integer.parseInt(exifOrientation);
bitmap = rotateImage(bitmap, i);

//

private Bitmap rotateImage(Bitmap bm, int i) {
    Matrix matrix = new Matrix();
    switch (i) {
        case ExifInterface.ORIENTATION_NORMAL:
            return bm;
        case ExifInterface.ORIENTATION_FLIP_HORIZONTAL:
            matrix.setScale(-1, 1);
            break;
        case ExifInterface.ORIENTATION_ROTATE_180:
            matrix.setRotate(180);
            break;
        case ExifInterface.ORIENTATION_FLIP_VERTICAL:
            matrix.setRotate(180);
            matrix.postScale(-1, 1);
            break;
        case ExifInterface.ORIENTATION_TRANSPOSE:
            matrix.setRotate(90);
            matrix.postScale(-1, 1);
            break;
        case ExifInterface.ORIENTATION_ROTATE_90:
            matrix.setRotate(90);
            break;
        case ExifInterface.ORIENTATION_TRANSVERSE:
            matrix.setRotate(-90);
            matrix.postScale(-1, 1);
            break;
        case ExifInterface.ORIENTATION_ROTATE_270:
            matrix.setRotate(-90);
            break;
        default:
            return bm;
    }
    try {
        Bitmap bmRotated = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), matrix, true);
        bm.recycle();
        return bmRotated;
    } catch (OutOfMemoryError e) {
        e.printStackTrace();
        return null;
    }
}
Lonergan6275
  • 1,938
  • 6
  • 32
  • 63
  • Thanks and yes i did something similar already. but there were many problems while doing so. some devices camera give rotated or even inverted image when i captured. – Usman Ghauri Jul 11 '16 at 08:57
  • @UsmanGhauri I had the same issue re. the rotation of the image. I have added code that solved this on all devices i tested on. – Lonergan6275 Jul 11 '16 at 09:06
  • I tried with your code but it is giving black image as it is surface view. Do you have any other solution for this...? – vijaypalod Sep 28 '16 at 11:18
  • @mVJ are you getting any exception? the code above does not do anything with the image (apply it to a surface view) just fixes the orientation – Lonergan6275 Sep 28 '16 at 11:46
  • @drojokef as i mentioned in the answer i am no longer working on this project. – Lonergan6275 Jun 18 '17 at 11:22