android 人脸识别demo,Android Camera 内置人脸识别的Demo
CameraFace
Android Camera 內置人臉識別的Demo
通過Android源生API支持的人臉識別FaceDetection,獲取到臉部矩形坐標,左右眼坐標,嘴坐標通過View動態實攝像頭實時裝飾功能
主要方法
//人臉識別設置監聽
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);
}
總結
以上是生活随笔為你收集整理的android 人脸识别demo,Android Camera 内置人脸识别的Demo的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android四大组件之广播接收器Bro
- 下一篇: 记录学习Android基础的心得00