aster.cloud aster.cloud
  • /
  • Platforms
    • Public Cloud
    • On-Premise
    • Hybrid Cloud
    • Data
  • Architecture
    • Design
    • Solutions
    • Enterprise
  • Engineering
    • Automation
    • Software Engineering
    • Project Management
    • DevOps
  • Programming
    • Learning
  • Tools
  • About
  • /
  • Platforms
    • Public Cloud
    • On-Premise
    • Hybrid Cloud
    • Data
  • Architecture
    • Design
    • Solutions
    • Enterprise
  • Engineering
    • Automation
    • Software Engineering
    • Project Management
    • DevOps
  • Programming
    • Learning
  • Tools
  • About
aster.cloud aster.cloud
  • /
  • Platforms
    • Public Cloud
    • On-Premise
    • Hybrid Cloud
    • Data
  • Architecture
    • Design
    • Solutions
    • Enterprise
  • Engineering
    • Automation
    • Software Engineering
    • Project Management
    • DevOps
  • Programming
    • Learning
  • Tools
  • About
  • Mobile
  • Programming

Apply Special Effects To Images With The CameraX Extensions API

  • aster.cloud
  • October 12, 2021
  • 5 minute read

Android CameraX was designed to make camera development easier. As CameraX was being developed, camera application developers have shared their passion and enthusiasm with us, and many great ideas have been built into the current API. One great example is the CameraX Extensions API. We’ve recently rearchitected these extensions with input from the developer community, and now with the new ExtensionsManager, you can use these extensions with as little as two lines of code! This post covers how to use the Extensions API in your app.

CameraX Extensions

Android devices contain powerful cameras, with manufacturers devoting a lot of effort to build many cutting-edge features, or special effects, into these camera devices. These powerful features used to be accessible only to the device’s native camera applications. With the CameraX Extensions API, third-party developers can now access these powerful camera features through a common, simple interface.


Partner with aster.cloud
for your next big idea.
Let us know here.



From our partners:

CITI.IO :: Business. Institutions. Society. Global Political Economy.
CYBERPOGO.COM :: For the Arts, Sciences, and Technology.
DADAHACKS.COM :: Parenting For The Rest Of Us.
ZEDISTA.COM :: Entertainment. Sports. Culture. Escape.
TAKUMAKU.COM :: For The Hearth And Home.
ASTER.CLOUD :: From The Cloud And Beyond.
LIWAIWAI.COM :: Intelligence, Inside and Outside.
GLOBALCLOUDPLATFORMS.COM :: For The World's Computing Needs.
FIREGULAMAN.COM :: For The Fire In The Belly Of The Coder.
ASTERCASTER.COM :: Supra Astra. Beyond The Stars.
BARTDAY.COM :: Prosperity For Everyone.

CameraX Extensions scope

Version 1.0.0 of the CameraX Extensions includes some of the most common built-in camera special effects:

  • BOKEH: making foreground people sharper when photos are taken in portrait mode.
  • HDR: taking photos with different Auto Exposure (AE) settings to generate the best result.
  • NIGHT: getting the best still images under low-light situations, typically at night time.
  • FACE RETOUCH: retouching face skin tone, geometry, and so on when taking still images.
  • AUTO: automatically adjusting the final image based on the surrounding scenery.

Let’s look at a couple of example photos taken with and without enabling special effects via CameraX Extension API on an Android phone.

BOKEH mode example

Figure 1. A BOKEH effect is applied to the image on the bottom.

HDR mode example

Figure 2. The image on the bottom contains the HDR mode effect.

NIGHT mode example

Figure 3. The image on the bottom contains the NIGHT mode effect.

The visual difference is clear. You can use the CameraX Extensions API to make these images in your own applications.

Now let’s see how to integrate the CameraX API into your application.

Read More  Canonical Introduces Anbox Cloud – Scalable Android™ In The Cloud

Extensions API

In an existing CameraX app, you can add CameraX Extensions by first including the camera-extensions Jetpack library:

dependencies {
    // CameraX core libraries which version matches the extensions library
    implementation 'androidx.camera:camera-core:1.1.0-alpha08'
    implementation 'androidx.camera:camera-camera2:1.1.0-alpha08'
    implementation 'androidx.camera:camera-lifecycle:1.1.0-alpha08'

    // CameraX Extensions library
    implementation 'androidx.camera:camera-extensions:1.0.0-alpha28'

    // other dependencies
    implementation('androidx.concurrent:concurrent-futures-ktx:1.1.0')
        …
}

Next, integrate the extensions with the following steps:

  1. Acquire the ExtensionsManager instance.
  2. Check whether the target device supports the intended extension mode.
  3. Get an extension-enabled CameraSelector.
  4. Call bindToLifecycle using the extension-enabled CameraSelector.

Acquire the ExtensionsManager instance

