Android 自带的人脸识别

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]); // _ _ 调用函数对人脸画面进行处理 _ __

_ } _

_ } _

_ } _

_ } _

_ } _

6. 完整例程下载
[http://download.csdn.net/source/2612476

](http://download.csdn.net/source/2612476)