ViewfinderView
public final class ViewfinderView extends FrameLayout
| java.lang.Object | ||||
| ↳ | android.view.View | |||
| ↳ | android.view.ViewGroup | |||
| ↳ | android.widget.FrameLayout | |||
| ↳ | androidx.camera.viewfinder.view.ViewfinderView |
Viewfinder widget that displays a Surface from a source such as a Camera2 camera feed.
It internally uses either a TextureView or SurfaceView to display the Surface, and applies required transformations on them to correctly display the viewfinder. This involves correcting their aspect ratio, scale, rotation, and mirroring.
Summary
Public constructors |
|---|
@UiThread |
@UiThread |
@UiThread |
@UiThread |
Public methods |
|
|---|---|
@Nullable Bitmap |
Returns a |
@NonNull ScaleType |
Returns the |
@NonNull ImplementationMode |
Returns the |
@NonNull TransformationInfo |
Returns the |
@NonNull ListenableFuture<ViewfinderSurfaceSession> |
@UiThreadRequests surface by sending a |
void |
@UiThreadApplies a |
void |
@UiThreadUpdates the |
Protected methods |
|
|---|---|
void |
|
void |
Extension functions |
|
|---|---|
final @NonNull ViewfinderSurfaceSession |
ViewfinderViewExtKt.requestSurfaceSession(Provides a suspending function of |
Inherited Constants |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Inherited methods |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Public constructors
ViewfinderView
@UiThread
public ViewfinderView(@NonNull Context context, @Nullable AttributeSet attrs)
ViewfinderView
@UiThread
public ViewfinderView(
@NonNull Context context,
@Nullable AttributeSet attrs,
int defStyleAttr
)
ViewfinderView
@UiThread
public ViewfinderView(
@NonNull Context context,
@Nullable AttributeSet attrs,
int defStyleAttr,
int defStyleRes
)
Public methods
getBitmap
@UiThread
public @Nullable Bitmap getBitmap()
Returns a Bitmap representation of the content displayed on the ViewfinderView, or null if the viewfinder hasn't started yet.
The returned Bitmap uses the ARGB_8888 pixel format and its dimensions are the same as this view's.
Do not invoke this method from a drawing method (onDraw for instance).
If an error occurs during the copy, an empty Bitmap will be returned.
| Returns | |
|---|---|
@Nullable Bitmap |
A |
getScaleType
@UiThread
public @NonNull ScaleType getScaleType()
Returns the ScaleType currently applied to the viewfinder.
The default value is FILL_CENTER.
getSurfaceImplementationMode
@UiThread
public @NonNull ImplementationMode getSurfaceImplementationMode()
Returns the ImplementationMode.
The ImplementationMode can be specified in multiple ways, with the following order of precedence:
- Set directly in the
ViewfinderSurfaceRequest. - Set via the
app:implementationModeattribute in the layout XML. - If not specified, it uses the default value from
getImplementationMode. This default is chosen for maximum compatibility, usingEMBEDDEDon API levels 24 and below or on devices with known hardware quirks, and the higher-performanceEXTERNALon all other devices.
| Returns | |
|---|---|
@NonNull ImplementationMode |
The currently active |
getTransformationInfo
@UiThread
public @NonNull TransformationInfo getTransformationInfo()
Returns the TransformationInfo currently applied to the viewfinder.
| Returns | |
|---|---|
@NonNull TransformationInfo |
the previously set transformation info, or |
requestSurfaceSessionAsync
@UiThread
public @NonNull ListenableFuture<ViewfinderSurfaceSession> requestSurfaceSessionAsync(
@NonNull ViewfinderSurfaceRequest surfaceRequest
)
Requests surface by sending a ViewfinderSurfaceRequest.
Only one request can be handled at the same time. If requesting a surface with a new ViewfinderSurfaceRequest, or the previous request, the previous returned ListenableFuture will be cancelled if it has not yet completed.
The result is a ListenableFuture of ViewfinderSurfaceSession, which provides the functionality to attach listeners and propagate exceptions.
ViewfinderSurfaceRequest request = new ViewfinderSurfaceRequest(width, height);
ListenableFuture<ViewfinderSurfaceSession> sessionFuture =
mViewfinder.requestSurfaceSessionAsync(request);
Futures.addCallback(sessionFuture, new FutureCallback<ViewfinderSurfaceSession>() {
{@}Override
public void onSuccess({@}Nullable ViewfinderSurfaceSession session) {
if (session != null) {
createCaptureSession(session);
}
}
{@}Override
public void onFailure(Throwable t) {}
}, ContextCompat.getMainExecutor(getContext())); If the source will produce frames that are rotated, mirrored, or require a crop, relative to the display orientation, use setTransformationInfo.
| Parameters | |
|---|---|
@NonNull ViewfinderSurfaceRequest surfaceRequest |
The |
| Returns | |
|---|---|
@NonNull ListenableFuture<ViewfinderSurfaceSession> |
A |
setScaleType
@UiThread
public void setScaleType(@NonNull ScaleType scaleType)
Applies a ScaleType to the viewfinder.
This value can also be set in the layout XML file via the app:scaleType attribute.
The default value is FILL_CENTER.
This method should be called after ViewfinderView is inflated and can be called before or after requestSurfaceSessionAsync. The ScaleType to set will be effective immediately after the method is called.
name app:scaleType
setTransformationInfo
@UiThread
public void setTransformationInfo(@NonNull TransformationInfo transformationInfo)
Updates the TransformationInfo used by the current surface session.
This can be used to specify information related to how the source buffers are transformed relative to the coordinate system of the source, such as rotation, mirroring, and crop (region of interest).
If not set, the default value is DEFAULT.
This method should be called after ViewfinderView is inflated and can be called before or after requestSurfaceSessionAsync. The TransformationInfo will be effective immediately after the method is called.
| Parameters | |
|---|---|
@NonNull TransformationInfo transformationInfo |
the updated transformation info. |
Protected methods
Extension functions
ViewfinderViewExtKt.requestSurfaceSession
public final @NonNull ViewfinderSurfaceSession ViewfinderViewExtKt.requestSurfaceSession(
@NonNull ViewfinderView receiver,
@NonNull ViewfinderSurfaceRequest viewfinderSurfaceRequest
)
Provides a suspending function of ViewfinderView.requestSurfaceSessionAsync to request a ViewfinderSurfaceSession by sending a ViewfinderSurfaceRequest.