The first step is to get an ExtensionsManager instance with its getInstance(Context) API. This API returns a ListenableFuture where we can use await() to get the result in the Kotlin suspend function to avoid blocking the main thread. (Note: using await() on ListenableFuture requires the androidx.concurrent:concurrent-futures-ktx: 1.1.0 dependency.)

// Creates the extensions manager (with Jetpack Concurrent library)
val extensionsManager =  ExtensionsManager.getInstance(context).await()

ExtensionsManager allows you to determine device support and get an extension-enabled CameraSelector for a specific extension mode. Note the following:

  • ExtensionsManager is a process-scoped global resource: only a single ExtensionsManager instance exists within a process.
  • ExtensionsManager always exists: CameraX provides a functioning ExtensionsManager instance regardless of whether the underneath device supports any extension(s).

Check the Extension Mode Availability

With the ExtensionsManager, check for the extension availability using the isExtensionAvailable(CameraProvider, CameraSelector, int) function: it returns true If any camera filtered by the CameraSelector on the device supports the queried extension, false otherwise.

        // Get the list of camera devices to check for extension availability
        val cameraSelector = CameraSelector.DEFAULT_BACK_CAMERA

        // Checks whether bokeh is supported
        if (extensionsManager.isExtensionAvailable(
                cameraProvider,
                cameraSelector,
                ExtensionMode.BOKEH
            )) {
            ...
        }

Get an extension-enabled CameraSelector

Once you know that the extension mode is supported on the device, retrieve an extension-enabled CameraSelector with the getExtensionEnabledCameraSelector(CameraProvider, CameraSelector, int) function. The returned extension-enabled CameraSelector contains all the details about the specified extension mode.

val bokehCameraSelector = extensionsManager
                          .getExtensionEnabledCameraSelector(
                              cameraProvider, cameraSelector, ExtensionMode.BOKEH)

Call bindToLifecycle() using the extension-enabled CameraSelector

The final step is to bind your use cases with the extension-enabled CameraSelector using bindToLifecycle(). The extension-enabled CameraSelector can be used just like a normal CameraSelector, such as with DEFAULT_BACK_CAMERA or DEFAULT_FRONT_CAMERA. CameraX enables the specified extension mode directly on the camera when binding use cases with the extension-enabled CameraSelector. For example, the extension effect is applied onto the preview when Preview is bound, or onto the images captured by the bound ImageCapture.

// Binds use case with the bokeh enabled camera selector
val imageCapture = ImageCapture.Builder().build()
val preview = Preview.Builder().build()
cameraProvider.bindToLifecycle(
                lifecycleOwner,
                bokehCameraSelector,
                imageCapture,
                preview
            )

Extensions API usage sample code

The full code for this extensions API example looks like the following:

fun onCreate() {
    lifecycleScope.launch {
        // Creates the camera provider
        val cameraProvider = ProcessCameraProvider.getInstance(context).await() 

        // Creates the extensions manager (with Jetpack Concurrent library)
        val extensionsManager = 
                ExtensionsManager.getInstance(context).await()

        // Get the list of camera devices to check for extension availability        
        val cameraSelector = CameraSelector.DEFAULT_BACK_CAMERA

        // Checks whether bokeh is supported
        if (extensionsManager.isExtensionAvailable(
                cameraProvider,
                cameraSelector,
                ExtensionMode.BOKEH
            )) {
            // Unbinds all use cases before enabling different extension modes.
            cameraProvider.unbindAll()

            // Gets the bokeh enabled camera selector
            val bokehCameraSelector = extensionsManager
                    .getExtensionEnabledCameraSelector(
                cameraProvider,
                cameraSelector,
                ExtensionMode.BOKEH
            )

            // Binds use case with the bokeh enabled camera selector
            val imageCapture = ImageCapture.Builder().build()
            val preview = Preview.Builder().build()
            cameraProvider.bindToLifecycle(
                lifecycleOwner,
                bokehCameraSelector,
                imageCapture,
                preview
            )
        }
    }
}

Extensions API dependencies on core modules

The CameraX Extensions API is implemented in the camera-extensions library and has a dependency on the CameraX core modules (core,camera2,lifecycle). When using CameraX Extensions, be sure to use the version from the same release package as the CameraX core modules that you’re using. For example, to use the camera-extensions:1.0.0-alpha28, you must also include version 1.0.0-alpha08 for the camera-lifecycle, camera-core, camera-camera2into application’s dependency list, as they were released in the same package on August 18, 2021.

Read More  Google I/O 2019 | Zero to App: Live Coding a Cross-Platform App on Firebase

Extensions supported devices

To use the CameraX extensions API, device manufacturers need to implement CameraX Vendor Extensions interfaces. You can find a partial list of the CameraX Extensions API supported devices on the CameraX devices page. Note that this is not an exhaustive list. If your device is listed but the availability checks return false, you might need to update your device to the latest ROM version from your manufacturer.

In addition to the supported devices list, starting with Android 12, you can also check the Android property ro.camerax.extensions.enabled to determine whether the device supports CameraX Extensions.

