【原创】【Android Camera】—— 关于FaceDetection

本文是个人开发Camera APP中总结的问题之一,主要介绍FaceDetection的基本使用方法和注意问题。文中如有错误和不足,欢迎批评指正。
 
一、关于FaceDetection
    1、版本支持
        FaceDetection是Android ICE_CREAM_SANDWICH (Android Version 4.0,ADT 14)版开始增加的功能,实际是否支持还得依赖于设备自身的限制(Camera.Parameters.getMaxNumDetectedFaces() > 0)。
    2、FaceDetectionListener
        下面是FaceDetectionListener接口的源码:
    /**
     * Callback interface for face detected in the preview frame.
     *
     */
    public interface FaceDetectionListener
    {
        /**
         * Notify the listener of the detected faces in the preview frame.
         *
         * @param faces The detected faces in a list
         * @param camera  The {@link Camera} service object
         */
        void onFaceDetection(Face[] faces, Camera camera);
    }    
        
        FaceDetectionListener只有一个方法,FaceDetection(Face[] faces, Camera camera)。其中faces是检测到的人脸数组(注意可能检测不到),camera是当前Camera对象。
        使用时,需要创建一个FaceDetectionListener并重写onFaceDetection方法,在方法中遍历faces进行处理。
        Example:
    mCamera.setFaceDetectionListener(new NodinFaceDetectionListener());     
    
    class NodinFaceDetectionListener implements FaceDetectionListener {
        private static final String TAG = "NodinFaceDetectionListener";
        
        @Override
        public void onFaceDetection(Face[] faces, Camera camera) {
            // TODO Auto-generated method stub
            if (null == faces || faces.length == 0) {
                //TODO Hide face frame.
                Log.d(TAG"onFaceDetection : There is no face found.");
            } else {
                //Face face = faces[0];
                //Rect faceRect = face.rect;
                //TODO Calculate the size of face frame and show.
                Log.d(TAG"onFaceDetection : At least one face has been found.");
            }
        }
    }
    3、启动FaceDetection
    启动FaceDetection很简单,mCamera.startFaceDetection().
    但是需要注意一个问题:startFaceDetection()方法必须在Camera的startPreview之后调用
    4、停止FaceDetection
    mCamera.stopFaceDetection().
    同样需要注意顺序问题:stopFaceDetection()方法必须在Camera的stopPreview之前调用
    5、关于takePicture
    Google API描述,在执行Camera的takePicture方法时,系统会自动调用stopPreview同时停止FaceDetection。如果此后又调用了stopFaceDetection()方法,会抛出IllegalStateException:stop facedetection failed.由于该异常是从底层抛出的,无法确定异常抛出行,解决起来比较棘手。在此例中,由于FaceDetection在takePicture时已经stop,如果再调用stopFaceDetection()方法,会出现该问题。在使用FaceDetection时,使用状态标识来记录Face Detector的状态,在takePicture时将标识设为stop并在start和stop方法中判断标识状态,可以避免该问题。
 
——2013.5.14 写于北京
posted @ 2013-05-25 18:50  陌上幽人  阅读(8764)  评论(0编辑  收藏  举报