Posted by Francesco Romano, Developer Relations Engineer, AndroidOver time, Android gadgets have advanced to incorporate a wide range of sizes, shapes, and shows, amongst different options. Because the starting, nonetheless, taking footage together with your cellphone has been one of the crucial essential use circumstances. Right this moment, digicam capabilities are nonetheless one of many prime causes shoppers buy a cellphone.
As a developer, you need to leverage digicam capabilities in your app, so that you resolve to undertake the Android Digicam Framework. The primary use case you need to implement is the Preview use case, which exhibits the output of the digicam sensor on the display screen.
So that you go forward and create a CaptureSession utilizing a floor as large because the display screen dimension. So long as the display screen has the identical side ratio because the digicam sensor output and the machine stays in its pure portrait orientation, every thing needs to be high-quality.
However what occurs while you resize the window, unfold your machine, or change the show or orientation? Effectively, normally, the preview could seem stretched, the wrong way up, or incorrectly rotated. And if you’re in a multi-window atmosphere, your app could even crash.
Why does this occur? Due to the implicit assumptions you made whereas creating the CaptureSession.
Traditionally, your app may stay in the identical window for its complete life cycle, however with the provision of latest type elements comparable to foldable gadgets, and new show modes comparable to multi-window and multi-display, you’ll be able to’t assume this will probably be true anymore.
Specifically, let’s examine among the most essential issues when growing an app focusing on varied type elements:
Let’s study some frequent pitfalls to keep away from when growing an app focusing on varied type elements:
- Do not assume your app will stay in a portrait-shaped window. Requesting a hard and fast orientation remains to be supported in Android 13, however now machine producers could have the choice of overriding an app request for a most popular orientation.
- Do not assume any mounted dimension or side ratio on your app. Even should you set resizableActivity = “false”, your app may nonetheless be utilized in multi-window mode on giant screens (>=600dp).
- Do not assume a hard and fast relationship between the orientation of the display screen 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 screen’s lengthy dimension.” Beginning with API stage 32, digicam purchasers that question the orientation on foldable gadgets 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 will 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 sources like digicam and microphone.
Whereas CameraX already handles a lot of the circumstances above, implementing a preview that works in several situations with Camera2 APIs will 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 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 side ratio, scale, and rotation. It’s absolutely appropriate together with your present Camera2 codebase and constantly examined on a number of gadgets.
Let’s see how you can use it.
First, add the dependency in your app-level construct.gradle file:
Sync your venture. Now you must be capable of immediately use the CameraViewfinder as every other View. For instance, you’ll be able to add it to your structure file:
As you’ll be able to see, CameraViewfinder has the identical controls out there on PreviewView, so you’ll be able to select totally different Implementation modes and scaling varieties.
Now that the part is a part of the structure, you’ll be able to nonetheless create a CameraCaptureSession, however as an alternative of offering a TextureView or SurfaceView as goal surfaces, use the results of requestSurfaceAsync().
Bonus: optimized layouts for foldable gadgets
CameraViewFinder is ready-to-use throughout resizable surfaces, configuration modifications, 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 screen gadgets, you’ll be able to mix CameraViewFinder with the Jetpack WindowManager library to supply distinctive experiences on your customers.
For instance, you’ll be able to select to keep away from displaying full display screen preview if there’s a hinge in the midst of the display screen, or if the machine is in “guide” or “tabletop” mode. In these situations, you’ll be able to have the viewfinder in a single portion of the display screen and the controls on the opposite aspect, or you should utilize a part of the display screen to point out the final footage taken. Creativeness is the restrict!
The pattern app is already optimized for foldable gadgets and yow will discover the code to deal with posture modifications right here. Take a look!