android4.0 FaceDetection笔记

这几天研究了下andoid4.0.3的FaceDetection这里写一下大致的流程,方便日后查阅。

相关说明可以在这里找到:

frameworks/base/docs/html/guide/topics/media/camera.jd

起始代码可以在camera.jd里找到也可以在

packages/apps/Camera/src/com/android/camera/Camera.java里找到,具体代码我就不说了.

一般起始点是startFaceDetection函数

startFaceDetection函数大致内容如下:

/*Your application must start the face detection function each time you start (or restart) the
camera preview. Create a method for starting face detection so you can call it as needed, as shown
in the example code below.*/

public void startFaceDetection(){
    // Try starting Face Detection
    ....
    // start face detection only *after* preview has started
    if (params.getMaxNumDetectedFaces() > 0){
        // camera supports face detection, so can start it:
        mCamera.startFaceDetection();
    }
}
首先判断硬件是否支持该功能

getMaxNumDetectedFaces函数在

frameworks/base/core/java/android/hardware/Camera.java

内容如下:

        /**
         * Gets the maximum number of detected faces supported. This is the
         * maximum length of the list returned from {@link FaceDetectionListener}.
         * If the return value is 0, face detection of the specified type is not
         * supported.
         *
         * @return the maximum number of detected face supported by the camera.
         * @see #startFaceDetection()
         */
        public int getMaxNumDetectedFaces() {
            return getInt(KEY_MAX_NUM_DETECTED_FACES_HW, 0);
        }
而KEY_MAX_NUM_DETECTED_FACES_HW一般是在HAL层initDefaultParameters函数里设置的,设置如下
p.set(CameraParameters::KEY_MAX_NUM_DETECTED_FACES_HW, "1"); //add by dao set the max face detectctor number 
initDefaultParameters里类似这样的代码,其它的我就不贴出来了.

mCamera.startFaceDetection();这个是执行的

frameworks/base/core/java/android/hardware/Camera.java里的startFaceDetection函数,内容如下:

    /**
     * Starts the face detection. This should be called after preview is started.
     * The camera will notify {@link FaceDetectionListener} of the detected
     * faces in the preview frame. The detected faces may be the same as the
     * previous ones. Applications should call {@link #stopFaceDetection} to
     * stop the face detection. This method is supported if {@link
     * Parameters#getMaxNumDetectedFaces()} returns a number larger than 0.
     * If the face detection has started, apps should not call this again.
     *
     * <p>When the face detection is running, {@link Parameters#setWhiteBalance(String)},
     * {@link Parameters#setFocusAreas(List)}, and {@link Parameters#setMeteringAreas(List)}
     * have no effect. The camera uses the detected faces to do auto-white balance,
     * auto exposure, and autofocus.
     *
     * <p>If the apps call {@link #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 {@link
     * #cancelAutoFocus()}, the face callbacks will also resume.</p>
     *
     * <p>After calling {@link #takePicture(Camera.ShutterCallback, Camera.PictureCallback,
     * Camera.PictureCallback)} or {@link #stopPreview()}, and then resuming
     * preview with {@link #startPreview()}, the apps should call this method
     * again to resume face detection.</p>
     *
     * @throws IllegalArgumentException if the face detection is unsupported.
     * @throws RuntimeException if the method fails or the face detection is
     *         already running.
     * @see FaceDetectionListener
     * @see #stopFaceDetection()
     * @see Parameters#getMaxNumDetectedFaces()
     */
    public final void startFaceDetection() {
        if (mFaceDetectionRunning) {
            throw new RuntimeException("Face detection is already running");
        }
        _startFaceDetection(CAMERA_FACE_DETECTION_HW);
        mFaceDetectionRunning = true;
    }
_startFaceDetection在JNI里,CAMERA_FACE_DETECTION_HW这里初始化值是0在这个文件的开始有定义

在frameworks/base/core/jni/android_hardware_Camera.cpp我们可以找到_startFaceDetection对应的函数:

  { "_startFaceDetection",
    "(I)V",
    (void *)android_hardware_Camera_startFaceDetection },
对应:android_hardware_Camera_startFaceDetection,内容如下:

static void android_hardware_Camera_startFaceDetection(JNIEnv *env, jobject thiz,
        jint type)
{
    LOGV("startFaceDetection");
    JNICameraContext* context;
    sp<Camera> camera = get_native_camera(env, thiz, &context);
    if (camera == 0) return;

    status_t rc = camera->sendCommand(CAMERA_CMD_START_FACE_DETECTION, type, 0);
    if (rc == BAD_VALUE) {
        char msg[64];
        snprintf(msg, sizeof(msg), "invalid face detection type=%d", type);
        jniThrowException(env, "java/lang/IllegalArgumentException", msg);
    } else if (rc != NO_ERROR) {
        jniThrowRuntimeException(env, "start face detection failed");
    }
}
这里camera->sendCommand对应

device/samsung/common/s5p/libcamera/SecCameraHWInterface.cpp文件里的sendCommand函数,内容如下

status_t CameraHardwareSec::sendCommand(int32_t command, int32_t arg1, int32_t arg2)
{
    return BAD_VALUE;
}
到这里调用就结束了。

不同的产家camera的库所存放的目录可能不一样,同时这个函数需要根据具体的要求,进行修改.
资料参考:http://blog.csdn.net/qq69696698/article/details/7325772

你可能感兴趣的:(android4.0 FaceDetection笔记)