Guaranteeing Function Combos With CameraX 1.5


Guaranteeing Function Combos With CameraX 1.5 1    

Fashionable digital camera apps are outlined by highly effective, overlapping options. Customers anticipate to file video with gorgeous HDR, seize fluid movement at 60 FPS, and get buttery-smooth footage with Preview Stabilization—typically all on the identical time.

As builders, we all know the truth is extra sophisticated. How will you assure {that a} particular gadget really helps a given mixture? Till now, enabling a number of options was typically a bet. You might examine for particular person function help, however combining them may result in undefined conduct or, worse, a failed digital camera session.  This uncertainty forces builders to be conservative, which prevents customers on succesful units from accessing the very best expertise.

As an example, only a few premium units reliably help HDR and 60 FPS video concurrently. Consequently, most apps keep away from enabling each directly to stop a poor person expertise on nearly all of telephones.

To deal with this, we’re introducing Function Group in CameraX – a brand new API designed to remove this guesswork. Now you can question whether or not a particular mixture of options is supported earlier than configuring the digital camera, or just inform CameraX your priorities and let it allow the best-supported mixture for you.

For These New to CameraX

Earlier than we dive into the brand new Function Group API, let’s shortly recap what CameraX is. CameraX is a Jetpack help library, constructed that can assist you make digital camera app improvement simpler. It offers a constant and easy-to-use API floor that works throughout most Android units, with backward-compatibility to Android 6.0 (API degree 23). In case you are new to CameraX, we suggest testing the official documentation and attempting the codelab to get began.

What You Can Construct with the Function Group API

You not have to gamble on function combos and may confidently ship the very best digital camera experiences – like simultaneous HDR and 60 FPS video on succesful {hardware} (e.g. a Pixel 10 Professional) – whereas gracefully avoiding errors on units that may’t help the mixture.

Guaranteeing Function Combos With CameraX 1.5 2
Pixel 10 Professional enabling each HDR and 60 FPS concurrently
Guaranteeing Function Combos With CameraX 1.5 3
On an older gadget the place HDR and 60 FPS cannot run concurrently, solely HDR is enabled whereas the 60 FPS possibility is disabled.

With the Function Group API, you’ll be able to:

  • Construct smarter, dynamic UIs: Intelligently allow or disable settings in your UI based mostly on real-time {hardware} help. For instance, if a person permits HDR, you’ll be able to immediately grey out and disable the 60 FPS possibility if the mixture is not supported on that gadget. 

Guaranteeing Function Combos With CameraX 1.5 4
  • Ship a dependable “Excessive-High quality” mode: Configure the digital camera with a prioritized listing of desired options. CameraX robotically finds and permits the best-supported mixture for any given gadget, guaranteeing an incredible outcome with out complicated, device-specific logic.

  • Forestall digital camera session failures: By verifying help beforehand, you stop the digital camera from trying to configure an unsupported mixture, eliminating a typical supply of crashes and providing a {smooth} person expertise.

How It Works: The Core Parts

