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

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

एक्सटेंशन की सुविधा वाले डिवाइसों की सूची देखने के लिए, काम करने वाले डिवाइस लेख पढ़ें.

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

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

पहली इमेज. कैमरे के एक्सटेंशन का आर्किटेक्चर

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

इमेज कैप्चर करने और उसकी झलक देखने के लिए, एक्सटेंशन की सुविधा चालू करना

एक्सटेंशन एपीआई का इस्तेमाल करने से पहले, ExtensionsManager इंस्टेंस को ExtensionsManager#getInstanceAsync(Context, CameraProvider) तरीके का इस्तेमाल करके वापस पाएं. इससे, आपको एक्सटेंशन की उपलब्धता के बारे में क्वेरी करने की सुविधा मिलेगी. इसके बाद, एक्सटेंशन की सुविधा चालू करने के लिए, CameraSelector को वापस पाएं. एक्सटेंशन मोड, इमेज कैप्चर करने और उसकी झलक देखने के लिए लागू होगा. ऐसा तब होगा, जब CameraSelector एक्सटेंशन की सुविधा चालू करके, bindToLifecycle() तरीके को कॉल किया जाएगा.

इमेज कैप्चर करने और उसकी झलक देखने के लिए, एक्सटेंशन की सुविधा लागू करने के लिए, यहां दिया गया कोड सैंपल देखें:

Kotlin

import androidx.camera.extensions.ExtensionMode
import androidx.camera.extensions.ExtensionsManager

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    val lifecycleOwner = this

    val cameraProviderFuture = ProcessCameraProvider.getInstance(applicationContext)
    cameraProviderFuture.addListener({
        // Obtain an instance of a process camera provider
        // The camera provider provides access to the set of cameras associated with the device.
        // The camera obtained from the provider will be bound to the activity lifecycle.
        val cameraProvider = cameraProviderFuture.get()

        val extensionsManagerFuture =
            ExtensionsManager.getInstanceAsync(applicationContext, cameraProvider)
        extensionsManagerFuture.addListener({
            // Obtain an instance of the extensions manager
            // The extensions manager enables a camera to use extension capabilities available on
            // the device.
            val extensionsManager = extensionsManagerFuture.get()

            // Select the camera
            val cameraSelector = CameraSelector.DEFAULT_BACK_CAMERA

            // Query if extension is available.
            // Not all devices will support extensions or might only support a subset of
            // extensions.
            if (extensionsManager.isExtensionAvailable(cameraSelector, ExtensionMode.NIGHT)) {
                // Unbind all use cases before enabling different extension modes.
                try {
                    cameraProvider.unbindAll()

                    // Retrieve a night extension enabled camera selector
                    val nightCameraSelector =
                        extensionsManager.getExtensionEnabledCameraSelector(
                            cameraSelector,
                            ExtensionMode.NIGHT
                        )

                    // Bind image capture and preview use cases with the extension enabled camera
                    // selector.
                    val imageCapture = ImageCapture.Builder().build()
                    val preview = Preview.Builder().build()
                    // Connect the preview to receive the surface the camera outputs the frames
                    // to. This will allow displaying the camera frames in either a TextureView
                    // or SurfaceView. The SurfaceProvider can be obtained from the PreviewView.
                    preview.setSurfaceProvider(surfaceProvider)

                    // Returns an instance of the camera bound to the lifecycle
                    // Use this camera object to control various operations with the camera
                    // Example: flash, zoom, focus metering etc.
                    val camera = cameraProvider.bindToLifecycle(
                        lifecycleOwner,
                        nightCameraSelector,
                        imageCapture,
                        preview
                    )
                } catch (e: Exception) {
                    Log.e(TAG, "Use case binding failed", e)
                }
            }
        }, ContextCompat.getMainExecutor(this))
    }, ContextCompat.getMainExecutor(this))
}

Java

