API ส่วนขยาย CameraX

CameraX มี Extensions API สำหรับการเข้าถึงส่วนขยายที่ผู้ผลิตอุปกรณ์นำมาใช้งานในอุปกรณ์ Android รุ่นต่างๆ ดูรายการโหมดส่วนขยายที่รองรับได้ที่ส่วนขยายกล้อง

โปรดดูรายการอุปกรณ์ที่รองรับส่วนขยายที่หัวข้ออุปกรณ์ที่รองรับ

สถาปัตยกรรมส่วนขยาย

รูปภาพต่อไปนี้แสดงสถาปัตยกรรมของส่วนขยายกล้อง

วันที่
รูปที่ 1 สถาปัตยกรรมส่วนขยายกล้อง

แอปพลิเคชัน CameraX สามารถใช้ส่วนขยายผ่าน CameraX Extensions API ได้ CameraX Extensions API จัดการการค้นหาส่วนขยายที่พร้อมใช้งาน การกำหนดค่า เซสชันกล้องของส่วนขยายและการสื่อสารกับ OEM ส่วนขยายกล้อง ไลบรารี ซึ่งทำให้แอปพลิเคชันใช้ความสามารถต่างๆ ได้ เช่น กลางคืน, HDR, อัตโนมัติ โ��เก้หรือรีทัชใบหน้า

เปิดใช้ส่วนขยายสำหรับการจับภาพและแสดงตัวอย่าง

ก่อนใช้ API ส่วนขยาย ให้เรียกข้อมูลอินสแตนซ์ ExtensionsManager โดยใช้ extensionManager#getInstanceAsync(Context, CameraProvider) วิธีนี้จะช่วยให้คุณค้นหาความพร้อมใช้งานของส่วนขยายได้ จากนั้นเรียกข้อมูลส่วนขยายที่เปิดใช้ CameraSelector ระบบจะใช้โหมดส่วนขยายในการจับภาพและแสดงตัวอย่างกรณีการใช้งาน การเรียก bindToLifecycle() ที่เปิดใช้ส่วนขยาย CameraSelector

วิธีใช้ส่วนขยายสำหรับการจับภาพและแสดงตัวอย่างกรณีการใช้งาน ดูตัวอย่างโค้ดต่อไปนี้

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}")
    ...
}

การนำ API เดิมออก

สำหรับ Extensions API ใหม่ที่เผยแพร่ใน 1.0.0-alpha26 ซึ่งเป็น ตอนนี้ Extensions API ที่เปิดตัวในเดือนสิงหาคม 2019 เลิกใช้งานแล้ว เริ่มต้นด้วย ระบบได้นํา Extensions API เดิมออกจากเวอร์ชัน 1.0.0-alpha28 แล้ว ไลบรารี ตอนนี้แอปพลิเคชันที่ใช้ Extensions API ใหม่จะต้องรับ CameraSelector ที่เปิดใช้ส่วนขยาย แล้วเชื่อมโยงกรณีการใช้งานเข้าด้วยกัน

แอปพลิเคชันที่ใช้ Extensions API เดิมควรย้ายข้อมูลไปยังเวอร์ชันใหม่ Extensions API เพื่อให้มีความสามารถในการใช้งานร่วมกันกับ CameraX ที่กำลังจะเปิดตัวในอนาคต รุ่น

แหล่งข้อมูลเพิ่มเติม

ดูข้อมูลเพิ่มเติมเกี่ยวกับ CameraX ได้ในแหล่งข้อมูลเพิ่มเติมต่อไ��นี้

Codelab

  • เริ่มต้นใช้งาน CameraX
  • ตัวอย่างโค้ด

    แอปตัวอย่างส่วนขยาย CameraX

    แหล่งอ้างอิงอื่นๆ

    ส่วนขยายของผู้ให้บริการ CameraX

    เครื่องมือตรวจสอบส่วนขยายของผู้ให้บริการ CameraX