CameraX एक्सटेंशन एपीआई

CameraX, एक्सटेंशन को ऐक्सेस करने के लिए Extensions API उपलब्ध कराता है. डिवाइस बनाने वाली कंपनियों ने इन एक्सटेंशन को अलग-अलग Android डिवाइसों पर लागू किया है. जैसे, नाइट, एचडीआर, ऑटो, बोकेह, और चेहरे की फ़ोटो क्वालिटी में सुधार. एक्सटेंशन के साथ काम करने वाले डिवाइसों की सूची देखने के लिए, एक्सटेंशन के साथ काम करने वाले डिवाइस पर जाएं.

एक्सटेंशन का आर्किटेक्चर

यहां दी गई इमेज में, कैमरा एक्सटेंशन का आर्किटेक्चर दिखाया गया है.

CameraX ऐप्लिकेशन, CameraX Extensions API का इस्तेमाल करता है. यह एपीआई, डिवाइस के कैमरा एक्सटेंशन के साथ कम्यूनिकेट करता है.
पहली इमेज. Camera Extensions का आर्किटेक्चर

CameraX ऐप्लिकेशन, CameraX Extensions API के ज़रिए एक्सटेंशन इस्तेमाल करता है. एपीआई, यह क्वेरी करता है कि कौनसे एक्सटेंशन उपलब्ध हैं. साथ ही, एक्सटेंशन कैमरा सेशन को कॉन्फ़िगर करता है और डिवाइस के कैमरा एक्सटेंशन के साथ कम्यूनिकेट करता है.

कोई एक्सटेंशन चालू करना

किसी एक्सटेंशन को चालू करने के लिए, यह तरीका अपनाएं:

  1. ExtensionsManager इंस्टेंस पाएं.
  2. देखें कि कैमरे के लिए एक्सटेंशन मोड उपलब्ध है या नहीं. इसके लिए, isExtensionAvailable() पर जाएं. हर डिवाइस पर हर मोड काम नहीं करता.
  3. एक्सटेंशन मोड और Preview और ImageCapture के इस्तेमाल के उदाहरणों के साथ ExtensionSessionConfig बनाएं.
  4. सेशन के कॉन्फ़िगरेशन को ProcessCameraProvider.bindToLifecycle() के साथ बाइंड करें. एक्सटेंशन मोड, Preview और ImageCapture के इस्तेमाल के उदाहरणों पर लागू होता है.

Kotlin

suspend fun bindNightMode(
    context: Context,
    lifecycleOwner: LifecycleOwner,
    surfaceProvider: Preview.SurfaceProvider,
) {
    val cameraProvider = ProcessCameraProvider.awaitInstance(context)
    val extensionsManager =
        ExtensionsManager.getInstance(context, cameraProvider)
    val cameraSelector = CameraSelector.DEFAULT_BACK_CAMERA

    // Not every device supports every mode, and binding a mode that the
    // camera doesn't support fails. Check availability first.
    val isNightAvailable = extensionsManager.isExtensionAvailable(
        cameraSelector, ExtensionMode.NIGHT
    )
    if (!isNightAvailable) {
        // Bind a regular session instead (not shown) so that the camera
        // still works on devices without the Night extension.
        return
    }

    val preview = Preview.Builder().build()
    preview.setSurfaceProvider(surfaceProvider)
    val imageCapture = ImageCapture.Builder().build()

    // The session config ties the extension mode to the use cases, so
    // CameraX applies Night mode to both Preview and ImageCapture.
    val sessionConfig = ExtensionSessionConfig(
        ExtensionMode.NIGHT,
        extensionsManager,
        preview,
        imageCapture,
    )

    cameraProvider.bindToLifecycle(
        lifecycleOwner, cameraSelector, sessionConfig
    )
}

Java

void bindNightMode(Context context, LifecycleOwner lifecycleOwner,
        Preview.SurfaceProvider surfaceProvider) {
    Executor mainExecutor = ContextCompat.getMainExecutor(context);
    ListenableFuture<ProcessCameraProvider> providerFuture =
            ProcessCameraProvider.getInstance(context);

    // Both the camera provider and ExtensionsManager initialize
    // asynchronously. Bind only after both are ready.
    providerFuture.addListener(() -> {
        try {
            ProcessCameraProvider cameraProvider = providerFuture.get();
            ListenableFuture<ExtensionsManager> managerFuture =
                    ExtensionsManager.getInstanceAsync(
                            context, cameraProvider);
            managerFuture.addListener(() -> {
                try {
                    bindNightModeSession(managerFuture.get(),
                            cameraProvider, lifecycleOwner,
                            surfaceProvider);
                } catch (ExecutionException | InterruptedException e) {
                    Log.e(TAG, "Failed to get ExtensionsManager", e);
                }
            }, mainExecutor);
        } catch (ExecutionException | InterruptedException e) {
            Log.e(TAG, "Failed to get the camera provider", e);
        }
    }, mainExecutor);
}

