Android Builders Weblog: Introducing Digital camera Viewfinder


Posted by Francesco Romano, Developer Relations Engineer, Androidhand holding a phoneOver time, Android gadgets have developed to incorporate a wide range of sizes, shapes, and shows, amongst different options. For the reason that starting, nonetheless, taking photos along with your telephone has been probably the most necessary use instances. Right this moment, digicam capabilities are nonetheless one of many high causes shoppers buy a telephone.

As a developer, you need to leverage digicam capabilities in your app, so that you resolve to undertake the Android Digital camera Framework. The primary use case you need to implement is the Preview use case, which reveals 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 facet ratio because the digicam sensor output and the system stays in its pure portrait orientation, all the pieces must be advantageous.

However what occurs while you resize the window, unfold your system, or change the show or orientation? Nicely, typically, the preview could seem stretched, the other way up, or incorrectly rotated. And in case you are in a multi-window surroundings, your app could even crash.

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

Traditionally, your app may dwell in the identical window for its entire life cycle, however with the supply of latest kind components comparable to foldable gadgets, and new show modes comparable to multi-window and multi-display, you may’t assume this shall be true anymore.

Particularly, let’s examine among the most necessary issues when creating an app focusing on numerous kind components:

Let’s study some widespread pitfalls to keep away from when creating an app focusing on numerous kind components:

  • Do not assume your app will dwell in a portrait-shaped window. Requesting a hard and fast orientation remains to be supported in Android 13, however now system producers could have the choice of overriding an app request for a most popular orientation.
  • Do not assume any mounted dimension or facet ratio in your app. Even in case you set resizableActivity = “false”, your app may 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 stage 32, digicam purchasers that question the orientation on foldable gadgets can obtain a price that dynamically adjustments relying on the system/fold state.
  • Do not assume the scale of the inset cannot change. The brand new taskbar is reported to purposes as an inset, and when used with gesture navigation, the taskbar may 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 get hold of entry to shared sources like digicam and microphone.

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

Wouldn’t it’s nice to have a easy element 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 permits you to 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 accurately show the viewfinder. This entails correcting their facet ratio, scale, and rotation. It’s absolutely suitable along with your present Camera2 codebase and repeatedly examined on a number of gadgets.

Let’s see methods to use it.

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


Sync your venture. Now it is best to have the ability to instantly use the CameraViewfinder as some other View. For instance, you may add it to your format file:


As you may see, CameraViewfinder has the identical controls accessible on PreviewView, so you may select totally different Implementation modes and scaling varieties.

Now that the element is a part of the format, you may nonetheless create a CameraCaptureSession, however as an alternative of offering a TextureView or SurfaceView as goal surfaces, use the results of requestSurfaceAsync().

enjoyable startCamera(){
    val previewResolution = Dimension(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 regular
        override enjoyable onFailure(t: Throwable) { /* one thing went fallacious */}
    }, ContextCompat.getMainExecutor(context))

Bonus: optimized layouts for foldable gadgets

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

However if you wish to implement optimized layouts for foldable and twin display gadgets, you may mix CameraViewFinder with the Jetpack WindowManager library to offer distinctive experiences in your customers.

For instance, you may select to keep away from exhibiting full display preview if there’s a hinge in the course of the display, or if the system is in “e-book” or “tabletop” mode. In these situations, you may have the viewfinder in a single portion of the display and the controls on the opposite facet, or you should utilize a part of the display to point out the final photos taken. Creativeness is the restrict!

The pattern app is already optimized for foldable gadgets and you will discover the code to deal with posture adjustments right here. Take a look!


Please enter your comment!
Please enter your name here