# 人脸检测 (/zh-CN/api-reference/rtc/android/video/video-prenpro/face-detection)

> For AI agents: see the complete documentation index at [llms.txt](/llms.txt).

<a id="toc_face_detection"></a>

<a id="api_irtcengine_enablefacedetection"></a>

<a id="ariaid-title2"></a>

## enableFaceDetection [#enablefacedetection]

启用或关闭本地用户的人脸检测功能。

<a id="api_irtcengine_enablefacedetection__prototype"></a>

```java
public abstract int enableFaceDetection(boolean enabled);
```

<a id="api_irtcengine_enablefacedetection__detailed_desc"></a>

<a id="api_irtcengine_enablefacedetection__timing"></a>

### 调用时机 [#调用时机]

该方法需在调用 `startPreview` 或 [enableVideo](/zh-CN/api-reference/rtc/android/video/video-basic#api_irtcengine_enablevideo) 启动相机后调用。

<a id="api_irtcengine_enablefacedetection__related"></a>

### 相关回调 [#相关回调]

启用人脸检测后，SDK 会触发 [onFacePositionChanged](/zh-CN/api-reference/rtc/android/video/video-prenpro/face-detection#callback_irtcengineeventhandler_onfacepositionchanged) 回调，报告本地用户的人脸信息，包括：

- 本地视频的宽度和高度。
- 人脸在本地视图中的位置。
- 人脸与屏幕之间的距离。

<a id="api_irtcengine_enablefacedetection__parameters"></a>

### 参数 [#参数]

<ParameterList>
<a id="enabled"></a>
<Parameter name="enabled">

是否启用本地用户的人脸检测功能：

- true：启用人脸检测。
- false：（默认）关闭人脸检测。

</Parameter>
</ParameterList>

<a id="api_irtcengine_enablefacedetection__return_values"></a>

### 返回值 [#返回值]

- 0：方法调用成功。
- &lt; 0：方法调用失败。详见[错误码](/zh-CN/api-reference/rtc/error-code)了解详情和解决建议。

### 所属接口类 [#所属接口类]

- [RtcEngine](/zh-CN/api-reference/rtc/android/rtc-interface-class#class_irtcengine)

<a id="callback_irtcengineeventhandler_onfacepositionchanged"></a>

<a id="ariaid-title3"></a>

## onFacePositionChanged [#onfacepositionchanged]

上报本地用户的人脸检测结果。

<a id="callback_irtcengineeventhandler_onfacepositionchanged__prototype"></a>

```java
public void onFacePositionChanged(int imageWidth, int imageHeight, AgoraFacePositionInfo[] faceRectArr)
```

<a id="callback_irtcengineeventhandler_onfacepositionchanged__detailed_desc"></a>

调用 `enableFaceDetection(true)` 启用人脸检测功能后，你可以实时获取以下信息：

- 本地视频的宽度和高度。
- 人脸在本地视图中的位置。
- 人脸与屏幕之间的距离。该距离值基于本地视频尺寸和人脸位置的拟合计算得出。

:::note
注：

- 当检测到摄像头前的人脸消失时，会立即触发该回调。当未检测到人脸时，为降低本地设备的功耗，该回调的触发频率会降低。
- 当人脸靠近屏幕时，SDK 会停止触发该回调。
- 在 Android 平台，该回调中上报的距离值可能与实际距离略有偏差，因此不建议用于精确计算。
:::

<a id="callback_irtcengineeventhandler_onfacepositionchanged__parameters"></a>

### 参数 [#参数-1]

<ParameterList>
<a id="imagewidth"></a>
<Parameter name="imageWidth">

本地摄像头采集的视频图像的宽度（单位为像素）。

</Parameter>
<a id="imageheight"></a>
<Parameter name="imageHeight">

本地摄像头采集的视频图像的高度（单位为像素）。

</Parameter>
<a id="facerectarr"></a>
<Parameter name="faceRectArr">

检测到的人脸信息。该数组的长度取决于检测到的人脸数量，可能为 0，表示摄像头前未检测到人脸。详见 [AgoraFacePositionInfo](/zh-CN/api-reference/rtc/android/class-agorafacepositioninfo)。

</Parameter>
</ParameterList>

### 所属接口类 [#所属接口类-1]

- [IRtcEngineEventHandler](/zh-CN/api-reference/rtc/android/rtc-interface-class#class_irtcengineeventhandler)
