Android 適用的擴增臉部開發人員指南

瞭解如何在你的應用程式中使用擴增臉孔功能。

必要條件

請務必瞭解基本 AR 概念 以及如何在繼續操作前設定 ARCore 工作階段

在 Android 中使用擴增臉孔

  1. 設定 ARCore 工作階段
  2. 使用系統偵測到的臉孔

設定 ARCore 工作階段

在現有的 ARCore 工作階段中選取前置鏡頭,即可開始使用擴增臉孔。請注意,選取前置鏡頭 都會造成幾次變更 執行迴圈

Java

// Set a camera configuration that usese the front-facing camera.
CameraConfigFilter filter =
    new CameraConfigFilter(session).setFacingDirection(CameraConfig.FacingDirection.FRONT);
CameraConfig cameraConfig = session.getSupportedCameraConfigs(filter).get(0);
session.setCameraConfig(cameraConfig);

Kotlin

// Set a camera configuration that usese the front-facing camera.
val filter = CameraConfigFilter(session).setFacingDirection(CameraConfig.FacingDirection.FRONT)
val cameraConfig = session.getSupportedCameraConfigs(filter)[0]
session.cameraConfig = cameraConfig

啟用 AugmentedFaceMode

Java

Config config = new Config(session);
config.setAugmentedFaceMode(Config.AugmentedFaceMode.MESH3D);
session.configure(config);

Kotlin

val config = Config(session)
config.augmentedFaceMode = Config.AugmentedFaceMode.MESH3D
session.configure(config)

臉孔網格方向

請注意臉部網格的方向:

存取偵測到的臉孔

取得 TrackableTrackable 是 ARCore 可以追蹤的項目,且 錨點可附加至圖片。

Java

// ARCore's face detection works best on upright faces, relative to gravity.
Collection<AugmentedFace> faces = session.getAllTrackables(AugmentedFace.class);

Kotlin

// ARCore's face detection works best on upright faces, relative to gravity.
val faces = session.getAllTrackables(AugmentedFace::class.java)

取得 TrackingState 每個 Trackable。如果是 TRACKING, 那麼 ARCore 就能知道它的姿勢。

Java

for (AugmentedFace face : faces) {
  if (face.getTrackingState() == TrackingState.TRACKING) {
    // UVs and indices can be cached as they do not change during the session.
    FloatBuffer uvs = face.getMeshTextureCoordinates();
    ShortBuffer indices = face.getMeshTriangleIndices();
    // Center and region poses, mesh vertices, and normals are updated each frame.
    Pose facePose = face.getCenterPose();
    FloatBuffer faceVertices = face.getMeshVertices();
    FloatBuffer faceNormals = face.getMeshNormals();
    // Render the face using these values with OpenGL.
  }
}

Kotlin

faces.forEach { face ->
  if (face.trackingState == TrackingState.TRACKING) {
    // UVs and indices can be cached as they do not change during the session.
    val uvs = face.meshTextureCoordinates
    val indices = face.meshTriangleIndices
    // Center and region poses, mesh vertices, and normals are updated each frame.
    val facePose = face.centerPose
    val faceVertices = face.meshVertices
    val faceNormals = face.meshNormals
    // Render the face using these values with OpenGL.
  }
}