The brand new API is centered round key additions to SessionConfig and CameraInfo.

  1. GroupableFeature: This API introduces a set of predefined groupable options, comparable to HDR_HLG10, FPS_60, PREVIEW_STABILIZATION, and IMAGE_ULTRA_HDR. As a consequence of computational limitations, solely a particular set of options could be grouped with the excessive diploma of reliability this API offers. We’re actively working to increase this listing and can introduce help for extra options in future releases.

  • New SessionConfig Parameters: This class, used for beginning a digital camera session, now accepts two new parameters:

    • requiredFeatureGroup: Use this for options that should be supported for the configuration to succeed – supreme for options {that a} person explicitly permits, comparable to toggling an ‘HDR’ change. To make sure a deterministic and constant expertise, the bindToLifecycle name will throw an IllegalArgumentException if the requested mixture will not be supported, relatively than silently ignoring a function request. The CameraInfo#isFeatureGroupSupported API (particulars under) needs to be used to question this outcome beforehand.

    • preferredFeatureGroup: Use this for options which can be fascinating however non-compulsory, for instance whenever you need to implement a default “Excessive-High quality” mode. You present an inventory of your required options ordered in line with your priorities, and CameraX robotically permits the highest-priority mixture that the gadget helps.

    1. CameraInfo#isFeatureGroupSupported(): That is the core question methodology for explicitly checking if a function group is supported, well-suited for offering solely supported function choices to customers in your app UI. You cross it a SessionConfig, and it returns a boolean indicating whether or not the mixture is supported. If you happen to intend to bind a SessionConfig with required options, you need to use this API first to make sure it’s supported. 

    Implementation in Observe

    Let’s take a look at easy methods to use these parts to construct a greater digital camera expertise.

    Situation 1: “Finest Effort” Excessive-High quality Mode

    If you wish to allow the very best options by default, you’ll be able to present a prioritized listing to preferredFeatureGroup. On this instance, we inform CameraX to prioritize HDR, then 60 FPS, and eventually Preview Stabilization. CameraX handles the complexity of checking all attainable combos and selecting the perfect one which the gadget helps.

    As an example, if a tool can deal with HDR and 60 FPS collectively however not with Preview Stabilization, CameraX will allow the primary two and discard the third. This manner, you get the very best expertise with out writing complicated, device-specific checks.

    cameraProvider.bindToLifecycle(

        lifecycleOwner,

        cameraSelector,

        SessionConfig(

            useCases = listOf(preview, videoCapture),

            // The order of options on this listing determines their precedence. 

            // CameraX will allow the best-supported mixture based mostly on these

            // priorities: HDR_HLG10 > FPS_60 > Preview Stabilization.  

            preferredFeatureGroup =

               listOf(HDR_HLG10, FPS_60, PREVIEW_STABILIZATION),

        ).apply {

            // (Non-compulsory) Get a callback with the enabled options

            // to replace your UI. 

            setFeatureSelectionListener { selectedFeatures ->

                updateUiIndicators(selectedFeatures)

            }

        }

    )

    For this code snippet, CameraX will try and allow function combos within the following precedence order, choosing the primary one the gadget totally helps:

    1. HDR + 60 FPS + Preview Stabilization

    2. HDR + 60 FPS

    3. HDR + Preview Stabilization

    4. HDR

    5. 60 FPS + Preview Stabilization

    6. 60 FPS

    7. Preview Stabilization

    8. Not one of the above options

    Situation 2: Constructing a Reactive UI

    To create a UI that responds to person picks and prevents customers from choosing an unsupported function mixture, you’ll be able to question for help instantly. The perform under checks which options are incompatible with the person’s present picks, permitting you to disable the corresponding UI parts.

    /**

     * Returns an inventory of options which can be NOT supported together

     * with the presently chosen options.

     */

    enjoyable getUnsupportedFeatures(

        currentFeatures: Set<GroupableFeature>

    ): Set<GroupableFeature> {

        val unsupportedFeatures = mutableSetOf<GroupableFeature>()

        val appFeatureOptions = setOf(HDR_HLG10, FPS_60, PREVIEW_STABILIZATION)


        // Iterate over each out there function possibility in your app. 

        appFeatureOptions.forEach { featureOption ->

            // Skip options the person has already chosen. 

            if (currentFeatures.accommodates(featureOption)) return@forEach


            // Verify if including this new function is supported. 

            val isSupported = cameraInfo.isFeatureGroupSupported(

                SessionConfig(

                    useCases = useCases,

                    // Verify the brand new function on prime of present ones.

                    requiredFeatureGroup = currentFeatures + featureOption

                )

            )


            if (!isSupported) {

                unsupportedFeatures.add(featureOption)

            }

        }


        return unsupportedFeatures

    }

    You may then wire this logic into your ViewModel or UI controller to react to person enter and re-bind the digital camera with a guaranteed-to-work configuration.

    // Invoked when person turns some function on/off.

    enjoyable onFeatureChange(currentFeatures: Set<GroupableFeature>) {

        // Determine options which can be unsupported with the present choice.

        val unsupportedFeatures = getUnsupportedFeatures(selectedFeatures)


        // Replace app UI in order that customers cannot allow them.

        updateDisabledFeatures(unsupportedFeatures)


        // Bind a session config with the brand new set of options. Since customers are

        // allowed to pick out solely supported options all the time, no have to explicitly

        // examine if function group is supported.

        cameraProvider.bindToLifecycle(

            lifecycleOwner,

            cameraSelector,

            SessionConfig(

                useCases = listOf(preview, videoCapture),

                requiredFeatureGroup = currentFeatures,

            ).apply {

                setFeatureSelectionListener { selectedFeatures ->

                    // Replace UI to let customers know which options at the moment are chosen

                    updateUiIndicators(selectedFeatures)

                }

            }

        )

    }


    To see these ideas in a working utility, you’ll be able to discover our inner check app. It offers a whole implementation of each the “greatest effort” and “reactive UI” eventualities mentioned above.

    Please be aware: This can be a check utility and never an formally supported pattern. Whereas it is an incredible reference for the Function Group API, it has not been polished for manufacturing use.

    Get Began At this time

    The Function Group API removes the anomaly of working with superior digital camera capabilities. By offering a deterministic solution to question for function help, you’ll be able to construct extra highly effective and dependable digital camera apps with confidence.

    The API is on the market as experimental in CameraX 1.5 and is scheduled to turn into totally secure within the 1.6 launch, with extra help and enhancements on the best way.

    To study extra, take a look at the official documentation. We are able to’t wait to see what you create, and we stay up for your suggestions. Please share your ideas and report any points by way of the next channels:

    Related Articles

    LEAVE A REPLY

    Please enter your comment!
    Please enter your name here

    Latest Articles