# UseCase

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/UseCase.java+class:androidx.camera.core.UseCase) Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/camera#1.0.0)

*** ** * ** ***


```
public abstract class UseCase
```

<br />

Known direct subclasses [ImageAnalysis](https://developer.android.com/reference/androidx/camera/core/ImageAnalysis), [ImageCapture](https://developer.android.com/reference/androidx/camera/core/ImageCapture), [Preview](https://developer.android.com/reference/androidx/camera/core/Preview), [VideoCapture](https://developer.android.com/reference/androidx/camera/video/VideoCapture)

|---|---|
| `https://developer.android.com/reference/androidx/camera/core/ImageAnalysis` | A use case providing CPU accessible images for an app to perform image analysis on. |
| `https://developer.android.com/reference/androidx/camera/core/ImageCapture` | A use case for taking a picture. |
| `https://developer.android.com/reference/androidx/camera/core/Preview` | A use case that provides a camera preview stream for displaying on-screen. |
| `https://developer.android.com/reference/androidx/camera/video/VideoCapture` | A use case that provides camera stream suitable for video application. |

*** ** * ** ***

The use case which all other use cases are built on top of.

A UseCase provides functionality to map the set of arguments in a use case to arguments that are usable by a camera. UseCase also will communicate of the active/inactive state to the Camera.

## Summary

| ### Nested types |
|---|
| `public interface https://developer.android.com/reference/androidx/camera/core/UseCase.InteropConfigurable<B extends https://developer.android.com/reference/androidx/camera/core/UseCase.InteropConfigurable<B>>` Applies camera-backend-specific options. |

| ### Public methods |
|---|---|
| `static int` | `https://developer.android.com/reference/androidx/camera/core/UseCase#snapToSurfaceRotation(int)(@https://developer.android.com/reference/androidx/annotation/IntRange(from = 0, to = 359) int orientation)` A utility function that can convert the orientation degrees of `https://developer.android.com/reference/android/view/OrientationEventListener.html` to the nearest `https://developer.android.com/reference/android/view/Surface.html` rotation. |

## Public methods

### snapToSurfaceRotation

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

```
public static int snapToSurfaceRotation(@IntRange(from = 0, to = 359) int orientation)
```

A utility function that can convert the orientation degrees of `https://developer.android.com/reference/android/view/OrientationEventListener.html` to the nearest `https://developer.android.com/reference/android/view/Surface.html` rotation.

In general, it is best to use an `https://developer.android.com/reference/android/view/OrientationEventListener.html` to set the UseCase target rotation. This way, the rotation output will indicate which way is down for a given image or video. This is important since display orientation may be locked by device default, user setting, or app configuration, and some devices may not transition to a reverse-portrait display orientation. In these cases, set target rotation dynamically according to the `https://developer.android.com/reference/android/view/OrientationEventListener.html`, without re-creating the use case. The sample code is as below:

```
public class CameraXActivity extends AppCompatActivity {

    private OrientationEventListener mOrientationEventListener;

    
    protected void onStart() {
        super.onStart();
        if (mOrientationEventListener == null) {
            mOrientationEventListener = new OrientationEventListener(this) {
                
                public void onOrientationChanged(int orientation) {
                    if (orientation == OrientationEventListener.ORIENTATION_UNKNOWN) {
                        return;
                    }
                    int rotation = UseCase.snapToSurfaceRotation(orientation);
                    mImageCapture.setTargetRotation(rotation);
                    mImageAnalysis.setTargetRotation(rotation);
                    mVideoCapture.setTargetRotation(rotation);
                }
            };
        }
        mOrientationEventListener.enable();
    }

    
    protected void onStop() {
        super.onStop();
        mOrientationEventListener.disable();
    }
}
```

Value is one of the following:

- `Surface.ROTATION_0`
- `Surface.ROTATION_90`
- `Surface.ROTATION_180`
- `Surface.ROTATION_270`

<br />

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/IntRange(from = 0, to = 359) int orientation` | the orientation degrees in range \[0, 359\]. |

| Returns |
|---|---|
| `int` | surface rotation. One of `https://developer.android.com/reference/android/view/Surface.html#ROTATION_0`, `https://developer.android.com/reference/android/view/Surface.html#ROTATION_90`, `https://developer.android.com/reference/android/view/Surface.html#ROTATION_180` and `https://developer.android.com/reference/android/view/Surface.html#ROTATION_270`. |

| Throws |
|---|---|
| `https://developer.android.com/reference/java/lang/IllegalArgumentException.html` | if the input orientation degrees is not in range \[0, 359\]. |

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