CameraX এক্সটেনশন API

CameraX একটি এক্সটেনশন এপিআই (Extensions API) প্রদান করে, যার মাধ্যমে ডিভাইস নির্মাতাদের দ্বারা বিভিন্ন অ্যান্ড্রয়েড ডিভাইসে প্রয়োগ করা এক্সটেনশনগুলো অ্যাক্সেস করা যায়। সমর্থিত এক্সটেনশন মোডগুলোর তালিকার জন্য, ক্যামেরা এক্সটেনশন (Camera extensions) দেখুন।

যেসব ডিভাইস এক্সটেনশন সমর্থন করে, তার তালিকার জন্য সমর্থিত ডিভাইসসমূহ দেখুন।

এক্সটেনশন স্থাপত্য

নিম্নোক্ত চিত্রটিতে ক্যামেরা এক্সটেনশনগুলোর স্থাপত্য দেখানো হয়েছে।

চিত্র ১. ক্যামেরা এক্সটেনশনের স্থাপত্য

একটি CameraX অ্যাপ্লিকেশন CameraX এক্সটেনশনস এপিআই (API)-এর মাধ্যমে এক্সটেনশন ব্যবহার করতে পারে। CameraX এক্সটেনশনস এপিআই উপলব্ধ এক্সটেনশনগুলির জন্য কোয়েরি করা, একটি এক্সটেনশন ক্যামেরা সেশন কনফিগার করা এবং ক্যামেরা এক্সটেনশনস ওইএম (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))
}

জাভা

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 এক্সটেনশনস এপিআইটি 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}"
    ...
}

কোটলিন

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 সংস্করণে নতুন এক্সটেনশনস এপিআই প্রকাশিত হওয়ায়, ২০১৯ সালের আগস্টে প্রকাশিত লিগ্যাসি এক্সটেনশনস এপিআই এখন অপ্রচলিত (deprecated) হিসেবে গণ্য হচ্ছে। 1.0.0-alpha28 সংস্করণ থেকে লিগ্যাসি এক্সটেনশনস এপিআই লাইব্রেরি থেকে সরিয়ে ফেলা হয়েছে। নতুন এক্সটেনশনস এপিআই ব্যবহারকারী অ্যাপ্লিকেশনগুলোকে এখন অবশ্যই একটি এক্সটেনশন-সক্ষম CameraSelector সংগ্রহ করতে হবে এবং ইউজ কেসগুলো বাইন্ড করার জন্য এটি ব্যবহার করতে হবে।

আসন্ন CameraX রিলিজগুলোর সাথে ভবিষ্যৎ সামঞ্জস্যতা নিশ্চিত করার জন্য, পুরোনো এক্সটেনশনস এপিআই ব্যবহারকারী অ্যাপ্লিকেশনগুলোর নতুন এক্সটেনশনস এপিআই-তে স্থানান্তরিত হওয়া উচিত।

অতিরিক্ত সম্পদ

CameraX সম্পর্কে আরও জানতে, নিম্নলিখিত অতিরিক্ত উৎসগুলো দেখুন।

কোডল্যাব

  • CameraX দিয়ে শুরু করা
  • কোডের নমুনা

    ক্যামেরাএক্স এক্সটেনশন নমুনা অ্যাপ

    অন্যান্য তথ্যসূত্র

    ক্যামেরাএক্স ভেন্ডর এক্সটেনশন

    ক্যামেরাএক্স ভেন্ডর এক্সটেনশন ভ্যালিডেশন টুল