android 人脸识别demo,Android Camera 内置人脸识别的Demo

CameraFace

Android Camera 内置人脸识别的Demo

通过Android源生API支持的人脸识别FaceDetection,获取到脸部矩形坐标,左右眼坐标,嘴坐标通过View动态实摄像头实时装饰功能

687474703a2f2f377873376e392e636f6d312e7a302e676c622e636c6f7564646e2e636f6d2f66616365322e706e67

主要方法

//人脸识别设置监听

mCamera.setFaceDetectionListener(mFaceDetectionListener);

//人脸识别监听启动

mCamera.startFaceDetection();

class FaceDetectionListener implements Camera.FaceDetectionListener{

@Override

public void onFaceDetection(Camera.Face[] faces, Camera camera) {

Message m = mHandler.obtainMessage();

m.what = MainActivity.AC_DRAW;

if (null == faces || faces.length == 0) {

m.obj = null;

Log.d("face", "onFaceDetection : There is no face found.");

} else {

Log.d("face", "onFaceDetection : face found.");

m.obj = faces;

for (int i = 0; i < faces.length; i++) {

Camera.Face face = faces[i];

if (face == null)return;

Rect rect = face.rect;

Log.i("face","face.score="+face.score);

Log.i("face","rect.left="+rect.left+"\nrect.top="+rect.top+"\nrect.right="+rect.right+"\nrect.bottom="+rect.bottom);

Log.i("face","id="+face.id+" \nface.leftEye.x="+face.leftEye.x+" \nface.leftEye.y"+face.leftEye.y+" \nface.mouth.x="+face.mouth.x+" \nface.mouth.y="+face.mouth.y);

}

}

m.sendToTarget();

}

}

//faces 识别出的人脸特征数据

public static class Face {

public int id;//对应id

public Point leftEye;//左眼

public Point mouth;//嘴

public Rect rect;//脸部坐标

public Point rightEye;//右眼

public int score;//识别分数

}

//绘制对应的装饰图片

protected void onDraw(Canvas canvas) {

if (mFaces == null || mFaces.length < 1) {

return;

}

boolean isMirror = false;

// int Id =CameraInterface.getInstance().getCameraId();

// if(Id == CameraInfo.CAMERA_FACING_BACK){

// isMirror = false; //后置Camera无需mirror

// }else if(Id == CameraInfo.CAMERA_FACING_FRONT){

isMirror = true; //前置Camera需要mirror

// }

// Util.prepareMatrix(mMatrix, isMirror, 90, mCameraW, mCameraH);

Util.prepareMatrix(mMatrix, isMirror, 90, getWidth(), getHeight());

canvas.save();

mMatrix.postRotate(0); //Matrix.postRotate默认是顺时针

canvas.rotate(-0); //Canvas.rotate()默认是逆时针

if (mFaces == null){

mFaceIndicatorDraw.draw(canvas);

mLeftEyeDraw.draw(canvas);

mRightEyeDraw.draw(canvas);

mMouthDraw.draw(canvas);

}else {

for (int i = 0; i < mFaces.length; i++) {

Camera.Face face = mFaces[i];

mRect.set(face.rect);

mMatrix.mapRect(mRect);//计算出在父布局的真是坐标

//识别的Rect 系数,使装饰图片根据人脸与摄像头的距离放大或者缩小

float dx = mRect.bottom - mRect.top;

if (isHeadShow){

Log.i(TAG, "FaceView dx=" +dx );

float h = dx * 0.4f;

mHeadDraw.setBounds(Math.round(mRect.left), Math.round(mRect.top - h),

Math.round(mRect.right), Math.round(mRect.bottom - h));

mHeadDraw.draw(canvas);

}

if (isFaceIndicatorShow){

mFaceIndicatorDraw.setBounds(Math.round(mRect.left), Math.round(mRect.top),

Math.round(mRect.right), Math.round(mRect.bottom));

mFaceIndicatorDraw.draw(canvas);

}

if (isLeftEyeShow){

float[] leftEye = {face.leftEye.x, face.leftEye.y};

mMatrix.mapPoints(leftEye);//计算出在父布局的真是坐标

float h = dx * 0.1f;

float w = dx * 0.15f;

mLeftEyeDraw.setBounds(Math.round(leftEye[0]-w), Math.round(leftEye[1]-h),

Math.round(leftEye[0]+w), Math.round(leftEye[1]+h));

mLeftEyeDraw.draw(canvas);

}

if (isRightEyeShow){

float[] rightEye = {face.rightEye.x, face.rightEye.y};

float w = dx * 0.15f;

float h = dx * 0.1f;

mMatrix.mapPoints(rightEye);//计算出在父布局的真是坐标

mRightEyeDraw.setBounds(Math.round(rightEye[0]-w), Math.round(rightEye[1]-h),

Math.round(rightEye[0]+w), Math.round(rightEye[1]+h));

mRightEyeDraw.draw(canvas);

}

if (isMouthShow){

float[] mouthP = {face.mouth.x, face.mouth.y};

mMatrix.mapPoints(mouthP);//计算出在父布局的真是坐标

float w = dx * 0.15f;

float h = dx * 0.2f;

mMouthDraw.setBounds(Math.round(mouthP[0]-w), Math.round(mouthP[1]-h),

Math.round(mouthP[0]+w), Math.round(mouthP[1]));

mMouthDraw.draw(canvas);

}

}

}

canvas.restore();

super.onDraw(canvas);

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值