Sunday, December 22, 2024

CameraX replace makes twin concurrent digital camera even simpler



CameraX replace makes twin concurrent digital camera even simpler

Posted by Donovan McMurray – Developer Relations Engineer

CameraX, Android’s Jetpack digital camera library, is getting an thrilling replace to its Twin Concurrent Digicam characteristic, making it even simpler to combine this characteristic into your app. This characteristic permits you to stream from 2 completely different cameras on the identical time. The unique model of Twin Concurrent Digicam was launched in CameraX 1.3.0, and it was already an enormous leap in making this characteristic simpler to implement.

Beginning with 1.5.0-alpha01, CameraX will now deal with the composition of the two digital camera streams as effectively. This replace is extra performance, and it doesn’t take away any prior performance neither is it a breaking change to your present Twin Concurrent Digicam code. To inform CameraX to deal with the composition, merely use the new SingleCameraConfig constructor which has a brand new parameter for a CompositionSettings object. Because you’ll be creating 2 SingleCameraConfigs, you need to be per what constructor you utilize.

Nothing has modified in the way in which you verify for concurrent digital camera assist from the prior model of this characteristic. As a reminder, here’s what that code appears to be like like.

// Arrange main and secondary digital camera selectors if supported on machine.
var primaryCameraSelector: CameraSelector? = null
var secondaryCameraSelector: CameraSelector? = null

for (cameraInfos in cameraProvider.availableConcurrentCameraInfos) {
    primaryCameraSelector = cameraInfos.first {
        it.lensFacing == CameraSelector.LENS_FACING_FRONT
    }.cameraSelector
    secondaryCameraSelector = cameraInfos.first {
        it.lensFacing == CameraSelector.LENS_FACING_BACK
    }.cameraSelector

    if (primaryCameraSelector == null || secondaryCameraSelector == null) {
        // If both a main or secondary selector wasn't discovered, reset each
        // to maneuver on to the subsequent listing of CameraInfos.
        primaryCameraSelector = null
        secondaryCameraSelector = null
    } else {
        // If each main and secondary digital camera selectors had been discovered, we are able to
        // conclude the search.
        break
    }
}

if (primaryCameraSelector == null || secondaryCameraSelector == null) {
    // Back and front concurrent digital camera not out there. Deal with accordingly.
}

Right here’s the up to date code snippet displaying learn how to implement picture-in-picture, with the entrance digital camera stream scaled down to suit into the decrease proper nook. On this instance, CameraX handles the composition of the digital camera streams.

// If 2 concurrent digital camera selectors had been discovered, create 2 SingleCameraConfigs
// and compose them in a picture-in-picture structure.
val main = SingleCameraConfig(
    cameraSelectorPrimary,
    useCaseGroup,
    CompositionSettings.Builder()
        .setAlpha(1.0f)
        .setOffset(0.0f, 0.0f)
        .setScale(1.0f, 1.0f)
        .construct(),
    lifecycleOwner);
val secondary = SingleCameraConfig(
    cameraSelectorSecondary,
    useCaseGroup,
    CompositionSettings.Builder()
        .setAlpha(1.0f)
        .setOffset(2 / 3f - 0.1f, -2 / 3f + 0.1f)
        .setScale(1 / 3f, 1 / 3f)
        .construct()
    lifecycleOwner);

// Bind to lifecycle
ConcurrentCamera concurrentCamera =
    cameraProvider.bindToLifecycle(listOf(main, secondary));

You aren’t constrained to a picture-in-picture structure. As an illustration, you would outline a side-by-side structure by setting the offsets and scaling components accordingly. You wish to preserve each dimensions scaled by the identical quantity to keep away from a stretched preview. Right here’s how which may look.

// If 2 concurrent digital camera selectors had been discovered, create 2 SingleCameraConfigs
// and compose them in a picture-in-picture structure.
val main = SingleCameraConfig(
    cameraSelectorPrimary,
    useCaseGroup,
    CompositionSettings.Builder()
        .setAlpha(1.0f)
        .setOffset(0.0f, 0.25f)
        .setScale(0.5f, 0.5f)
        .construct(),
    lifecycleOwner);
val secondary = SingleCameraConfig(
    cameraSelectorSecondary,
    useCaseGroup,
    CompositionSettings.Builder()
        .setAlpha(1.0f)
        .setOffset(0.5f, 0.25f)
        .setScale(0.5f, 0.5f)
        .construct()
    lifecycleOwner);

// Bind to lifecycle
ConcurrentCamera concurrentCamera =
    cameraProvider.bindToLifecycle(listOf(main, secondary));

We’re excited to supply this enchancment to an already developer-friendly characteristic. Really the CameraX means! CompositionSettings in Twin Concurrent Digicam is at the moment in alpha, so in case you have characteristic requests to enhance upon it earlier than the API is locked in, please give us suggestions within the CameraX Dialogue Group. And take a look at the full CameraX 1.5.0-alpha01 launch notes to see what else is new in CameraX.


👇Observe extra 👇
👉 bdphone.com
👉 ultraactivation.com
👉 trainingreferral.com
👉 shaplafood.com
👉 bangladeshi.assist
👉 www.forexdhaka.com
👉 uncommunication.com
👉 ultra-sim.com
👉 forexdhaka.com
👉 ultrafxfund.com
👉 ultractivation.com
👉 bdphoneonline.com

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles