Search code examples
androidgoogle-apigoogle-vision

Google Vision API Capture image with graphic overlay


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


Solution

  • 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;
        }
    }