1. 相关背景
Google 于2006年8月收购Neven Vision 公司 (该公司拥有 10 多项应用于移动设备领域的图像识别的专利),以此获得了图像识别的技术,并很快应用到免费的 Picasa 相册管理程序中,提供基于人脸识别的相片管理功能,另外还推出了一个新项目叫Goggle ,能从照片中识别世界各地的地标建筑,同样Google 也把人脸识别功能添加到了Android 中。不过由于个人隐私等相关因素,Google Goggles好像暂时屏蔽了人脸识别功能 。
2. Android 中的人脸识别技术
底层库:android/external/neven/
framework 层:frameworks/base/media/java/android/media/FaceDetector.java
Java 层接口的限制:
3. 人脸识别技术的应用
A. 为 Camera 添加人脸识别的功能:使得 Camera 的取景器上能标识出人脸范围;如果硬件支持,可以对人脸进行对焦。
B. 为相册程序添加按人脸索引相册的功能:按人脸索引相册,按人脸分组,搜索相册。
4.Neven库给上层提供的主要方法:
Since: API Level 1
Creates a FaceDetector, configured with the size of the images to be analysed and the maximum number of faces that can be detected. These parameters cannot be changed once the object is constructed.
width
the width of the image
height
the height of the image
maxFaces
the maximum number of faces to identify
Since: API Level 1
Finds all the faces found in a given Bitmap
. The supplied array is populated with FaceDetector.Face
s for each face found. The bitmap must be in 565 format (for now).
bitmap
the Bitmap
graphic to be analyzed
faces
an array in which to place all found FaceDetector.Face
s. The array must be sized equal to the maxFaces value set at initialization
the number of faces found
IllegalArgumentException
if the Bitmap dimensions don't match the dimensions defined at initialization or the given array is not sized equal to the maxFaces value defined at initialization
代码
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]); // 调用函数对人脸画面进行处理
}
}
}
}
}
源地址:http://blog.renren.com/GetEntry.do?id=723400977&owner=230434842