Giter Site home page Giter Site logo

z56402344 / cameraface Goto Github PK

View Code? Open in Web Editor NEW
13.0 2.0 5.0 1.89 MB

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

Java 100.00%

cameraface's Introduction

CameraFace

Android Camera 内置人脸识别的Demo

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

Image

主要方法

//人脸识别设置监听

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);
    }

cameraface's People

Contributors

z56402344 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

cameraface's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.