Preview

public class Preview
extends UseCase

java.lang.Object
   ↳ androidx.camera.core.UseCase
     ↳ androidx.camera.core.Preview


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

The preview stream is connected to the Surface provided via Preview.PreviewSurfaceProvider. The application decides how the Surface is shown, and is responsible for managing the Surface lifecycle after providing it.

To display the preview with the correct orientation, app needs to take different actions based on the source of the Surface. If the Surface is backed by a SurfaceView, it will always be in the device's display orientation. If the Surface is backed by ImageReader, MediaCodec or other objects, it's the application's responsibility to calculate the rotation. If the Surface is backed by a SurfaceTexture, SurfaceTexture.getTransformMatrix(float[]) can be used to transform the preview to natural orientation. The value is available after a frame is pushed to the SurfaceTexture and its SurfaceTexture.OnFrameAvailableListener.onFrameAvailable(SurfaceTexture) has been called. TextureView handles this automatically and always puts the preview in the natural orientation. To further transform the TextureView to display orientation, the app needs to apply the current display rotation. Example:

     
         switch (getWindowManager().getDefaultDisplay().getRotation()) {
             case Surface.ROTATION_0:
                 displayRotation = 0;
                 break;
             case Surface.ROTATION_90:
                 displayRotation = 90;
                 break;
             case Surface.ROTATION_180:
                 displayRotation = 180;
                 break;
             case Surface.ROTATION_270:
                 displayRotation = 270;
                 break;
             default:
                 throw new UnsupportedOperationException(
                         "Unsupported display rotation: " + displayRotation);
         }
         matrix.postRotate(-displayRotation, centerX, centerY);
         textureView.setTransform(matrix);
     
 

Summary

Nested classes

class Preview.Builder

Builder for a Preview

interface Preview.PreviewSurfaceProvider

A interface implemented by the application to provide a Surface for Preview

Public methods

Preview.PreviewSurfaceProvider getPreviewSurfaceProvider()

Gets Preview.PreviewSurfaceProvider

Setting the callback will signal to the camera that the use case is ready to receive data.

void setPreviewSurfaceProvider(Executor executor, Preview.PreviewSurfaceProvider previewSurfaceProvider)

Sets a Preview.PreviewSurfaceProvider to provide Surface for Preview.

void setPreviewSurfaceProvider(Preview.PreviewSurfaceProvider previewSurfaceProvider)

Sets a Preview.PreviewSurfaceProvider to provide Surface for Preview.

String toString()

Inherited methods

Public methods

getPreviewSurfaceProvider

public Preview.PreviewSurfaceProvider getPreviewSurfaceProvider ()

Gets Preview.PreviewSurfaceProvider

Setting the callback will signal to the camera that the use case is ready to receive data.

Returns
Preview.PreviewSurfaceProvider the last set callback or null if no listener is set

setPreviewSurfaceProvider

public void setPreviewSurfaceProvider (Executor executor, 
                Preview.PreviewSurfaceProvider previewSurfaceProvider)

Sets a Preview.PreviewSurfaceProvider to provide Surface for Preview.

Setting the provider will signal to the camera that the use case is ready to receive data.

Parameters
executor Executor: on which the previewSurfaceProvider will be triggered.

previewSurfaceProvider Preview.PreviewSurfaceProvider: PreviewSurfaceProvider that provides a Preview.

setPreviewSurfaceProvider

public void setPreviewSurfaceProvider (Preview.PreviewSurfaceProvider previewSurfaceProvider)

Sets a Preview.PreviewSurfaceProvider to provide Surface for Preview.

Setting the provider will signal to the camera that the use case is ready to receive data. The provider will be triggered on main thread.

Parameters
previewSurfaceProvider Preview.PreviewSurfaceProvider: PreviewSurfaceProvider that provides a Preview.

toString

public String toString ()

Returns
String