Android Camera开发:sdk 14+ 新特新 人脸识别

Stella981
• 阅读 654

必要条件

Camera 人脸检测 需要 android sdk 14+,当开启人脸识别特性时, android.hardware.Camera 类的setWhiteBalance(String)[白平衡], setFocusAreas(List)[一个或多个对焦区域]和setMeteringAreas(List)[ 指定一个或多个区域来计算白平衡 ]不再起作用.

当android.hardware.Camera.Parameters.getMaxNumDetectedFaces() 返回值大于0, 表示硬件支持人脸识别

实现FaceDetectionListener接口 

关键是重载onFaceDetection(Face[] faces,Camera camera)方法。顾名思义,onFaceDetection方法就是对识别结果的回调

/**
         * 识别结果的回调 *
         * @param faces 检测到的人脸人脸集合
         * @param camera  照相机实例
         */
        void onFaceDetection(Face[] faces, Camera camera);

例如:

public class SimpleFaceDetect implements FaceDetectionListener {
    private static final String TAG = "FACE";
    private Context mContext;
    private Handler mHander;
    public SimpleFaceDetect(Context context, Handler handler){
        this.mContext = context;
        mHander = handler;
    }
    
    @Override
    public void onFaceDetection(Face[] faces, Camera camera) {
                        //检测结果集合大小为0,表示未检测到人脸
            Log.v(TAG, "人脸数量:"+faces.length);
                        //发送结果消息
            Message m = mHander.obtainMessage();
            m.what = EventUtil.MSG_FACE;
            m.obj = faces;
            m.sendToTarget();

    }

}

开启人脸检测

/**
     * 开启人脸检测
     * @param faceDetectionListener 检测结果回调
     * @return 是否开启成功
     */
    public boolean startFaceDetection(FaceDetectionListener faceDetectionListener){
        boolean bool=false;
        if(mParams.getMaxNumDetectedFaces() > 0){            
            
            mCamera.setFaceDetectionListener(faceDetectionListener);
            /**
             * 当启用面部检测特性时,setWhiteBalance(String), setFocusAreas(List) 和setMeteringAreas(List)不再起作用.
             */
            mCamera.startFaceDetection();
            
            bool=true;
        }
        
        return bool;
    }

调用此方法应注意时机,有的人喜欢在Activity的onCreate方法调用导致人脸检测开启不了,其实google在API 方法注释里已经对如何在恰当的时候开启检测进行了详细说明,具体实现方法是native的,有兴趣可以深入了解下

void android.hardware.Camera.startFaceDetection()


Starts the face detection. This should be called after preview is started. The camera will notify FaceDetectionListener of the detected faces in the preview frame. The detected faces may be the same as the previous ones. Applications should call stopFaceDetection to stop the face detection. This method is supported if Parameters.getMaxNumDetectedFaces() returns a number larger than 0. If the face detection has started, apps should not call this again. 

When the face detection is running, Parameters.setWhiteBalance(String), Parameters.setFocusAreas(List), and Parameters.setMeteringAreas(List) have no effect. The camera uses the detected faces to do auto-white balance, auto exposure, and autofocus. 

If the apps call autoFocus(AutoFocusCallback), the camera will stop sending face callbacks. The last face callback indicates the areas used to do autofocus. After focus completes, face detection will resume sending face callbacks. If the apps call cancelAutoFocus(), the face callbacks will also resume.

After calling takePicture(Camera.ShutterCallback, Camera.PictureCallback, Camera.PictureCallback) or stopPreview(), and then resuming preview with startPreview(), the apps should call this method again to resume face detection.

简单来说就是要在 每次相机 预览开始以后才能调用:

/**
     * 开启预览
     * @param holder
     * @param previewRate
     */
    public void startPreview(SurfaceHolder holder, float previewRate){
    ...相机参数设置略...
    handler.sendEmptyMessage(MSG_STARTED_PREVIEW);//发送消息通知开启人脸检测
        }
    }

关闭人脸检测

关闭预览时关闭

/**
     * 关闭人脸检测
     */
    public boolean stopFaceDetection( ){
        boolean bool=false;
        if(mParams.getMaxNumDetectedFaces() > 0){
            mCamera.setFaceDetectionListener(null);
            mCamera.stopFaceDetection();
            bool=true;
        }
        
        return bool;
    }

在预览界面绘制人脸位置框

布局文件中增加

<com.longyuan.camerademo.ui.FaceView
            android:id="@+id/face_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

代码:

public class FaceView extends ImageView {
    private static final String TAG = "FACE";
    private Context mContext;
    private Face[] mFaces;
    private Matrix mMatrix = new Matrix();
    private RectF mRect = new RectF();
    private Drawable mFaceIndicator = null;
    public FaceView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        mFaceIndicator = getResources().getDrawable(R.drawable.ic_face_find_1);
    }


    public void setFaces(Face[] faces){
        this.mFaces = faces;
        invalidate();
    }
    public void clearFaces(){
        mFaces = null;
        invalidate();
    }
    

    @Override
    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, getWidth(), getHeight());
        canvas.save();
        mMatrix.postRotate(0); //Matrix.postRotate默认是顺时针
        canvas.rotate(-0);   //Canvas.rotate()默认是逆时针 
        for(int i = 0; i< mFaces.length; i++){
            mRect.set(mFaces[i].rect);
            mMatrix.mapRect(mRect);
            mFaceIndicator.setBounds(Math.round(mRect.left), Math.round(mRect.top),
                    Math.round(mRect.right), Math.round(mRect.bottom));
            mFaceIndicator.draw(canvas);  }
        canvas.restore();
        super.onDraw(canvas);
    }

}

