1.Android自带的人脸识别Android自带的人脸识别只能识别出人脸在画面中的位置,中点,眼间距,角度等基本特性,提供给拍照性质的应用使用。从基本功能中不能得出明显的特征数据
2.底层库支持external/neven/*
3.接口frameworks/base/media/java/android/media/FaceDetector.java
4.Android源码参考packages/apps/Gallery/src/com/android/camera/*
5.关键代码分析
package com.android.mydetect;
import android.app.Activity;
import android.widget.TextView;
import android.os.Bundle;
import android.media.FaceDetector;//人脸识别接口
import android.widget.ImageView;
import android.graphics.BitmapFactory;
import android.graphics.Bitmap;
import android.graphics.PointF;
import android.graphics.Matrix;
import android.util.Log;
import android.graphics.Canvas;
import android.graphics.Paint;
public class MyDetectActivity extends Activity {
private ImageView mImageView;//图片显示控件
private Bitmap mBitmap;
private float mScale = 1F;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mImageView = (ImageView) this.findViewById(R.id.image);
detect();//识别函数
}
private void handleFace(FaceDetector.Face f) {//在图片上对每张脸进行处理
PointF midPoint = new PointF();
int r = ((int) (f.eyesDistance() * mScale * 1.5));//取眼睛间距离
f.getMidPoint(midPoint);//取脸的中点
midPoint.x *= mScale;
midPoint.y *= mScale;
Canvas c = new Canvas(mBitmap);
Paint p = new Paint();
p.setAntiAlias(true);
p.setAlpha(0x80);
c.drawCircle(midPoint.x, midPoint.y, r, p)//用半透明标出人脸区域;
mImageView.setImageBitmap(mBitmap);//显示图片
}
private void detect() {
Matrix matrix = new Matrix();
FaceDetector.Face[] mFaces = new FaceDetector.Face[3];//定义最多识别三张脸
int mNumFaces = 0;
mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.baby);//取原始图
if (mBitmap == null) {
return;
}
if (mBitmap.getWidth() > 256) {
mScale = 256.0F / mBitmap.getWidth();
}
matrix.setScale(mScale, mScale);
Bitmap faceBitmap = Bitmap.createBitmap(mBitmap, 0, 0, mBitmap
.getWidth(), mBitmap.getHeight(), matrix, true);//生成缩放后的新图
mScale = 1.0F / mScale;
if (faceBitmap != null) {
FaceDetector detector = new FaceDetector(faceBitmap.getWidth(),
faceBitmap.getHeight(), mFaces.length);//创建识别器
mNumFaces = detector.findFaces(faceBitmap, mFaces);//识别
if (mNumFaces > 0) {
for (int i = 0; i < mNumFaces; i++) {
handleFace(mFaces[i]);//调用函数对人脸画面进行处理
}
}
}
}
}