Google mobile vision api - crop face as bitmap from camera

Viewed 167

I am trying to crop the detected face from camera after converting it to bitmap in onPictureTaken, but it doesn't crop correctly on the face area, how to crop face bitmap from camera?

enter image description here

mCameraSource.takePicture(null, new CameraSource.PictureCallback() {

  @Override
  public void onPictureTaken(byte[] bytes) {

    BitmapFactory.Options opt = new BitmapFactory.Options();
    opt.inDensity = DisplayMetrics.DENSITY_DEFAULT;
    opt.inTargetDensity = DisplayMetrics.DENSITY_DEFAULT;
    opt.inScaled = false;
    Bitmap bmp = BitmapFactory.decodeByteArray(bytes, 0, bytes.length, opt);

    Bitmap croppedFace = Bitmap.createBitmap(bmp, (int) facePosition.x, (int) facePosition.y, (int) faceWidth, (int) faceHeight);
    ivProfileImg.setImageBitmap(croppedFace);

  }
});

where facePosition is obtained from Tracker:

private class GraphicFaceTracker extends Tracker<Face> {
    private GraphicOverlay mOverlay;
    private FaceGraphic mFaceGraphic;

    GraphicFaceTracker(GraphicOverlay overlay) {
        mOverlay = overlay;
        mFaceGraphic = new FaceGraphic(overlay);
    }

    @Override
    public void onUpdate(FaceDetector.Detections<Face> detectionResults, Face face) {
        mOverlay.add(mFaceGraphic);
        mFaceGraphic.updateFace(face);

        //for cropping face
        facePosition = face.getPosition();
        faceWidth = face.getWidth();
        faceHeight = face.getHeight();

    }
}

Update: I managed to crop face by trial and error, added code to resize bmp into 1080 width, and scale facePosition values with 1.5, but I'm still not sure what is the proper code to replace these hard-coded values:

enter image description here

mCameraSource.takePicture(null, new CameraSource.PictureCallback() {

  @Override
  public void onPictureTaken(byte[] bytes) {

    BitmapFactory.Options opt = new BitmapFactory.Options();
    opt.inDensity = DisplayMetrics.DENSITY_DEFAULT;
    opt.inTargetDensity = DisplayMetrics.DENSITY_DEFAULT;
    opt.inScaled = false;
    Bitmap bmp = BitmapFactory.decodeByteArray(bytes, 0, bytes.length, opt);//2976, 3968

    //resized to 1080
    int resizedHeight = 1080*bmp.getHeight()/bmp.getWidth();
    bmp = Bitmap.createScaledBitmap(bmp,1080, resizedHeight, true);//1080, 1440

    //scale facePosition values to x1.5
    float scale = 1.5f;
    int screenX = (int)facePosition.x * scale;
    int screenY = (int)facePosition.y * scale;
    float fw = faceWidth * scale;
    float fh = faceHeight * scale;

    Bitmap croppedFace = Bitmap.createBitmap(bmp, screenX, screenY, (int) fw, (int) fh);
    ivProfileImg.setImageBitmap(croppedFace);

  }
});
0 Answers
Related