void bindNightModeSession(ExtensionsManager extensionsManager,
        ProcessCameraProvider cameraProvider,
        LifecycleOwner lifecycleOwner,
        Preview.SurfaceProvider surfaceProvider) {
    CameraSelector cameraSelector = CameraSelector.DEFAULT_BACK_CAMERA;

    // Not every device supports every mode, and binding a mode that the
    // camera doesn't support fails. Check availability first.
    if (!extensionsManager.isExtensionAvailable(
            cameraSelector, ExtensionMode.NIGHT)) {
        // Bind a regular session instead (not shown) so that the camera
        // still works on devices without the Night extension.
        return;
    }

    Preview preview = new Preview.Builder().build();
    preview.setSurfaceProvider(surfaceProvider);
    ImageCapture imageCapture = new ImageCapture.Builder().build();

    // The session config ties the extension mode to the use cases, so
    // CameraX applies Night mode to both Preview and ImageCapture.
    ExtensionSessionConfig sessionConfig = new ExtensionSessionConfig(
            ExtensionMode.NIGHT, extensionsManager, preview, imageCapture);

    cameraProvider.bindToLifecycle(
            lifecycleOwner, cameraSelector, sessionConfig);
}

इस्तेमाल के उदाहरण

यहां दी गई टेबल में बताया गया है कि एक्सटेंशन चालू होने पर, किन कामों के लिए Gemini का इस्तेमाल किया जा सकता है:

इस्तेमाल का उदाहरण कोई एक्सटेंशन चालू होने पर
Preview, ImageCapture इस्तेमाल के लिए उपलब्ध है. एक्सटेंशन मोड लागू किया गया है.
VideoCapture इस्तेमाल के लिए उपलब्ध है. VideoCapture, शेयर की गई Preview स्ट्रीम को रिकॉर्ड करता है.
ImageAnalysis समर्थित नहीं.

यह जांच करना कि कैमरा, सेशन कॉन्फ़िगरेशन के साथ काम करता है या नहीं

अगर आपको इस्तेमाल के और उदाहरण जोड़ने हैं, जैसे कि VideoCapture या अन्य सुविधाएं सेट करनी हैं, तो बाइंड करने से पहले यह देख लें कि कैमरा पूरे कॉम्बिनेशन के साथ काम करता है या नहीं:

  1. उसी CameraSelector और मोड का इस्तेमाल करके, isExtensionAvailable() को कॉल करें.
  2. ExtensionSessionConfig को ProcessCameraProvider.getCameraInfo() को पास करें. यह उस कैमरे का CameraInfo दिखाता है जिसे bindToLifecycle() उस कॉन्फ़िगरेशन के लिए बाइंड करेगा.
  3. CameraInfo पर isSessionConfigSupported() को कॉल करें.

अगर इनमें से कोई भी जांच पूरी नहीं होती है, तो इसके बजाय सामान्य SessionConfig को बाइंड करें.

Kotlin

fun createSessionConfig(
    mode: Int,
    vararg useCases: UseCase,
): SessionConfig {
    // getCameraInfo() throws if no camera matches the extension, so check
    // that the mode is available first.
    if (extensionsManager.isExtensionAvailable(cameraSelector, mode)) {
        val extensionConfig =
            ExtensionSessionConfig(mode, extensionsManager, *useCases)
        // Check the camera that would actually be bound for this config,
        // and verify that it supports these use cases together with the
        // extension, for example VideoCapture.
        if (cameraProvider.getCameraInfo(cameraSelector, extensionConfig)
                .isSessionConfigSupported(extensionConfig)
        ) {
            return extensionConfig
        }
    }
    // The same use cases can still run in a regular session, so the app
    // keeps working without the extension.
    return SessionConfig(*useCases)
}

