安卓人脸识别
final int MAX_FACE = 1;
private FaceDetector faceDetector;
private FaceDetector.Face[] faces;
private Paint rectPaint = new Paint();
rectPaint.setAntiAlias(true);
rectPaint.setStrokeWidth(8);
rectPaint.setStyle(Paint.Style.STROKE);
rectPaint.setColor(Color.GREEN);
Bitmap必须为RGB_565色彩格式!如果不是,可以用下面的代码先转一下:
Bitmap bmp565 = bitmap.copy(Bitmap.Config.RGB_565, false);
private void detectFace(Bitmap bitmap) {
Canvas canvas = new Canvas(bitmap);
int n = faceDetector.findFaces(bitmap, faces);
for (int i=0; i<n; i++) {
FaceDetector.Face f = faces[i];
PointF midPoint = new PointF();
float dis = f.eyesDistance();
f.getMidPoint(midPoint);
int dd = (int)(dis);
Point eyeLeft = new Point((int)(midPoint.x - dis/2), (int)midPoint.y);
Point eyeRight = new Point((int)(midPoint.x + dis/2), (int)midPoint.y);
faceRect = new Rect((int)(midPoint.x - dd), (int)(midPoint.y - dd), (int)(midPoint.x + dd), (int)(midPoint.y + dd));
canvas.drawRect(faceRect, rectPaint);
}
}