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

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

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

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

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

पहली इमेज. Camera Extensions का आर्किटेक्चर

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

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

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

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

// 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))
}

एक्सटेंशन बंद करना

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

डिपेंडेंसी

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

Groovy

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 में रिलीज़ किए गए नए एक्सटेंशन एपीआई के साथ, अगस्त 2019 में रिलीज़ किया गया लेगसी एक्सटेंशन एपीआई अब काम नहीं करता. वर्शन 1.0.0-alpha28 से, लेगसी एक्सटेंशन एपीआई को लाइब्रेरी से हटा दिया गया है. नए Extensions API का इस्तेमाल करने वाले ऐप्लिकेशन को अब एक्सटेंशन की सुविधा वाला CameraSelector हासिल करना होगा. साथ ही, इस्तेमाल के उदाहरणों को बाइंड करने के लिए, इसका इस्तेमाल करना होगा.

लेगसी Extensions API का इस्तेमाल करने वाले ऐप्लिकेशन को नए Extensions API पर माइग्रेट करना चाहिए. इससे यह पक्का किया जा सकेगा कि आने वाले समय में CameraX के नए वर्शन के साथ काम किया जा सके.

अन्य संसाधन

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

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

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

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

    अन्य रेफ़रंस

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

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