Your app can retrieve depth information through ARCore for Jetpack XR to determine how close physical objects are to the device.

Your browser doesn't support HTML video. Here is a[link to the video](https://developer.android.com/static/develop/xr/develop/videos/arcore_raw_depth.mp4)instead.

## Create an ARCore for Jetpack XR session

Obtain head pose information through an ARCore for Jetpack XR session. See[Understand a Session's lifecycle](https://developer.android.com/develop/xr/jetpack-xr-sdk/work-with-arcore#session-lifecycle)to obtain a[`Session`](https://developer.android.com/reference/kotlin/androidx/xr/runtime/Session).

## Configure the session

Depth map retrieval is not enabled by default on XR sessions. To enable depth map retrieval, configure the session and set a[`DepthEstimationMode`](https://developer.android.com/reference/kotlin/androidx/xr/runtime/Config.DepthEstimationMode):

<br />

```kotlin
val newConfig = session.config.copy(
    depthEstimation = Config.DepthEstimationMode.SMOOTH_ONLY,
)
when (val result = session.configure(newConfig)) {
    is SessionConfigureSuccess -> TODO(/* Success! */)
    is SessionConfigureConfigurationNotSupported ->
        TODO(/* Some combinations of configurations are not valid. Handle this failure case. */)
    else ->
        TODO(/* The session could not be configured. See SessionConfigureResult for possible causes. */)
}https://github.com/android/snippets/blob/7bae2021b1c80a8bd422842967ad78dcf4d3756a/xr/src/main/java/com/example/xr/arcore/DepthMaps.kt#L27-L36
```

<br />

The following values of`DepthEstimationMode`are available:

- `DISABLED`: No information about scene depth is provided.
- `RAW_ONLY`: Depth estimation is enabled with raw depth and confidence values.
- `SMOOTH_ONLY`: Depth estimation is enabled with smooth depth and confidence values.
- `SMOOTH_AND_RAW`: Depth estimation is enabled with both raw and smooth depth and confidence values.

Raw depth maps provide depth estimates with higher accuracy, but raw depth images might not include depth estimates for all pixels in the camera image. In contrast, the smooth depth maps provide estimated depth for every pixel, but per-pixel depth data might be less accurate due to smoothing and interpolation of depth estimates.
| **Note:** Enabling any depth estimation mode requires the`android.permission.SCENE_UNDERSTANDING_FINE`[runtime permission](https://developer.android.com/training/permissions/requesting)to be granted to your app.

## Retrieve depth data

To obtain depth data for a given camera, use[`DepthMap`](https://developer.android.com/reference/kotlin/androidx/xr/arcore/DepthMap):

<br />

```kotlin
val depthMap = DepthMap.left(session) ?: return  
https://github.com/android/snippets/blob/7bae2021b1c80a8bd422842967ad78dcf4d3756a/xr/src/main/java/com/example/xr/arcore/DepthMaps.kt#L43-L43
```

<br />

Different devices have different capabilities. Devices with a stereo camera configuration return non-null depth maps for the`left`and`right`cameras. Likewise, devices with a singular camera return a non-null depth map using`mono`.

## Calculate depth values

You can obtain depth and confidence values from the resulting depth map:

<br />

```kotlin
val depthMap = DepthMap.left(session) ?: return  
https://github.com/android/snippets/blob/7bae2021b1c80a8bd422842967ad78dcf4d3756a/xr/src/main/java/com/example/xr/arcore/DepthMaps.kt#L43-L43
```

<br />

Depending on the configuration setting used, access the corresponding depth map using`smoothDepthMap`or`rawDepthMap`. Measurements contained in these maps are expressed in meters. You can also access the confidence values using`smoothConfidenceMap`and`rawConfidenceMap`. These values range from 0 to 255, where 255 represents the highest confidence.

To render a depth map for debug or visualization purposes, see the[Depth part of the ARCore test app](https://cs.android.com/androidx/platform/frameworks/support/+/androidx-main:xr/arcore/integration-tests/testapp/src/main/kotlin/androidx/xr/arcore/testapp/depthmaps/).