# SurfaceRequest.TransformationInfo

Artifact: [androidx.camera:camera-core](https://developer.android.com/jetpack/androidx/releases/camera) [View Source](https://cs.android.com/search?q=file:androidx/camera/core/SurfaceRequest.java+class:androidx.camera.core.SurfaceRequest.TransformationInfo) Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/camera#1.0.0)

*** ** * ** ***


```
@AutoValue
public abstract class SurfaceRequest.TransformationInfo
```

<br />

*** ** * ** ***

Transformation associated the preview output.

The `https://developer.android.com/reference/androidx/camera/core/SurfaceRequest.TransformationInfo` can be used transform the `https://developer.android.com/reference/android/view/Surface.html` provided via `https://developer.android.com/reference/androidx/camera/core/SurfaceRequest#provideSurface(android.view.Surface,java.util.concurrent.Executor,androidx.core.util.Consumer<androidx.camera.core.SurfaceRequest.Result>)`. The info is based on the camera sensor rotation, preview target rotation and the `https://developer.android.com/reference/androidx/camera/core/ViewPort` associated with the `https://developer.android.com/reference/androidx/camera/core/Preview`. The application of the info depends on the source of the `https://developer.android.com/reference/android/view/Surface.html`. For detailed example, please check out the source code of PreviewView in androidx.camera.view artifact.

The info is also needed to transform coordinates across use cases. In a face detection example, one common scenario is running a face detection algorithm against a `https://developer.android.com/reference/androidx/camera/core/ImageAnalysis` use case, and highlight the detected face in the preview. Below is a code sample to get the transformation `https://developer.android.com/reference/android/graphics/Matrix.html` based on the `https://developer.android.com/reference/androidx/camera/core/ImageProxy` from `https://developer.android.com/reference/androidx/camera/core/ImageAnalysis` and the `https://developer.android.com/reference/androidx/camera/core/SurfaceRequest.TransformationInfo` from `https://developer.android.com/reference/androidx/camera/core/Preview`:

```
    // Get rotation transformation.
    val transformation = Matrix()
    transformation.setRotate(info.getRotationDegrees())

    // Get rotated crop rect and cropping transformation.
    val rotatedRect = new RectF()
    rotation.mapRect(rotatedRect, RectF(imageProxy.getCropRect()))
    rotatedRect.sort()
    val cropTransformation = Matrix()
    cropTransformation.setRectToRect(
         RectF(imageProxy.getCropRect()), rotatedRect, ScaleToFit.FILL)

    // Concatenate the rotation and cropping transformations.
    transformation.postConcat(cropTransformation)
```

| See also |
|---|---|
| `https://developer.android.com/reference/androidx/camera/core/Preview#setTargetRotation(int)` |   |
| `https://developer.android.com/reference/androidx/camera/core/Preview.Builder#setTargetRotation(int)` |   |
| `https://developer.android.com/reference/android/hardware/camera2/CameraCharacteristics.html#SENSOR_ORIENTATION` |   |
| `https://developer.android.com/reference/androidx/camera/core/ViewPort` |   |

## Summary

| ### Public methods |
|---|---|
| `abstract @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/graphics/Rect.html` | `https://developer.android.com/reference/androidx/camera/core/SurfaceRequest.TransformationInfo#getCropRect()()` Returns the crop rect rectangle. |
| `abstract int` | `https://developer.android.com/reference/androidx/camera/core/SurfaceRequest.TransformationInfo#getRotationDegrees()()` Returns the rotation needed to transform the output from sensor to the target rotation. |
| `abstract @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/graphics/Matrix.html` | `https://developer.android.com/reference/androidx/camera/core/SurfaceRequest.TransformationInfo#getSensorToBufferTransform()()` Returns the sensor to image buffer transform matrix. |
| `abstract boolean` | `https://developer.android.com/reference/androidx/camera/core/SurfaceRequest.TransformationInfo#hasCameraTransform()()` Whether the `https://developer.android.com/reference/android/view/Surface.html` contains the camera transform. |
| `abstract boolean` | `https://developer.android.com/reference/androidx/camera/core/SurfaceRequest.TransformationInfo#isMirroring()()` Returns whether the buffer should be mirrored. |

## Public methods

### getCropRect

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/camera#1.0.0)

```
public abstract @NonNull Rect getCropRect()
```

Returns the crop rect rectangle.

The returned value dictates how the `https://developer.android.com/reference/android/view/Surface.html` provided in `https://developer.android.com/reference/androidx/camera/core/SurfaceRequest#provideSurface(android.view.Surface,java.util.concurrent.Executor,androidx.core.util.Consumer<androidx.camera.core.SurfaceRequest.Result>)` should be displayed. The crop rectangle specifies the region of valid pixels in the buffer, using coordinates from (0, 0) to the (width, height) of `https://developer.android.com/reference/androidx/camera/core/SurfaceRequest#getResolution()`. The caller should arrange the UI so that only the valid region is visible to app users.

If `https://developer.android.com/reference/androidx/camera/core/Preview` is configured with a `https://developer.android.com/reference/androidx/camera/core/ViewPort`, this value is calculated based on the configuration of `https://developer.android.com/reference/androidx/camera/core/ViewPort`; if not, it returns the full rect of the buffer. For code sample on how to apply the crop rect, please see `https://developer.android.com/reference/androidx/camera/core/ViewPort#FIT()`.

| See also |
|---|---|
| `https://developer.android.com/reference/androidx/camera/core/ViewPort` |   |

### getRotationDegrees

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/camera#1.0.0)

```
public abstract int getRotationDegrees()
```

Returns the rotation needed to transform the output from sensor to the target rotation.

This is a clockwise rotation in degrees that needs to be applied to the sensor buffer. The rotation will be determined by `https://developer.android.com/reference/android/hardware/camera2/CameraCharacteristics.html`, `https://developer.android.com/reference/androidx/camera/core/Preview#setTargetRotation(int)` and `https://developer.android.com/reference/androidx/camera/core/Preview.Builder#setTargetRotation(int)`. This value is useful for transforming coordinates across use cases.

This value is most useful for transforming coordinates across use cases, e.g. in preview, highlighting a pattern detected in image analysis. For correcting the preview itself, usually the source of the `https://developer.android.com/reference/android/view/Surface.html` handles the rotation without needing this value. For `https://developer.android.com/reference/android/view/SurfaceView.html`, it automatically corrects the preview to match the display rotation. For `https://developer.android.com/reference/android/view/TextureView.html`, the only additional rotation needed is the display rotation. For detailed example, please check out the source code of PreviewView in androidx.camera .view artifact.

Value is one of the following:

- `0`
- `90`
- `180`
- `270`

<br />

| Returns |
|---|---|
| `int` | The rotation in degrees which will be a value in {0, 90, 180, 270}. |

| See also |
|---|---|
| `https://developer.android.com/reference/androidx/camera/core/Preview#setTargetRotation(int)` |   |
| `https://developer.android.com/reference/androidx/camera/core/Preview#getTargetRotation()` |   |
| `https://developer.android.com/reference/androidx/camera/core/ViewPort` |   |

### getSensorToBufferTransform

Added in [1.4.0](https://developer.android.com/jetpack/androidx/releases/camera#1.4.0)

```
public abstract @NonNull Matrix getSensorToBufferTransform()
```

Returns the sensor to image buffer transform matrix.

The value is a mapping from sensor coordinates to buffer coordinates, which is, from the rect of `https://developer.android.com/reference/android/hardware/camera2/CameraCharacteristics.html#SENSOR_INFO_ACTIVE_ARRAY_SIZE` to the rect defined by `(0, 0, #getResolution#getWidth(), #getResolution#getHeight())`. The matrix can be used to map the coordinates from one `https://developer.android.com/reference/androidx/camera/core/UseCase` to another. For example, detecting face with `https://developer.android.com/reference/androidx/camera/core/ImageAnalysis`, and then highlighting the face in `https://developer.android.com/reference/androidx/camera/core/Preview`.

Code sample

```
 // Get the transformation from sensor to effect input.
 Matrix sensorToEffect = surfaceRequest.getSensorToBufferTransform();
 // Get the transformation from sensor to ImageAnalysis.
 Matrix sensorToAnalysis = imageProxy.getSensorToBufferTransform();
 // Concatenate the two matrices to get the transformation from ImageAnalysis to effect.
 Matrix analysisToEffect = Matrix()
 sensorToAnalysis.invert(analysisToEffect);
 analysisToEffect.postConcat(sensorToEffect);
```

### hasCameraTransform

Added in [1.4.0](https://developer.android.com/jetpack/androidx/releases/camera#1.4.0)

```
public abstract boolean hasCameraTransform()
```

Whether the `https://developer.android.com/reference/android/view/Surface.html` contains the camera transform.

When the Surface is connected to the camera directly, camera writes the camera orientation value to the Surface. For example, the value can be retrieved via `https://developer.android.com/reference/android/graphics/SurfaceTexture.html#getTransformMatrix(float[])`. Android components such as `https://developer.android.com/reference/android/view/TextureView.html` and `https://developer.android.com/reference/android/view/SurfaceView.html` use the value to transform the output. When the Surface is not connect to the camera directly, for example, when it was copied with OpenGL, the Surface will not contain the camera orientation value.

The app may need to transform the UI differently based on this flag. If this value is true, the app only needs to apply the Surface transformation; otherwise, the app needs to apply the value of `https://developer.android.com/reference/androidx/camera/core/SurfaceRequest.TransformationInfo#getRotationDegrees()`. For example, if the preview is displayed in a `https://developer.android.com/reference/android/view/TextureView.html`:

```
int rotationDegrees;
if (surfaceRequest.hasCameraTransform()) {
  switch (textureView.getDisplay().getRotation()) {
    case Surface.ROTATION_0:
      rotationDegrees = 0;
      break;
    case Surface.ROTATION_90:
      rotationDegrees = 90;
      break;
    case Surface.ROTATION_180:
      rotationDegrees = 180;
      break;
    case Surface.ROTATION_270:
      rotationDegrees = 270;
      break;
    }
} else {
  rotationDegrees = transformationInfo.getRotationDegrees();
}
Matrix textureViewTransform = new Matrix();
textureViewTransform.postRotate(rotationDegrees);
textureView.setTransform(textureViewTransform);
```

| Returns |
|---|---|
| `boolean` | true if the `https://developer.android.com/reference/android/view/Surface.html` contains the camera transformation. |

### isMirroring

Added in [1.4.0](https://developer.android.com/jetpack/androidx/releases/camera#1.4.0)

```
public abstract boolean isMirroring()
```

Returns whether the buffer should be mirrored.

This flag indicates whether the buffer needs to be mirrored across the vertical axis. For example, for front camera preview, the buffer should usually be mirrored. The mirroring should be applied after the `https://developer.android.com/reference/androidx/camera/core/SurfaceRequest.TransformationInfo#getRotationDegrees()` is applied.