UseCase

@RequiresApi(value = 21)
public abstract class UseCase

Known direct subclasses
ImageAnalysis

A use case providing CPU accessible images for an app to perform image analysis on.

ImageCapture

A use case for taking a picture.

Preview

A use case that provides a camera preview stream for displaying on-screen.

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

Public methods

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

A utility function that can convert the orientation degrees of OrientationEventListener to the nearest Surface rotation.

Public methods

snapToSurfaceRotation

Added in 1.3.0
public static int snapToSurfaceRotation(@IntRange(from = 0, to = 359) int orientation)

A utility function that can convert the orientation degrees of OrientationEventListener to the nearest Surface rotation.

In general, it is best to use an android.view.OrientationEventListener 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 android.view.OrientationEventListener, 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();
    }
}
Parameters
@IntRange(from = 0, to = 359) int orientation

the orientation degrees in range [0, 359].

Returns
int

surface rotation. One of ROTATION_0, ROTATION_90, ROTATION_180 and ROTATION_270.

Throws
java.lang.IllegalArgumentException

if the input orientation degrees is not in range [0, 359].