cameraProvider.bindToLifecycle(
    lifecycleOwner,
    cameraSelector,
    createSessionConfig(
        ExtensionMode.NIGHT, preview, imageCapture, videoCapture
    ),
)

Java

SessionConfig createSessionConfig(int mode, UseCase... useCases) {
    // getCameraInfo() throws if no camera matches the extension, so check
    // that the mode is available first.
    if (extensionsManager.isExtensionAvailable(cameraSelector, mode)) {
        ExtensionSessionConfig extensionConfig =
                new ExtensionSessionConfig(
                        mode, extensionsManager, useCases);
        // Check the camera that would actually be bound for this config,
        // and verify that it supports these use cases together with the
        // extension, for example VideoCapture.
        if (cameraProvider.getCameraInfo(cameraSelector, extensionConfig)
                .isSessionConfigSupported(extensionConfig)) {
            return extensionConfig;
        }
    }
    // The same use cases can still run in a regular session, so the app
    // keeps working without the extension.
    return new SessionConfig(useCases);
}

cameraProvider.bindToLifecycle(lifecycleOwner, cameraSelector,
        createSessionConfig(
                ExtensionMode.NIGHT, preview, imageCapture, videoCapture));

किसी एक्सटेंशन को बंद करना या उसे किसी दूसरे एक्सटेंशन से बदलना

किसी दूसरे एक्सटेंशन मोड पर स्विच करने के लिए, नए ExtensionSessionConfig के साथ bindToLifecycle() को फिर से कॉल करें. एक्सटेंशन बंद करने के लिए, इस्तेमाल के एक जैसे मामलों के साथ एक सामान्य SessionConfig को बाइंड करें. आपको पहले unbind() या unbindAll() को कॉल करने की ज़रूरत नहीं है: पहले से बाउंड सेशन कॉन्फ़िगरेशन और उसके इस्तेमाल के उदाहरण अपने-आप अनबाउंड हो जाते हैं.

Kotlin

// Binding a new session config unbinds the previous one and its use
// cases, so you don't need to call unbindAll() before switching modes.
cameraProvider.bindToLifecycle(
    lifecycleOwner,
    cameraSelector,
    ExtensionSessionConfig(
        ExtensionMode.NIGHT, extensionsManager, preview, imageCapture
    ),
)

// A regular SessionConfig with the same use cases turns the extension off
// while Preview and ImageCapture keep working.
cameraProvider.bindToLifecycle(
    lifecycleOwner,
    cameraSelector,
    SessionConfig(preview, imageCapture),
)

Java

// Binding a new session config unbinds the previous one and its use
// cases, so you don't need to call unbindAll() before switching modes.
cameraProvider.bindToLifecycle(lifecycleOwner, cameraSelector,
        new ExtensionSessionConfig(ExtensionMode.NIGHT,
                extensionsManager, preview, imageCapture));

// A regular SessionConfig with the same use cases turns the extension off
// while Preview and ImageCapture keep working.
cameraProvider.bindToLifecycle(lifecycleOwner, cameraSelector,
        new SessionConfig(preview, imageCapture));

कैप्चर करने के अनुभव को बेहतर बनाना

नाइट और एचडीआर जैसे कुछ एक्सटेंशन मोड में, किसी स्टिल इमेज को प्रोसेस करने में कुछ सेकंड लग सकते हैं. इंतज़ार के दौरान उपयोगकर्ताओं को जानकारी देने के लिए, पोस्टव्यू और कैप्चर प्रोग्रेस का इस्तेमाल करें. ऐसा तब करें, जब डिवाइस पर ये सुविधाएं काम करती हों:

  • पोस्टव्यू, कम रिज़ॉल्यूशन वाली इमेज को जल्दी डिलीवर करता है. इसे तब तक दिखाया जा सकता है, जब तक फ़ाइनल इमेज तैयार न हो जाए.
  • कैप्चर प्रोसेस प्रोग्रेस, प्रोसेसिंग की प्रोग्रेस को 0 से 100 तक रिपोर्ट करती है. इसे प्रोग्रेस इंडिकेटर के तौर पर दिखाया जा सकता है.