在UI线程更新界面

case MSG_FACE:
                    final Face[] faces = (Face[]) msg.obj;    
                    faceView.setFaces(faces);    break;

No picture say J8.

Android  Camera开发:sdk 14+ 新特新 人脸识别

再来一张

Android  Camera开发:sdk 14+ 新特新 人脸识别

Android  Camera开发:sdk 14+ 新特新 人脸识别

测试机 : 中兴 天机 S291

已知华为荣耀X1不支持

结果还可以哈 闪~

资料:

各版API中引入的常用相机特性表

Feature

API Level

Description

Face Detection

14

检测人脸位置并用结果来计算焦点,测光和白平衡

Metering Areas

14

在一个图像内指定一个或多个区域来计算白平衡

Focus Areas

14

在一个图像中设置一个或多个聚焦区域

White Balance Lock

14

停止或开始自动白平衡调整

Exposure Lock

14

停止或开始自动曝光调整

Video Snapshot

14

在拍视频时抓取一个图像

Time Lapse Video

11

定时录像

Multiple Cameras

9

在一个设备上支持一个或多个相机,包括正面和反面的相机。

Focus Distance

9

焦距

Zoom

8

设置图像的放大率

Exposure Compensation

8

减小或增大曝光级别

GPS Data

5

图像中包含或不包含地理位置信息

White Balance

5

设置白平衡的模式

Focus Mode

5

设置在一个物体上如何聚焦,比如自动,固定,微距,无限远.

Scene Mode

5

场景模式,比如晚上,海滩,雪地或烛光.

JPEG Quality

5

设置JPEG图像的压缩级别.

Flash Mode

5

设置闪光灯开,关或自动.

Color Effects

5

应用一个颜色效果到图像,比如黑白,褐色,反色.

Anti-Banding

5

减少在JPEG压缩时的颜色渐变的边缘效应.

Picture Format

1

指定图像的文件格式

Picture Size

1

指定图像的宽和高

资料:

http://blog.csdn.net/qq69696698/article/details/7325772

点赞
收藏
评论区
推荐文章
blmius blmius
2年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
不是海碗 不是海碗
1年前
人脸识别之人脸检测的重要性
在整个人脸识别的整个工程当中,必然是少不了人脸检测的,它承担着很重要的职责。首先摄像头在捕捉到的图像中,需要用人脸检测技术,检测这张图片当中是否有人脸,检测到人脸以及人脸的位置之后,才进行后续的特征提取、特征对比等步骤,最后才形成一个完整的人脸识别过程。
不是海碗 不是海碗
1年前
景区如何限流?竟然可以用人脸检测做到
我们可以通过人脸检测去进行景区限流。在景区门口放置摄像头,摄像头捕捉到游客的人脸图像,然后使用人脸检测技术,识别出图像中是否含有人脸,含有几张人脸,检测一张人脸,就在计数器上1。这样景区就可以通过客流量的统计,当达到最大客流量的时候,就停止进入,实现景区限流。
不是海碗 不是海碗
1年前
APISpace的 人脸检测API 它来啦~
人脸检测是指通过计算机视觉技术,从图像中识别、检测出人脸,并确定人脸的位置及大小。它是一种计算机图像处理技术,是计算机视觉领域的关键技术,可用于实现自动识别和跟踪人脸。
不是海碗 不是海碗
1年前
人脸检测之身份识别你需要的那些事
人脸检测是进行身份识别的一个重要环节,因为它可以准确地识别出图像中的人脸,这样才能保证身份识别的准确性。
不是海碗 不是海碗
1年前
人脸检测:在公共交通场所监控中起什么样的作用?
在公共交通场所的监控系统中,人脸检测起着至关重要的作用。它被用来识别人脸,并检测未识别的人脸是否是真实的人脸。
Stella981 Stella981
2年前
Android So动态加载 优雅实现与原理分析
背景:漫品Android客户端集成适配转换功能(基于目标识别(So库35M)和人脸识别库(5M)),导致apk体积50M左右,为优化客户端体验,决定实现So文件动态加载.!(https://oscimg.oschina.net/oscnet/00d1ff90e4b34869664fef59e3ec3fdd20b.png)点击上方“蓝字”关注我
Stella981 Stella981
2年前
C#实现基于ffmpeg加虹软的人脸识别
关于人脸识别目前的人脸识别已经相对成熟,有各种收费免费的商业方案和开源方案,其中OpenCV很早就支持了人脸识别,在我选择人脸识别开发库时,也横向对比了三种库,包括在线识别的百度、开源的OpenCV和商业库虹软(中小型规模免费)。百度的人脸识别,才上线不久,文档不太完善,之前联系百度,官方也给了我基于Android的Example,但是不太符合我
Stella981 Stella981
2年前
Django中Admin中的一些参数配置
设置在列表中显示的字段,id为django模型默认的主键list_display('id','name','sex','profession','email','qq','phone','status','create_time')设置在列表可编辑字段list_editable
稚然 稚然
3个月前
千锋修海_人工智能OpenCV人脸识别开发
//下仔のke:https://yeziit.cn/14502/人脸识别是基于人的脸部特征信息进行身份识别的一种生物识别技术。通过摄像机或摄像头采集含有人脸的图像或视频流,并自动在图像中检测和跟踪人脸,进而对检测到的人脸进行脸部识别的一系列相关技术,通常也