Android Builders Weblog: Introducing Digicam Viewfinder

Posted by Francesco Romano, Developer Relations Engineer, Androidhand holding a phoneThrough the years, Android units have advanced to incorporate a wide range of sizes, shapes, and shows, amongst different options. Because the starting, nevertheless, taking footage together with your telephone has been one of the vital use instances. As we speak, digicam capabilities are nonetheless one of many prime causes customers buy a telephone.

As a developer, you wish to leverage digicam capabilities in your app, so that you determine to undertake the Android Digicam Framework. The primary use case you wish to implement is the Preview use case, which exhibits the output of the digicam sensor on the display.

So that you go forward and create a CaptureSession utilizing a floor as huge because the display dimension. So long as the display has the identical side ratio because the digicam sensor output and the machine stays in its pure portrait orientation, every part needs to be high quality.

However what occurs if you resize the window, unfold your machine, or change the show or orientation? Effectively, usually, the preview might seem stretched, the wrong way up, or incorrectly rotated. And if you’re in a multi-window setting, your app might even crash.

Why does this occur? Due to the implicit assumptions you made whereas creating the CaptureSession.

Traditionally, your app might dwell in the identical window for its entire life cycle, however with the supply of latest type components similar to foldable units, and new show modes similar to multi-window and multi-display, you possibly can’t assume this can be true anymore.

Specifically, let’s have a look at a number of the most vital concerns when creating an app focusing on varied type components:

Let’s study some frequent pitfalls to keep away from when creating an app focusing on varied type components:

  • Do not assume your app will dwell in a portrait-shaped window. Requesting a hard and fast orientation continues to be supported in Android 13, however now machine producers might have the choice of overriding an app request for a most well-liked orientation.
  • Do not assume any fastened dimension or side ratio on your app. Even if you happen to set resizableActivity = “false”, your app might nonetheless be utilized in multi-window mode on massive screens (>=600dp).
  • Do not assume a hard and fast relationship between the orientation of the display and the digicam. The Android Compatibility Definition Doc specifies {that a} digicam picture sensor “MUST be oriented in order that the lengthy dimension of the digicam aligns with the display’s lengthy dimension.” Beginning with API degree 32, digicam shoppers that question the orientation on foldable units can obtain a price that dynamically modifications relying on the machine/fold state.
  • Do not assume the scale of the inset cannot change. The brand new taskbar is reported to functions as an inset, and when used with gesture navigation, the taskbar might be hidden and proven dynamically.
  • Do not assume your app has unique entry to the digicam. Whereas your app is in a multi-window state, different apps can receive entry to shared assets like digicam and microphone.

Whereas CameraX already handles many of the instances above, implementing a preview that works in several eventualities with Camera2 APIs might be advanced, as we describe within the Help resizable surfaces in your digicam app Codelab.

Wouldn’t or not it’s nice to have a easy part that takes care of these particulars and allows you to focus in your particular app logic?

Say no extra…

Introducing CameraViewfinder

CameraViewfinder is a brand new artifact from the Jetpack library that means that you can shortly implement digicam previews with minimal effort. It internally makes use of both a TextureView or SurfaceView to show the digicam feed, and applies the required transformations on them to appropriately show the viewfinder. This entails correcting their side ratio, scale, and rotation. It’s absolutely appropriate together with your present Camera2 codebase and constantly examined on a number of units.

Let’s see find out how to use it.

First, add the dependency in your app-level construct.gradle file:


Sync your challenge. Now it is best to be capable of instantly use the CameraViewfinder as another View. For instance, you possibly can add it to your structure file:


As you possibly can see, CameraViewfinder has the identical controls out there on PreviewView, so you possibly can select totally different Implementation modes and scaling sorts.

Now that the part is a part of the structure, you possibly can nonetheless create a CameraCaptureSession, however as a substitute of offering a TextureView or SurfaceView as goal surfaces, use the results of requestSurfaceAsync().

enjoyable startCamera(){
    val previewResolution = Measurement(width, peak)
    val viewfinderSurfaceRequest =
ViewfinderSurfaceRequest(previewResolution, traits)
    val surfaceListenableFuture =

    Futures.addCallback(surfaceListenableFuture, object : FutureCallback<Floor> {
        override enjoyable onSuccess(floor: Floor) {
            //create a CaptureSession utilizing this floor as normal
        override enjoyable onFailure(t: Throwable) { /* one thing went improper */}
    }, ContextCompat.getMainExecutor(context))

Bonus: optimized layouts for foldable units

CameraViewFinder is ready-to-use throughout resizable surfaces, configuration modifications, rotations, and multi-window modes, and it has been examined on many foldable units.

However if you wish to implement optimized layouts for foldable and twin display units, you possibly can mix CameraViewFinder with the Jetpack WindowManager library to supply distinctive experiences on your customers.

For instance, you possibly can select to keep away from exhibiting full display preview if there’s a hinge in the midst of the display, or if the machine is in “guide” or “tabletop” mode. In these eventualities, you possibly can have the viewfinder in a single portion of the display and the controls on the opposite aspect, or you should utilize a part of the display to indicate the final footage taken. Creativeness is the restrict!

The pattern app is already optimized for foldable units and you’ll find the code to deal with posture modifications right here. Take a look!

Related Articles


Please enter your comment!
Please enter your name here

Latest Articles