बाइंड करने से पहले, ImageCapture.Builder.setPostviewEnabled() की मदद से पोस्टव्यू चालू करें. अगर कैमरा पोस्टव्यू की सुविधा के साथ काम नहीं करता है, तो पोस्टव्यू इमेज जनरेट नहीं होती. बाइंड करने के बाद, बाइंड किए गए कैमरे के CameraInfo के साथ ImageCapture.getImageCaptureCapabilities() का इस्तेमाल करें, ताकि isPostviewSupported() और isCaptureProcessProgressSupported() की जांच की जा सके. साथ ही, मैच करने के लिए अपना यूज़र इंटरफ़ेस (यूआई) सेट अप करें.

Kotlin

// Postview must be configured before binding. If the camera doesn't
// support postview, enabling it has no effect.
val imageCapture = ImageCapture.Builder()
    .setPostviewEnabled(true)
    .build()

val camera = cameraProvider.bindToLifecycle(
    lifecycleOwner,
    cameraSelector,
    ExtensionSessionConfig(
        ExtensionMode.NIGHT, extensionsManager, preview, imageCapture
    ),
)

// Postview and progress support depend on the extension-enabled camera,
// so query the capabilities of the bound camera.
val capabilities =
    ImageCapture.getImageCaptureCapabilities(camera.cameraInfo)
setUpCaptureUi(
    showPostview = capabilities.isPostviewSupported,
    showProgress = capabilities.isCaptureProcessProgressSupported,
)

// Extension captures can take several seconds. Progress updates and an
// early postview image show users that the capture is being processed.
val results = imageCapture.takePicture(
    outputFileOptions,
    onCaptureProcessProgressed = { progress -> updateProgress(progress) },
    onPostviewBitmapAvailable = { bitmap -> showPostview(bitmap) },
)
// Processing is done. Replace the postview with the final image.
showFinalImage(results.savedUri)

Java

// Postview must be configured before binding. If the camera doesn't
// support postview, enabling it has no effect.
ImageCapture imageCapture = new ImageCapture.Builder()
        .setPostviewEnabled(true)
        .build();

Camera camera = cameraProvider.bindToLifecycle(
        lifecycleOwner, cameraSelector,
        new ExtensionSessionConfig(ExtensionMode.NIGHT,
                extensionsManager, preview, imageCapture));

// Postview and progress support depend on the extension-enabled camera,
// so query the capabilities of the bound camera.
ImageCaptureCapabilities capabilities =
        ImageCapture.getImageCaptureCapabilities(camera.getCameraInfo());
setUpCaptureUi(
        capabilities.isPostviewSupported(),
        capabilities.isCaptureProcessProgressSupported());

// Extension captures can take several seconds. Progress updates and an
// early postview image show users that the capture is being processed.
imageCapture.takePicture(outputFileOptions, mainExecutor,
        new ImageCapture.OnImageSavedCallback() {
            @Override
            public void onCaptureProcessProgressed(int progress) {
                updateProgress(progress);
            }

            @Override
            public void onPostviewBitmapAvailable(@NonNull Bitmap bitmap) {
                showPostview(bitmap);
            }

            @Override
            public void onImageSaved(
                    @NonNull ImageCapture.OutputFileResults results) {
                // Processing is done. Replace the postview with the final
                // image.
                showFinalImage(results.getSavedUri());
            }

            @Override
            public void onError(@NonNull ImageCaptureException exception) {
                Log.e(TAG, "Capture failed", exception);
            }
        });

अल्ट्रा एचडीआर इमेज कैप्चर करना

कुछ डिवाइसों पर, एक्सटेंशन चालू होने पर भी अल्ट्रा एचडीआर इमेज कैप्चर की जा सकती हैं. बाइंडिंग से पहले, आउटपुट फ़ॉर्मैट सेट करना ज़रूरी है. अगर फ़ॉर्मैट काम नहीं करता है, तो बाइंडिंग नहीं हो पाती है. इसलिए, पहले यह देखें कि डिवाइस पर एक्सटेंशन काम करता है या नहीं: isExtensionAvailable() से true मिलने के बाद, getCameraInfo() और ExtensionSessionConfig का इस्तेमाल करके, एक्सटेंशन की सुविधा वाले कैमरे का CameraInfo पाएं. इसके बाद, देखें कि getSupportedOutputFormats() में OUTPUT_FORMAT_JPEG_ULTRA_HDR शामिल है या नहीं.

Kotlin

// Supported output formats can differ when an extension is enabled, so
// query them from the extension-enabled camera. isExtensionAvailable()
// must have returned true for this selector and mode first; otherwise,
// getCameraInfo() throws.
val extensionCameraInfo = cameraProvider.getCameraInfo(
    cameraSelector,
    ExtensionSessionConfig(ExtensionMode.NIGHT, extensionsManager),
)
val supportedFormats = ImageCapture
    .getImageCaptureCapabilities(extensionCameraInfo)
    .supportedOutputFormats

// Binding fails if the output format isn't supported, so request Ultra HDR
// only when the extension-enabled camera supports it.
val imageCapture = ImageCapture.Builder().apply {
    if (ImageCapture.OUTPUT_FORMAT_JPEG_ULTRA_HDR in supportedFormats) {
        setOutputFormat(ImageCapture.OUTPUT_FORMAT_JPEG_ULTRA_HDR)
    }
}.build()

cameraProvider.bindToLifecycle(
    lifecycleOwner,
    cameraSelector,
    ExtensionSessionConfig(
        ExtensionMode.NIGHT, extensionsManager, preview, imageCapture
    ),
)

Java

// Supported output formats can differ when an extension is enabled, so
// query them from the extension-enabled camera. isExtensionAvailable()
// must have returned true for this selector and mode first; otherwise,
// getCameraInfo() throws.
CameraInfo extensionCameraInfo = cameraProvider.getCameraInfo(
        cameraSelector,
        new ExtensionSessionConfig(ExtensionMode.NIGHT, extensionsManager));
Set<Integer> supportedFormats =
        ImageCapture.getImageCaptureCapabilities(extensionCameraInfo)
                .getSupportedOutputFormats();

// Binding fails if the output format isn't supported, so request Ultra HDR
// only when the extension-enabled camera supports it.
ImageCapture.Builder builder = new ImageCapture.Builder();
if (supportedFormats.contains(ImageCapture.OUTPUT_FORMAT_JPEG_ULTRA_HDR)) {
    builder.setOutputFormat(ImageCapture.OUTPUT_FORMAT_JPEG_ULTRA_HDR);
}
ImageCapture imageCapture = builder.build();

cameraProvider.bindToLifecycle(lifecycleOwner, cameraSelector,
        new ExtensionSessionConfig(ExtensionMode.NIGHT,
                extensionsManager, preview, imageCapture));

एक्सटेंशन को मॉनिटर करना और उसमें बदलाव करना

बाइंड करने के बाद, एक्सटेंशन को मॉनिटर और अडजस्ट करने के लिए, बाइंड किए गए कैमरे के साथ ExtensionsManager.getCameraExtensionsInfo() और ExtensionsManager.getCameraExtensionsControl() का इस्तेमाल करें.

एक्सटेंशन की स्ट्रेंथ अडजस्ट करना

अगर isExtensionStrengthAvailable(), true दिखाता है, तो 0 से 100 के बीच की वैल्यू के साथ setExtensionStrength() को कॉल करें. 0 वैल्यू डालने पर, एक्सटेंशन प्रोसेसिंग लागू नहीं होती. साथ ही, 100 डिफ़ॉल्ट वैल्यू होती है. स्ट्रेंथ से क्या कंट्रोल किया जाता है, यह मोड पर निर्भर करता है. उदाहरण के लिए, नाइट मोड में इससे यह कंट्रोल किया जाता है कि कितनी इमेज को फ़्यूज़ किया जाए और फ़ाइनल इमेज कितनी ब्राइट हो. मौजूदा वैल्यू के लिए, getExtensionStrength() देखें.

Kotlin

val camera = cameraProvider.bindToLifecycle(
    lifecycleOwner,
    cameraSelector,
    ExtensionSessionConfig(
        ExtensionMode.NIGHT, extensionsManager, preview, imageCapture
    ),
)
// Pass the bound extension-enabled camera. For a camera without an
// extension, strength isn't available and the control is null.
val extensionsInfo =
    extensionsManager.getCameraExtensionsInfo(camera.cameraInfo)
val extensionsControl =
    extensionsManager.getCameraExtensionsControl(camera.cameraControl)

// Not every device lets apps adjust the strength, so check support before
// showing a strength control.
if (extensionsInfo.isExtensionStrengthAvailable) {
    // For example, connect this to a slider in your UI.
    extensionsControl?.setExtensionStrength(50)
}

Java

Camera camera = cameraProvider.bindToLifecycle(
        lifecycleOwner, cameraSelector,
        new ExtensionSessionConfig(ExtensionMode.NIGHT,
                extensionsManager, preview, imageCapture));
