HomeAndroidAndroid Builders Weblog: Introducing Digicam Viewfinder

Android Builders Weblog: Introducing Digicam Viewfinder



Posted by Francesco Romano, Developer Relations Engineer, Androidhand holding a phoneOver time, Android units have developed to incorporate a wide range of sizes, shapes, and shows, amongst different options. Because the starting, nonetheless, taking photos along with your cellphone has been one of the necessary use instances. Immediately, digital camera capabilities are nonetheless one of many prime causes shoppers buy a cellphone.

As a developer, you wish to leverage digital camera capabilities in your app, so that you resolve 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 digital camera sensor on the display.

So that you go forward and create a CaptureSession utilizing a floor as huge because the display measurement. So long as the display has the identical side ratio because the digital camera sensor output and the machine stays in its pure portrait orientation, every thing ought to be tremendous.

However what occurs whenever you resize the window, unfold your machine, or change the show or orientation? Effectively, most often, the preview might seem stretched, the wrong way up, or incorrectly rotated. And in case you are in a multi-window surroundings, 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 kind components similar to foldable units, and new show modes similar to multi-window and multi-display, you’ll be able to’t assume this will likely be true anymore.

Particularly, let’s have a look at a number of the most necessary issues when growing an app focusing on varied kind components:

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

  • Do not assume your app will dwell in a portrait-shaped window. Requesting a set orientation remains to be supported in Android 13, however now machine producers might have the choice of overriding an app request for a most popular orientation.
  • Do not assume any mounted dimension or side ratio in your app. Even should you set resizableActivity = “false”, your app might nonetheless be utilized in multi-window mode on giant screens (>=600dp).
  • Do not assume a set relationship between the orientation of the display and the digital camera. The Android Compatibility Definition Doc specifies {that a} digital camera picture sensor “MUST be oriented in order that the lengthy dimension of the digital camera aligns with the display’s lengthy dimension.” Beginning with API stage 32, digital camera 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 dimensions 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 digital camera. Whereas your app is in a multi-window state, different apps can acquire entry to shared sources like digital camera and microphone.

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

Wouldn’t 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 permits you to shortly implement digital camera previews with minimal effort. It internally makes use of both a TextureView or SurfaceView to show the digital camera feed, and applies the required transformations on them to appropriately show the viewfinder. This entails correcting their side ratio, scale, and rotation. It’s totally appropriate along with your current Camera2 codebase and constantly examined on a number of units.

Let’s see easy methods to use it.

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

implementationandroidx.digital camera:camera-viewfinder:1.3.0-alpha01″

Sync your venture. Now it’s best to be capable to immediately use the CameraViewfinder as some other View. For instance, you’ll be able to add it to your format file:

<androidx.digital camera.viewfinder.CameraViewfinder
  android:id=“@+id/view_finder”
  app:scaleType=“fitCenter”
  app:implementationMode=“efficiency”
  android:layout_width=“match_parent”
  android:layout_height=“match_parent”/>

As you’ll be able to see, CameraViewfinder has the identical controls out there on PreviewView, so you’ll be able to select completely different Implementation modes and scaling sorts.

Now that the part is a part of the format, you’ll be able to 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 = Dimension(width, top)
    val viewfinderSurfaceRequest =
ViewfinderSurfaceRequest(previewResolution, traits)
    val surfaceListenableFuture =
        cameraViewfinder.requestSurfaceAsync(viewfinderSurfaceRequest)

    Futures.addCallback(surfaceListenableFuture, object : FutureCallback<Floor> {
        override enjoyable onSuccess(floor: Floor) {
            //create a CaptureSession utilizing this floor as traditional
        }
        override enjoyable onFailure(t: Throwable) { /* one thing went fallacious */}
    }, 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’ll be able to mix CameraViewFinder with the Jetpack WindowManager library to offer distinctive experiences in your customers.

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

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

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments