I'm trying to develop face detection using google-mlkit. After setup camera, analyzer, detector etc following docs from google-mlkit https://developers.google.com/ml-kit/vision/face-detection/android, I'm able to detect face. After detect face I'm trying to extract detected face image.
Get detected face boundingBox, extract full bitmap into detected face bitmap using boundingBox. Code below.
Bitmap bmp = Bitmap.createBitmap(mediaImage.getWidth(), mediaImage.getHeight(), Bitmap.Config.ARGB_8888);
YuvToRgbConverter converter = new YuvToRgbConverter(context);
converter.yuvToRgb(mediaImage, bmp);
Bitmap extractedBmp = extractFace(
bmp,
(int) (face.getBoundingBox().exactCenterX() / 2),
(int) (face.getBoundingBox().exactCenterY() / 2),
face.getBoundingBox().width(),
face.getBoundingBox().height()
);
private Bitmap extractFace(Bitmap bmp, int x, int y, int width, int height) {
return Bitmap.createBitmap(bmp, x, y, width, height);
}
Sometimes It's success and sometimes also failed. After debugging I found it's error because of x + width must be <= bitmap.width()
.
What I'm doing wrong?