// Pass the bound extension-enabled camera. For a camera without an
// extension, strength isn't available and the control is null.
CameraExtensionsInfo extensionsInfo =
        extensionsManager.getCameraExtensionsInfo(camera.getCameraInfo());
CameraExtensionsControl extensionsControl =
        extensionsManager.getCameraExtensionsControl(
                camera.getCameraControl());

// Not every device lets apps adjust the strength, so check support before
// showing a strength control.
if (extensionsInfo.isExtensionStrengthAvailable()
        && extensionsControl != null) {
    // For example, connect this to a slider in your UI.
    extensionsControl.setExtensionStrength(50);
}

मौजूदा एक्सटेंशन मोड दिखाएं

ExtensionMode.AUTO, सीन के हिसाब से नाइट या एचडीआर जैसे मोड पर स्विच कर सकता है. अगर isCurrentExtensionModeAvailable() जवाब में true दिखाता है, तो getCurrentExtensionMode() को मॉनिटर करके उपयोगकर्ताओं को दिखाएं कि कौनसा मोड चालू है.

Kotlin

val camera = cameraProvider.bindToLifecycle(
    lifecycleOwner,
    cameraSelector,
    ExtensionSessionConfig(
        ExtensionMode.AUTO, extensionsManager, preview, imageCapture
    ),
)
val extensionsInfo =
    extensionsManager.getCameraExtensionsInfo(camera.cameraInfo)

// In AUTO mode, the active mode can change with the scene, for example to
// NIGHT in low light. The LiveData is null when the device doesn't report
// the current mode.
if (extensionsInfo.isCurrentExtensionModeAvailable) {
    extensionsInfo.currentExtensionMode?.observe(lifecycleOwner) { mode ->
        showActiveModeBadge(mode)
    }
}

Java

Camera camera = cameraProvider.bindToLifecycle(
        lifecycleOwner, cameraSelector,
        new ExtensionSessionConfig(ExtensionMode.AUTO,
                extensionsManager, preview, imageCapture));
CameraExtensionsInfo extensionsInfo =
        extensionsManager.getCameraExtensionsInfo(camera.getCameraInfo());

// In AUTO mode, the active mode can change with the scene, for example to
// NIGHT in low light. The LiveData is null when the device doesn't report
// the current mode.
LiveData<Integer> currentMode = extensionsInfo.getCurrentExtensionMode();
if (extensionsInfo.isCurrentExtensionModeAvailable()
        && currentMode != null) {
    currentMode.observe(lifecycleOwner, this::showActiveModeBadge);
}

नाइट मोड का सुझाव देना

नाइट मोड इंडिकेटर, आपके ऐप्लिकेशन को बताता है कि सीन में इतना अंधेरा है कि नाइट एक्सटेंशन से मदद मिलेगी. इससे, उपयोगकर्ता को नाइट मोड पर स्विच करने का सुझाव दिया जा सकता है. यह इंडिकेटर तब दिखता है, जब कैमरा किसी एक्सटेंशन के बिना चल रहा हो. यह देखने के लिए कि यह सुविधा काम करती है या नहीं, CameraInfo.isNightModeIndicatorSupported() का इस्तेमाल करें. इसके बाद, CameraInfo.getNightModeIndicator() को देखें. जब वैल्यू NightModeIndicator.RECOMMENDED हो, तो कोई सुझाव दिखाएं. अगर उपयोगकर्ता स्वीकार करता है, तो ExtensionSessionConfig को ExtensionMode.NIGHT से बाइंड करें.

नाइट मोड इंडिकेटर के लिए, CameraX 1.7.0 या इसके बाद का वर्शन और Android 16 (एपीआई लेवल 36) या इसके बाद के वर्शन पर चलने वाला डिवाइस ज़रूरी है. isNightModeIndicatorSupported(), निचले एपीआई लेवल पर false दिखाता है.

Kotlin

// The indicator is reported while a regular (non-extension) session is
// running with Preview, so the app can suggest Night mode only when the
// scene is dark enough to need it.
val cameraInfo = camera.cameraInfo

// Don't suggest a mode that the camera can't bind.
val isNightAvailable = extensionsManager.isExtensionAvailable(
    cameraSelector, ExtensionMode.NIGHT
)

