IT_Programming/Android_Java

[Android] ICS부터 지원되는 Face Detection을 사용해 보자.

JJun ™ 2013. 7. 16. 07:11

 

 


출처: http://lsit81.tistory.com/entry/Android-ICS부터-지원되는-Face-Detection을-사용해-보자


 

 

 

 

Face detection

 

 

안드로이드 ICS(API 14)부터 Camera의 영상을 통해 사람의 얼굴을 인식할 수 있는 기능이 추가되었습니다. 사람의 얼굴 인식기능은 단순히 사람의 얼굴 위치만 추적이 가능하며 눈, 코, 입과 같은 세부 사항은 볼 수 없습니다

 

 

Note: Face detection이 동작하는 동안, setWhiteBalance(String)setFocusAreas(List)setMeteringAreas(List) 은 아무런 영향을 받지 않습니다. 

 


 

* 사용 절차

 


    1. 하드웨어 장비가 Face Detection 기능을 제공하는지 체크합니다.  
    2. Face Detection Listener를 만든다.
    3. 생성된 Listener를 Camera에 연결 시킵니다.
    4. 카메라 프리뷰가 나오면 Face Detection을 시작시킵니다.

 


 

그럼 아래에 실제 코드를 보여드리도록 하겠습니다. 

* 사용 방법.

 

1. 
 "Camera.Parameters.getMaxNumDetectedFaces()"을 이용하여 Face Detection 기능을
      제공하는지 체크


1
2
3
4
5
// start face detection only *after* preview has started
if (params.getMaxNumDetectedFaces() > 0){
    // Face Detection 기능을 제공할 경우 1 이상의 값이 나타납니다.
    mCamera.startFaceDetection();
}




2. Camera.FaceDetectionListener Interface를 이용하여 onFaceDetection() 부분을

    구현합니다. 

 

1
2
3
4
5
6
7
8
9
10
11
class MyFaceDetectionListener implements Camera.FaceDetectionListener {
 
    @Override
    public void onFaceDetection(Face[] faces, Camera camera) {
        if (faces.length > 0){
            Log.d("FaceDetection", "face detected: "+ faces.length +
                    " Face 1 Location X: " + faces[0].rect.centerX() +
                    "Y: " + faces[0].rect.centerY() );
        }
    }
}



* 주의사항

 


onFaceDetection(Face[] faces, Camera camera)에서 faces 객체에 넘어오는

데이터의 좌표는 "[Android] Camera ICS에서 선택 포커싱" 포스팅에서 설명 드렸던

내용과 같이 


가로 : -1000 ~ 1000

세로 : -1000 ~ 1000


으로 넘어 옮니다. 


그리고 단말기에 따라서 전면 카메라를 사용하는 경우 x축에 대한 좌표가 reverse되어서

넘어올 수 있습니다. 


 


 

3. Camera에  Camera.FaceDetectionListener를 연결 시킵니다.

 

1  
mCamera.setFaceDetectionListener(new MyFaceDetectionListener());




4. Camera Preview를 시작 시킨이후 Face Detection을 시작시킵니다.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
public void surfaceCreated(SurfaceHolder holder) {
    try {
        mCamera.setPreviewDisplay(holder);
        mCamera.startPreview();
 
        startFaceDetection(); // start face detection feature
 
    } catch (IOException e) {
        Log.d(TAG, "Error setting camera preview: " + e.getMessage());
    }
}
 
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
 
    if (mHolder.getSurface() == null){
        // preview surface does not exist
        Log.d(TAG, "mHolder.getSurface() == null");
        return;
    }
 
    try {
        mCamera.stopPreview();
 
    } catch (Exception e){
        // ignore: tried to stop a non-existent preview
        Log.d(TAG, "Error stopping camera preview: " + e.getMessage());
    }
 
    try {
        mCamera.setPreviewDisplay(mHolder);
        mCamera.startPreview();
 
        startFaceDetection(); // re-start face detection feature
 
    } catch (Exception e){
        // ignore: tried to stop a non-existent preview
        Log.d(TAG, "Error starting camera preview: " + e.getMessage());
    }
}


 

 

* 참고 

http://developer.android.com/guide/topics/media/camera.html#metering-focus-areas


 

 

* 샘플 소스

 

CameraAPI14Test.zip

 

 

 

CameraAPI14Test.zip
0.04MB