我正在尝试使用google-mlkit开发人脸检测。在按照google-mlkit https://developers.google.com/ml-kit/vision/face-detection/android的文档设置好摄像头、分析仪、检测器等之后,我就能够检测到人脸了。在检测到人脸之后,我尝试提取检测到的人脸图像。
获取检测到的人脸boundingBox,使用boundingBox将完整的位图提取到检测到的人脸位图中。下面的代码。
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);
}有时是成功的,有时也是失败的。经过调试,我发现这是因为x + width must be <= bitmap.width()导致的错误。我哪里做错了?
发布于 2020-11-14 04:27:08
您可以尝试mlkit示例here中提供的util方法来从YUV图像创建bimap吗?
https://stackoverflow.com/questions/64711144
复制相似问题