// If the indicator isn't supported, its value is always UNKNOWN.
if (cameraInfo.isNightModeIndicatorSupported && isNightAvailable) {
    cameraInfo.nightModeIndicator.observe(lifecycleOwner) { indicator ->
        showNightModeSuggestion(
            visible = indicator == NightModeIndicator.RECOMMENDED
        )
    }
}

fun onNightModeSuggestionAccepted() {
    // Binding the extension session config replaces the regular session.
    cameraProvider.bindToLifecycle(
        lifecycleOwner,
        cameraSelector,
        ExtensionSessionConfig(
            ExtensionMode.NIGHT, extensionsManager, preview, imageCapture
        ),
    )
}

Java

// The indicator is reported while a regular (non-extension) session is
// running with Preview, so the app can suggest Night mode only when the
// scene is dark enough to need it.
CameraInfo cameraInfo = camera.getCameraInfo();

// Don't suggest a mode that the camera can't bind.
boolean isNightAvailable = extensionsManager.isExtensionAvailable(
        cameraSelector, ExtensionMode.NIGHT);

// If the indicator isn't supported, its value is always UNKNOWN.
if (cameraInfo.isNightModeIndicatorSupported() && isNightAvailable) {
    cameraInfo.getNightModeIndicator().observe(lifecycleOwner,
            indicator -> showNightModeSuggestion(
                    indicator == NightModeIndicator.RECOMMENDED));
}

void onNightModeSuggestionAccepted() {
    // Binding the extension session config replaces the regular session.
    cameraProvider.bindToLifecycle(lifecycleOwner, cameraSelector,
            new ExtensionSessionConfig(ExtensionMode.NIGHT,
                    extensionsManager, preview, imageCapture));
}

डिपेंडेंसी

CameraX Extensions API को camera-extensions लाइब्रेरी में लागू किया गया है. एक्सटेंशन, CameraX के कोर मॉड्यूल (core, camera2, और lifecycle) पर निर्भर करते हैं. सभी androidx.camera लाइब्रेरी के लिए एक ही वर्शन का इस्तेमाल करें.

Kotlin

dependencies {
    val camerax_version = "1.7.0"
    implementation("androidx.camera:camera-core:${camerax_version}")
    implementation("androidx.camera:camera-camera2:${camerax_version}")
    implementation("androidx.camera:camera-lifecycle:${camerax_version}")
    // The CameraX Extensions library
    implementation("androidx.camera:camera-extensions:${camerax_version}")
}

Groovy

dependencies {
    def camerax_version = "1.7.0"
    implementation "androidx.camera:camera-core:${camerax_version}"
    implementation "androidx.camera:camera-camera2:${camerax_version}"
    implementation "androidx.camera:camera-lifecycle:${camerax_version}"
    // The CameraX Extensions library
    implementation "androidx.camera:camera-extensions:${camerax_version}"
}

अब काम न करने वाले कैमरा सिलेक्टर एपीआई से माइग्रेट करना

ExtensionsManager.getExtensionEnabledCameraSelector() को CameraX 1.7.0-alpha02 के बाद से बंद कर दिया गया है. इसके बजाय, ExtensionSessionConfig का इस्तेमाल करें, जैसा कि इस टेबल में दिखाया गया है:

अब इस्तेमाल में नहीं है प्रतिस्थापन
getExtensionEnabledCameraSelector(cameraSelector, mode) ExtensionSessionConfig(mode, extensionsManager, useCases)
bindToLifecycle(owner, extensionCameraSelector, preview, imageCapture) bindToLifecycle(owner, cameraSelector, extensionSessionConfig)
getCameraInfo(extensionCameraSelector) getCameraInfo(cameraSelector, extensionSessionConfig)
unbindAll(), फिर मोड बदलने के लिए फिर से बाइंड करें सेशन के नए कॉन्फ़िगरेशन को बाइंड करें. पिछला डिवाइस अपने-आप अनबाउंड हो जाता है.

अन्य संसाधन

CameraX के बारे में ज़्यादा जानने के लिए, यहां दिए गए संसाधन देखें.

कोडलैब (कोड बनाना सीखना)

  • CameraX का इस्तेमाल शुरू करना
  • कोड सैंपल

    कैमरा सैंपल कैटलॉग (एक्सटेंशन का सैंपल देखें)

    अन्य रेफ़रंस

    CameraX वेंडर एक्सटेंशन

    CameraX वेंडर एक्सटेंशन की पुष्टि करने वाला टूल