Google 致力于为黑人社区推动种族平等。查看具体举措

Preview

public final 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.SurfaceProvider. 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.SurfaceProvider

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

Public methods

int getTargetRotation()

Returns the rotation that the intended target resolution is expressed in.

void setSurfaceProvider(Executor executor, Preview.SurfaceProvider surfaceProvider)

Sets a Preview.SurfaceProvider to provide a Surface for Preview.

void setSurfaceProvider(Preview.SurfaceProvider surfaceProvider)

Sets a Preview.SurfaceProvider to provide a Surface for Preview.

void setTargetRotation(int targetRotation)

Sets the target rotation.

String toString()

Inherited methods

Public methods

getTargetRotation

public int getTargetRotation ()

Returns the rotation that the intended target resolution is expressed in.

The rotation is set when constructing an Preview instance using Preview.Builder.setTargetRotation(int). If not set, the target rotation defaults to the value of Display.getRotation() of the default display at the time the use case is created. The use case is fully created once it has been attached to a camera.

Returns
int The rotation of the intended target.

setSurfaceProvider

public void setSurfaceProvider (Executor executor, 
                Preview.SurfaceProvider surfaceProvider)

Sets a Preview.SurfaceProvider to provide a Surface for Preview.

Setting the provider will signal to the camera that the use case is ready to receive data. If the provider is removed by calling this again with a null SurfaceProvider then the camera will stop producing data for this Preview instance.

Parameters
executor Executor: on which the surfaceProvider will be invoked.

surfaceProvider Preview.SurfaceProvider: SurfaceProvider that provides a Surface for Preview. This will replace the previous SurfaceProvider set either this method or setSurfaceProvider(SurfaceProvider).

setSurfaceProvider

public void setSurfaceProvider (Preview.SurfaceProvider surfaceProvider)

Sets a Preview.SurfaceProvider to provide a 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. If the provider is removed by calling this again with a null SurfaceProvider then the camera will stop producing data for this Preview instance.

Parameters
surfaceProvider Preview.SurfaceProvider: SurfaceProvider that provides a Surface for Preview. This will replace the previous SurfaceProvider set either this method or setSurfaceProvider(Executor, SurfaceProvider).

setTargetRotation

public void setTargetRotation (int targetRotation)

Sets the target rotation.

This adjust the getTargetRotation(), which once applied will update the output to match target rotation specified here.

While rotation can also be set via Preview.Builder.setTargetRotation(int) , using setTargetRotation(int) allows the target rotation to be set without rebinding new use cases. When this function is called, value set by Preview.Builder.setTargetResolution(Size) will be updated automatically to make sure the suitable resolution can be selected when the use case is bound.

If not set here or by configuration, the target rotation will default to the value of Display.getRotation() of the default display at the time the use case is created. The use case is fully created once it has been attached to a camera.

Parameters
targetRotation int: Target rotation of the output image, expressed as one of Surface.ROTATION_0, Surface.ROTATION_90, Surface.ROTATION_180, or Surface.ROTATION_270.

toString

public String toString ()

Returns
String