Legacy Extensions API removal

CameraX released a legacy Extensions API in August 2019. This legacy Extensions API provided extender classes which are needed to apply extensions-related configuration onto each Preview and ImageCapture use case. The legacy extender design might cause the developers to miss enabling extension mode on either Preview or ImageCapture and could cause unintended behavior.

The new CameraX Extensions library was introduced in 1.0.0-alpha26. The newer Extensions API switches extension binding from use cases to the target camera and are much easier to use. Be sure to migrate to take advantage of the new Extensions API.

We especially appreciate our awesome Android camera developers and device manufacturers that help to make the CameraX Extensions API happen! If you’d like to stay in touch with the latest CameraX development, join the Android CameraX Discussion Group.

Other references

  • CameraX Extensions API guides
  • Extensions API Reference
  • Extensions Test app
  • CameraX release Note
  • Getting Started with CameraX
  • CameraX Github Samples
Read More  Independent Versioning Of Jetpack Compose Libraries

 

 

By Charcoal Chen
Source Medium


For enquiries, product placements, sponsorships, and collaborations, connect with us at [email protected]. We'd love to hear from you!

Our humans need coffee too! Your support is highly appreciated, thank you!

aster.cloud

Related Topics
  • Android
  • CameraX
  • ExtensionsManager
You May Also Like
View Post
  • Architecture
  • Data
  • Engineering
  • People
  • Programming
  • Software Engineering
  • Technology
  • Work & Jobs

Predictions: Top 25 Careers Likely In High Demand In The Future

  • June 6, 2023
View Post
  • Programming
  • Software Engineering
  • Technology

Build a Python App to Alert You When Asteroids Are Close to Earth

  • May 22, 2023
View Post
  • Programming

Illuminating Interactions: Visual State In Jetpack Compose

  • May 20, 2023
View Post
  • Computing
  • Data
  • Programming
  • Software
  • Software Engineering

The Top 10 Data Interchange Or Data Exchange Format Used Today

  • May 11, 2023
View Post
  • Gears
  • Mobile
  • Technology

Apple Watch Pride Edition Celebrates The LGBTQ+ Community

  • May 10, 2023
View Post
  • Architecture
  • Programming
  • Public Cloud

From Receipts To Riches: Save Money W/ Google Cloud & Supermarket Bills – Part 1

  • May 8, 2023
View Post
  • Programming
  • Public Cloud

3 New Ways To Authorize Users To Your Private Workloads On Cloud Run

  • May 4, 2023
View Post
  • Programming
  • Public Cloud

Buffer HTTP Requests With Cloud Tasks

  • May 4, 2023

Stay Connected!
LATEST
  • college-of-cardinals-2025 1
    The Definitive Who’s Who of the 2025 Papal Conclave
    • May 7, 2025
  • conclave-poster-black-smoke 2
    The World Is Revalidating Itself
    • May 6, 2025
  • 3
    Conclave: How A New Pope Is Chosen
    • April 25, 2025
  • Getting things done makes her feel amazing 4
    Nurturing Minds in the Digital Revolution
    • April 25, 2025
  • 5
    AI is automating our jobs – but values need to change if we are to be liberated by it
    • April 17, 2025
  • 6
    Canonical Releases Ubuntu 25.04 Plucky Puffin
    • April 17, 2025
  • 7
    United States Army Enterprise Cloud Management Agency Expands its Oracle Defense Cloud Services
    • April 15, 2025
  • 8
    Tokyo Electron and IBM Renew Collaboration for Advanced Semiconductor Technology
    • April 2, 2025
  • 9
    IBM Accelerates Momentum in the as a Service Space with Growing Portfolio of Tools Simplifying Infrastructure Management
    • March 27, 2025
  • 10
    Tariffs, Trump, and Other Things That Start With T – They’re Not The Problem, It’s How We Use Them
    • March 25, 2025
about
Hello World!

We are aster.cloud. We’re created by programmers for programmers.

Our site aims to provide guides, programming tips, reviews, and interesting materials for tech people and those who want to learn in general.

We would like to hear from you.

If you have any feedback, enquiries, or sponsorship request, kindly reach out to us at:

[email protected]
Most Popular
  • 1
    IBM contributes key open-source projects to Linux Foundation to advance AI community participation
    • March 22, 2025
  • 2
    Co-op mode: New partners driving the future of gaming with AI
    • March 22, 2025
  • 3
    Mitsubishi Motors Canada Launches AI-Powered “Intelligent Companion” to Transform the 2025 Outlander Buying Experience
    • March 10, 2025
  • PiPiPi 4
    The Unexpected Pi-Fect Deals This March 14
    • March 13, 2025
  • Nintendo Switch Deals on Amazon 5
    10 Physical Nintendo Switch Game Deals on MAR10 Day!
    • March 9, 2025
  • /
  • Technology
  • Tools
  • About
  • Contact Us

Input your search keywords and press Enter.