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函数大致内容如下:

  1. /*Your application must start the face detection function each time you start (or restart) the 
  2. camera preview. Create a method for starting face detection so you can call it as needed, as shown 
  3. in the example code below.*/  
  4.   
  5. public void startFaceDetection(){  
  6.     // Try starting Face Detection   
  7.     ....  
  8.     // start face detection only *after* preview has started   
  9.     if (params.getMaxNumDetectedFaces() > 0){  
  10.         // camera supports face detection, so can start it:   
  11.         mCamera.startFaceDetection();  
  12.     }  
  13. }  
/*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

内容如下:

  1. /** 
  2.  * Gets the maximum number of detected faces supported. This is the 
  3.  * maximum length of the list returned from {@link FaceDetectionListener}. 
  4.  * If the return value is 0, face detection of the specified type is not 
  5.  * supported. 
  6.  * 
  7.  * @return the maximum number of detected face supported by the camera. 
  8.  * @see #startFaceDetection() 
  9.  */  
  10. public int getMaxNumDetectedFaces() {  
  11.     return getInt(KEY_MAX_NUM_DETECTED_FACES_HW, 0);  
  12. }  
        /**
         * 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函数里设置的,设置如下
  1. <SPAN style="FONT-SIZE: 12px">p.set(CameraParameters::KEY_MAX_NUM_DETECTED_FACES_HW, "1"); //add by dao set the max face detectctor number </SPAN>  
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函数,内容如下:

  1. /** 
  2.  * Starts the face detection. This should be called after preview is started. 
  3.  * The camera will notify {@link FaceDetectionListener} of the detected 
  4.  * faces in the preview frame. The detected faces may be the same as the 
  5.  * previous ones. Applications should call {@link #stopFaceDetection} to 
  6.  * stop the face detection. This method is supported if {@link 
  7.  * Parameters#getMaxNumDetectedFaces()} returns a number larger than 0. 
  8.  * If the face detection has started, apps should not call this again. 
  9.  * 
  10.  * <p>When the face detection is running, {@link Parameters#setWhiteBalance(String)}, 
  11.  * {@link Parameters#setFocusAreas(List)}, and {@link Parameters#setMeteringAreas(List)} 
  12.  * have no effect. The camera uses the detected faces to do auto-white balance, 
  13.  * auto exposure, and autofocus. 
  14.  * 
  15.  * <p>If the apps call {@link #autoFocus(AutoFocusCallback)}, the camera 
  16.  * will stop sending face callbacks. The last face callback indicates the 
  17.  * areas used to do autofocus. After focus completes, face detection will 
  18.  * resume sending face callbacks. If the apps call {@link 
  19.  * #cancelAutoFocus()}, the face callbacks will also resume.</p> 
  20.  * 
  21.  * <p>After calling {@link #takePicture(Camera.ShutterCallback, Camera.PictureCallback, 
  22.  * Camera.PictureCallback)} or {@link #stopPreview()}, and then resuming 
  23.  * preview with {@link #startPreview()}, the apps should call this method 
  24.  * again to resume face detection.</p> 
  25.  * 
  26.  * @throws IllegalArgumentException if the face detection is unsupported. 
  27.  * @throws RuntimeException if the method fails or the face detection is 
  28.  *         already running. 
  29.  * @see FaceDetectionListener 
  30.  * @see #stopFaceDetection() 
  31.  * @see Parameters#getMaxNumDetectedFaces() 
  32.  */  
  33. public final void startFaceDetection() {  
  34.     if (mFaceDetectionRunning) {  
  35.         throw new RuntimeException("Face detection is already running");  
  36.     }  
  37.     _startFaceDetection(CAMERA_FACE_DETECTION_HW);  
  38.     mFaceDetectionRunning = true;  
  39. }  
    /**
     * 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对应的函数:

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

  1. static void android_hardware_Camera_startFaceDetection(JNIEnv *env, jobject thiz,  
  2.         jint type)  
  3. {  
  4.     LOGV("startFaceDetection");  
  5.     JNICameraContext* context;  
  6.     sp<Camera> camera = get_native_camera(env, thiz, &context);  
  7.     if (camera == 0return;  
  8.   
  9.     status_t rc = camera->sendCommand(CAMERA_CMD_START_FACE_DETECTION, type, 0);  
  10.     if (rc == BAD_VALUE) {  
  11.         char msg[64];  
  12.         snprintf(msg, sizeof(msg), "invalid face detection type=%d", type);  
  13.         jniThrowException(env, "java/lang/IllegalArgumentException", msg);  
  14.     } else if (rc != NO_ERROR) {  
  15.         jniThrowRuntimeException(env, "start face detection failed");  
  16.     }  
  17. }  
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函数,内容如下

  1. status_t CameraHardwareSec::sendCommand(int32_t command, int32_t arg1, int32_t arg2)  
  2. {  
  3.     return BAD_VALUE;  
  4. }  
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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值