CameraX มี Extensions API สำหรับเข้าถึงส่วนขยายที่ผู้ผลิตอุปกรณ์ได้ติดตั้งใช้งานในอุปกรณ์ Android ต่างๆ ดูรายการโหมดส่วนขยายที่รองรับได้ที่ส่วนขยายกล้อง
ดูรายการอุปกรณ์ที่รองรับส่วนขยายได้ที่หัวข้ออุปกรณ์ที่รองรับ
สถาปัตยกรรมของส่วนขยาย
รูปภาพต่อไปนี้แสดงสถาปัตยกรรมของส่วนขยายกล้อง
แอปพลิเคชัน CameraX สามารถใช้ส่วนขยายผ่าน CameraX Extensions API ได้ CameraX Extensions API จะจัดการการค้นหาส่วนขยายที่พร้อมใช้งาน การกำหนดค่าเซสชันกล้องของส่วนขยาย และการสื่อสารกับไลบรารี OEM ของส่วนขยายกล้อง ซึ่งจะช่วยให้แอปพลิเคชันของคุณใช้ความสามารถต่างๆ เช่น โหมดกลางคืน, HDR, อัตโนมัติ, โบเก้ หรือรีทัชใบหน้าได้
เปิดใช้ส่วนขยายสำหรับการจับภาพและการแสดงตัวอย่าง
ก่อนใช้ Extensions API ให้ดึงข้อมูลอินสแตนซ์ ExtensionsManager โดยใช้เมธอด ExtensionsManager#getInstanceAsync(Context, CameraProvider) ซึ่งจะช่วยให้คุณค้นหาข้อมูลความพร้อมใช้งานของส่วนขยายได้ จากนั้นดึงข้อมูล CameraSelector ที่เปิดใช้ส่วนขยาย ระบบจะใช้โหมดส่วนขยายกับ Use Case การจับภาพและการแสดงตัวอย่างเมื่อเรียกใช้เมธอด bindToLifecycle() โดยเปิดใช้ส่วนขยาย CameraSelector
หากต้องการใช้ส่วนขยายสำหรับ Use Case การจับภาพและการแสดงตัวอย่าง โปรดดูตัวอย่างโค้ดต่อไปนี้
// 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)) }
ปิดใช้ส่วนขยาย
หากต้องการปิดใช้ส่วนขยายของผู้ให้บริการ ให้เลิกเชื่อมโยง Use Case ทั้งหมด แล้วเชื่อมโยง Use Case การจับภาพและการแสดงตัวอย่างอีกครั้งด้วยตัวเลือกกล้องปกติ เช่น เชื่อมโยงกับกล้องหลังอีกครั้งโดยใช้ 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 ตั้งแต่เวอร์ชัน 1.0.0-alpha28 เป็นต้นไป ระบบจะนำ Extensions API เดิมออกจากคลัง ตอนนี้แอปพลิเคชันที่ใช้ Extensions API ใหม่ต้องได้รับ CameraSelector ที่เปิดใช้ส่วนขยายและใช้เพื่อเชื่อมโยง Use Case
แอปพลิเคชันที่ใช้ Extensions API เดิมควรย้ายข้อมูลไปยัง Extensions API ใหม่เพื่อให้มั่นใจว่าแอปพลิเคชันจะเข้ากันได้กับ CameraX รุ่นต่อๆ ไปในอนาคต
แหล่งข้อมูลเพิ่มเติม
ดูข้อมูลเพิ่มเติมเกี่ยวกับ CameraX ได้ที่แหล่งข้อมูลเพิ่มเติมต่อไปนี้
Codelab
ตัวอย่างโค้ด
การอ้างอิงอื่นๆ
ส่วนขยายของผู้ให้บริการ CameraX
เครื่องมือตรวจสอบส่วนขยายของผู้ให้บริการ CameraX