import androidx.camera.extensions.ExtensionMode;
import androidx.camera.extensions.ExtensionsManager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    final LifecycleOwner lifecycleOwner = this;

    final ListenableFuture cameraProviderFuture =
            ProcessCameraProvider.getInstance(getApplicationContext());

    cameraProviderFuture.addListener(() -> {
      try {
          // Obtain an instance of a process camera provider
          // The camera provider provides access to the set of cameras associated with the
          // device. The camera obtained from the provider will be bound to the activity
          // lifecycle.
          final ProcessCameraProvider cameraProvider = cameraProviderFuture.get();

          final ListenableFuture extensionsManagerFuture =
                  ExtensionsManager.getInstanceAsync(getApplicationContext(), cameraProvider);
          extensionsManagerFuture.addListener(() -> {
              // Obtain an instance of the extensions manager
              // The extensions manager enables a camera to use extension capabilities available
              // on the device.
              try {
                  final ExtensionsManager extensionsManager = extensionsManagerFuture.get();

                  // Select the camera
                  final CameraSelector cameraSelector = CameraSelector.DEFAULT_BACK_CAMERA;

                  // Query if extension is available.
                  // Not all devices will support extensions or might only support a subset of
                  // extensions.
                  if (extensionsManager.isExtensionAvailable(
                          cameraSelector,
                          ExtensionMode.NIGHT
                  )) {
                      // Unbind all use cases before enabling different extension modes.
                      cameraProvider.unbindAll();

                      // Retrieve extension enabled camera selector
                      final CameraSelector nightCameraSelector = extensionsManager
                              .getExtensionEnabledCameraSelector(cameraSelector, ExtensionMode.NIGHT);

                      // Bind image capture and preview use cases with the extension enabled camera
                      // selector.
                      final ImageCapture imageCapture = new ImageCapture.Builder().build();
                      final Preview preview = new Preview.Builder().build();
                      // Connect the preview to receive the surface the camera outputs the frames
                      // to. This will allow displaying the camera frames in either a TextureView
                      // or SurfaceView. The SurfaceProvider can be obtained from the PreviewView.
                      preview.setSurfaceProvider(surfaceProvider);

                      cameraProvider.bindToLifecycle(
                              lifecycleOwner,
                              nightCameraSelector,
                              imageCapture,
                              preview
                      );
                  }
              } catch (ExecutionException | InterruptedException e) {
                  throw new RuntimeException(e);
              }
          }, ContextCompat.getMainExecutor(this));

      } catch (ExecutionException | InterruptedException e) {
          throw new RuntimeException(e);
      }

  }, ContextCompat.getMainExecutor(this));
}

एक्सटेंशन की सुविधा बंद करना

वेंडर एक्सटेंशन की सुविधा बंद करने के लिए, सभी इस्तेमाल के मामलों को अनबाइंड करें. इसके बाद, सामान्य कैमरा सिलेक्टर की मदद से, इमेज कैप्चर करने और उसकी झलक देखने के लिए, इस्तेमाल के मामलों को फिर से बाइंड करें. उदाहरण के लिए, CameraSelector.DEFAULT_BACK_CAMERA का इस्तेमाल करके, बैक कैमरे पर फिर से बाइंड करें.

डिपेंडेंसी

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

शानदार

dependencies {
  def camerax_version = "1.2.0-rc01"
  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}"
    ...
}

Kotlin

dependencies {
  val camerax_version = "1.2.0-rc01"
  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}")
    ...
}

लेगसी एपीआई को हटाना

1.0.0-alpha26 में रिलीज़ किए गए नए Extensions API के साथ, अगस्त 2019 में रिलीज़ किया गया लेगसी Extensions API अब बंद कर दिया गया है. वर्शन 1.0.0-alpha28 से, लेगसी Extensions API को लाइब्रेरी से हटा दिया गया है. नए Extensions API का इस्तेमाल करने वाले ऐप्लिकेशन को अब एक्सटेंशन की सुविधा वाले CameraSelector को हासिल करना होगा. साथ ही, इस्तेमाल के मामलों को बाइंड करने के लिए, इसका इस्तेमाल करना होगा.

लेगसी Extensions API का इस्तेमाल करने वाले ऐप्लिकेशन को नए Extensions API पर माइग्रेट करना चाहिए. इससे, आने वाले समय में CameraX के नए वर्शन के साथ, ऐप्लिकेशन की सुविधाओं को बिना किसी रुकावट के उपलब्ध कराने में मदद मिलेगी.

अन्य संसाधन

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

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

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

    CameraX एक्सटेंशन का सैंपल ऐप्लिकेशन

    अन्य रेफ़रंस

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

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