หน้านี้กล่าวถึงสถาปัตยกรรมของ CameraX รวมถึงโครงสร้าง, วิธีทำงานกับ API, วิธีทำงานกับวงจร และวิธีรวมกรณีการใช้งาน
โครงสร้าง CameraX
คุณสามารถใช้ CameraX เพื่อเชื่อมต่อกับกล้องของอุปกรณ์ผ่าน Abstraction ที่เรียกว่า Use Case กรณีการใช้งานมีดังนี้
- ดูตัวอย่าง: ยอมรับแพลตฟอร์มสำหรับแสดงตัวอย่าง เช่น
PreviewView
- การวิเคราะห์รูปภาพ: ให้บัฟเฟอร์ที่ CPU เข้าถึงได้สําหรับการวิเคราะห์ เช่น สําหรับการเรียนรู้ของเครื่อง
- การจับภาพ: จับภาพและบันทึกรูปภาพ
- การจับภาพวิดีโอ: บันทึกวิดีโอและเสียงด้วย
VideoCapture
กรณีการใช้งานต่างๆ สามารถรวมกันและใช้งานพร้อมกันได้ เช่น แอปอาจช่วยให้ ผู้ใช้ดูภาพที่กล้องเห็นโดยใช้กรณีการใช้งานตัวอย่าง กรณีการใช้งานการวิเคราะห์รูปภาพที่กำหนดว่าคนในภาพนั้น ยิ้ม และใส่กรณีการใช้งานการจับภาพเพื่อถ่ายภาพในขณะนั้นเลย
โมเดล API
หากต้องการใช้งานไลบรารี คุณต้องระบุสิ่งต่อไปนี้
- Use Case ที่ต้องการพร้อมด้วยตัวเลือกการกำหนดค่า
- สิ่งที่ต้องทำกับข้อมูลเอาต์พุตโดยการแนบ Listeners
- ขั้นตอนที่ต้องการ เช่น เมื่อใดที่ควรเปิดใช้กล้อง และเมื่อใดที่จะสร้างข้อมูล โดยเชื่อมโยงกรณีการใช้งานกับสถาปัตยกรรมของ Android วงจร
การเขียนแอป CameraX ทำได้ 2 วิธี ได้แก่ CameraController
(เหมาะสําหรับผู้ที่ต้องการใช้ CameraX ด้วยวิธีที่ง่ายที่สุด) หรือ CameraProvider
(เหมาะสําหรับผู้ที่ต้องการความยืดหยุ่นมากขึ้น)
คอนโทรลเลอร์กล้อง
CameraController
มีฟังก์ชันหลักส่วนใหญ่ของ CameraX อยู่ใน
ต้องใช้รหัสการตั้งค่าเพียงเล็กน้อย และสามารถจัดการกล้องได้โดยอัตโนมัติ
การเริ่มต้น, การจัดการกรณีการใช้งาน, การหมุนเป้าหมาย, แตะเพื่อโฟกัส
บีบเพื่อซูม และอื่นๆ คลาสที่เป็นรูปธรรมที่ขยาย CameraController
คือ
LifecycleCameraController
Kotlin
val previewView: PreviewView = viewBinding.previewView var cameraController = LifecycleCameraController(baseContext) cameraController.bindToLifecycle(this) cameraController.cameraSelector = CameraSelector.DEFAULT_BACK_CAMERA previewView.controller = cameraController
Java
PreviewView previewView = viewBinding.previewView; LifecycleCameraController cameraController = new LifecycleCameraController(baseContext); cameraController.bindToLifecycle(this); cameraController.setCameraSelector(CameraSelector.DEFAULT_BACK_CAMERA); previewView.setController(cameraController);
UseCase
เริ่มต้นของ CameraController
คือ Preview
, ImageCapture
และ
ImageAnalysis
หากต้องการปิดImageCapture
หรือImageAnalysis
หรือเพื่อปิด
VideoCapture
เปิดอยู่ ให้ใช้
setEnabledUseCases()
ดูการใช้งาน CameraController
เพิ่มเติมได้ในตัวอย่างเครื่องมือสแกนคิวอาร์โค้ดหรือวิดีโอพื้นฐานเกี่ยวกับ CameraController
ผู้ให้บริการกล้อง
CameraProvider
ยังคงใช้งานง่าย แต่เนื่องจากนักพัฒนาแอปจัดการการตั้งค่าได้มากขึ้น จึงมีโอกาสปรับแต่งการกำหนดค่าได้มากขึ้น เช่น การเปิดใช้การหมุนรูปภาพเอาต์พุตหรือการตั้งค่ารูปแบบรูปภาพเอาต์พุตใน ImageAnalysis
นอกจากนี้ คุณยังใช้ Surface
ที่กําหนดเองสําหรับการแสดงตัวอย่างกล้องได้เพื่อให้มีความยืดหยุ่นมากขึ้น แต่หากใช้ CameraController คุณต้องใช้ PreviewView
การใช้รหัส Surface
ที่มีอยู่อาจมีประโยชน์หากเป็นอินพุตสำหรับส่วนอื่นๆ ของแอปอยู่แล้ว
คุณกําหนดค่า Use Case โดยใช้เมธอด set()
และสรุปด้วยเมธอด build()
ออบเจ็กต์ Use Case แต่ละรายการมีชุด API เฉพาะ Use Case สำหรับ
ตัวอย่างเช่น Use Case การจับภาพจะมีการเรียกเมธอด takePicture()
แทนที่แอปพลิเคชันจะเรียกใช้เมธอดเริ่มต้นและหยุดที่เจาะจงใน onResume()
และ onPause()
แอปพลิเคชันจะระบุวงจรเพื่อเชื่อมโยงกล้องโดยใช้ cameraProvider.bindToLifecycle()
วงจรนี้จะแจ้งให้ CameraX ทราบว่าต้องกําหนดค่าเซสชันการจับภาพของกล้องเมื่อใด
และตรวจสอบว่าสถานะของกล้องเปลี่ยนไปอย่างเหมาะสมเพื่อให้ตรงกับการเปลี่ยนวงจร
โปรดดูขั้นตอนการติดตั้งใช้งานสำหรับแต่ละกรณีการใช้งานที่หัวข้อติดตั้งใช้งาน แสดงตัวอย่าง, วิเคราะห์ รูปภาพ, รูปภาพ จับภาพและการจับภาพวิดีโอ
Use Case ตัวอย่างจะโต้ตอบกับ
Surface
สำหรับจอแสดงผล แอปพลิเคชันสร้าง Use Case ด้วยตัวเลือกการกําหนดค่าโดยใช้โค้ดต่อไปนี้
Kotlin
val preview = Preview.Builder().build() val viewFinder: PreviewView = findViewById(R.id.previewView) // The use case is bound to an Android Lifecycle with the following code val camera = cameraProvider.bindToLifecycle(lifecycleOwner, cameraSelector, preview) // PreviewView creates a surface provider and is the recommended provider preview.setSurfaceProvider(viewFinder.getSurfaceProvider())
Java
Preview preview = new Preview.Builder().build(); PreviewView viewFinder = findViewById(R.id.view_finder); // The use case is bound to an Android Lifecycle with the following code Camera camera = cameraProvider.bindToLifecycle(lifecycleOwner, cameraSelector, preview); // PreviewView creates a surface provider, using a Surface from a different // kind of view will require you to implement your own surface provider. preview.previewSurfaceProvider = viewFinder.getSurfaceProvider();
ดูตัวอย่างโค้ดเพิ่มเติมได้ที่ตัวอย่าง CameraX อย่างเป็นทางการ แอป
วงจรการใช้งาน CameraX
CameraX คอยสังเกตวงจรการใช้งานเพื่อพิจารณาว่าจะเปิดกล้องเมื่อใดและเมื่อใด สร้างเซสชันการจับภาพ รวมถึงเวลาที่ควรหยุดและปิดการทำงาน Use Case API มีให้ และ Callback ของเมธอดเพื่อตรวจสอบความคืบหน้า
ตามที่อธิบายไว้ในรวมกรณีการใช้งาน คุณสามารถเชื่อมโยง ให้เป็นวงจรเดียว เมื่อแอปของคุณต้องรองรับกรณีการใช้งานที่ ไม่สามารถรวมได้ คุณสามารถทำอย่างใดอย่างหนึ่งต่อไปนี้
- จัดกลุ่ม Use Case ที่เข้ากันได้ให้เป็นมากกว่า 1 ส่วนย่อย จากนั้นสลับระหว่าง ส่วนย่อย
- สร้างคอมโพเนนต์อายุการใช้งานที่กำหนดเองและใช้เพื่อควบคุมกล้องด้วยตนเอง วงจร
ถ้าคุณแยกมุมมองและกรณีการใช้งานกล้องออก เจ้าของวงจร (เช่น
หากคุณใช้วงจรที่กำหนดเองหรือการรักษา
ส่วนย่อย) แล้วคุณ
ต้องตรวจสอบให้แน่ใจว่า Use Case ทั้งหมดไม่มีการเชื่อมโยงจาก CameraX โดยใช้
ProcessCameraProvider.unbindAll()
หรือโดยยกเลิกการเชื่อมโยงกรณีการใช้งานแต่ละกรณี หรือเมื่อคุณผูก
ไปจนถึงวงจร คุณสามารถให้ CameraX
จัดการการเปิดและปิดเซสชันการบันทึก รวมถึงยกเลิกการเชื่อมโยงกรณีการใช้งาน
หากฟังก์ชันการทำงานทั้งหมดของกล้องสอดคล้องกับวงจร
คอมโพเนนต์ที่รับรู้ถึงวงจร เช่น
AppCompatActivity
หรือ
AppCompat
แฟรกเมนต์ แล้วใช้วงจรของคอมโพเนนต์นั้นเมื่อทำการเชื่อมโยง
กรณีการใช้งานที่ต้องการทั้งหมดจะทำให้ฟังก์ชันต่างๆ ของกล้องพร้อมใช้งาน
เมื่อคอมโพเนนต์ที่รับรู้ถึงอายุการใช้งานทำงานอยู่และจะกำจัดอย่างปลอดภัย ไม่ใช่
การใช้ทรัพยากรทั้งหมด มิฉะนั้น
LifecycleOwner ที่กําหนดเอง
สำหรับกรณีขั้นสูง คุณสามารถสร้าง
LifecycleOwner
เพื่อเปิดใช้
เพื่อควบคุมวงจรเซสชัน CameraX อย่างชัดเจนแทนที่จะผูกกับ
Android มาตรฐาน LifecycleOwner
ตัวอย่างโค้ดต่อไปนี้แสดงวิธีสร้าง LifecycleOwner ที่กําหนดเองแบบง่าย
Kotlin
class CustomLifecycle : LifecycleOwner { private val lifecycleRegistry: LifecycleRegistry init { lifecycleRegistry = LifecycleRegistry(this); lifecycleRegistry.markState(Lifecycle.State.CREATED) } ... fun doOnResume() { lifecycleRegistry.markState(State.RESUMED) } ... override fun getLifecycle(): Lifecycle { return lifecycleRegistry } }
Java
public class CustomLifecycle implements LifecycleOwner { private LifecycleRegistry lifecycleRegistry; public CustomLifecycle() { lifecycleRegistry = new LifecycleRegistry(this); lifecycleRegistry.markState(Lifecycle.State.CREATED); } ... public void doOnResume() { lifecycleRegistry.markState(State.RESUMED); } ... public Lifecycle getLifecycle() { return lifecycleRegistry; } }
เมื่อใช้ LifecycleOwner
นี้ แอปของคุณสามารถใส่การเปลี่ยนสถานะได้ตามต้องการ
ในโค้ด หากต้องการข้อมูลเพิ่มเติมเกี่ยวกับการใช้ฟังก์ชันนี้ในแอป
ดูการติดตั้งใช้งานคู่มือที่กำหนดเอง
LifecycleOwner
Use Case ที่เกิดขึ้นพร้อมกัน
กรณีการใช้งานต่างๆ สามารถดำเนินการพร้อมกันได้ ขณะที่กรณีการใช้งานสามารถเชื่อมโยงตามลำดับกับ
คุณควรเชื่อมโยง Use Case ทั้งหมดด้วยการเรียกเพียงครั้งเดียวเพื่อเชื่อมโยง
CameraProcessProvider.bindToLifecycle()
สำหรับข้อมูลเพิ่มเติมเกี่ยวกับ
แนวทางปฏิบัติในการเปลี่ยนแปลงการกำหนดค่าที่หัวข้อจัดการการกำหนดค่า
การเปลี่ยนแปลง
ในตัวอย่างโค้ดต่อไปนี้ แอประบุ Use Case 2 ฉบับที่จะสร้างขึ้น และเรียกใช้พร้อมกัน และยังระบุวงจรที่จะใช้สำหรับทั้ง 2 ประเภทด้วย เพื่อให้ทั้งเริ่มต้นและหยุดตามวงจร
Kotlin
private lateinit var imageCapture: ImageCapture override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val cameraProviderFuture = ProcessCameraProvider.getInstance(this) cameraProviderFuture.addListener(Runnable { // Camera provider is now guaranteed to be available val cameraProvider = cameraProviderFuture.get() // Set up the preview use case to display camera preview. val preview = Preview.Builder().build() // Set up the capture use case to allow users to take photos. imageCapture = ImageCapture.Builder() .setCaptureMode(ImageCapture.CAPTURE_MODE_MINIMIZE_LATENCY) .build() // Choose the camera by requiring a lens facing val cameraSelector = CameraSelector.Builder() .requireLensFacing(CameraSelector.LENS_FACING_FRONT) .build() // Attach use cases to the camera with the same lifecycle owner val camera = cameraProvider.bindToLifecycle( this as LifecycleOwner, cameraSelector, preview, imageCapture) // Connect the preview use case to the previewView preview.setSurfaceProvider( previewView.getSurfaceProvider()) }, ContextCompat.getMainExecutor(this)) }
Java
private ImageCapture imageCapture; @Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); PreviewView previewView = findViewById(R.id.previewView); ListenableFuture<ProcessCameraProvider> cameraProviderFuture = ProcessCameraProvider.getInstance(this); cameraProviderFuture.addListener(() -> { try { // Camera provider is now guaranteed to be available ProcessCameraProvider cameraProvider = cameraProviderFuture.get(); // Set up the view finder use case to display camera preview Preview preview = new Preview.Builder().build(); // Set up the capture use case to allow users to take photos imageCapture = new ImageCapture.Builder() .setCaptureMode(ImageCapture.CAPTURE_MODE_MINIMIZE_LATENCY) .build(); // Choose the camera by requiring a lens facing CameraSelector cameraSelector = new CameraSelector.Builder() .requireLensFacing(lensFacing) .build(); // Attach use cases to the camera with the same lifecycle owner Camera camera = cameraProvider.bindToLifecycle( ((LifecycleOwner) this), cameraSelector, preview, imageCapture); // Connect the preview use case to the previewView preview.setSurfaceProvider( previewView.getSurfaceProvider()); } catch (InterruptedException | ExecutionException e) { // Currently no exceptions thrown. cameraProviderFuture.get() // shouldn't block since the listener is being called, so no need to // handle InterruptedException. } }, ContextCompat.getMainExecutor(this)); }
CameraX สามารถใช้ 1 อินสแตนซ์ต่อ Preview
พร้อมกันได้
VideoCapture
, ImageAnalysis
และ ImageCapture
นอกจากนี้
- กรณีการใช้งานทุกอย่างทำงานได้ในตัวเอง เช่น แอปสามารถบันทึกวิดีโอได้โดยไม่ต้องใช้การแสดงตัวอย่าง
- เมื่อเปิดใช้ส่วนขยาย เฉพาะ
ImageCapture
และPreview
ที่ร่วมงานกัน คุณอาจเพิ่มImageAnalysis
ไม่ได้ ทั้งนี้ขึ้นอยู่กับการใช้งาน OEM และส่วนขยายจะเปิดใช้งานไม่ได้สำหรับ Use Case ของVideoCapture
ตรวจสอบ เอกสารอ้างอิงส่วนขยาย เพื่อดูรายละเอียด - กล้องบางตัวอาจรองรับการใช้งานร่วมกันที่ โหมดความละเอียดที่ต่ำลง แต่ไม่สามารถรองรับชุดค่าผสมเดียวกันในบางโหมด ความละเอียดที่สูงขึ้นได้
- ในอุปกรณ์ที่มีฮาร์ดแวร์ของกล้องระดับ
FULL
หรือต่ำกว่า ซึ่งรวมPreview
VideoCapture
และImageCapture
หรือImageAnalysis
อาจบังคับใช้ CameraX เพื่อทำสำเนาสตรีมPRIV
ของกล้องสำหรับPreview
และVideoCapture
ช่วงเวลานี้ การทำซ้ำที่เรียกว่าการแชร์สตรีม จะช่วยให้สามารถใช้ แต่มาพร้อมกับค่าใช้จ่ายเมื่อความต้องการในการประมวลผลเพิ่มขึ้น คุณอาจ เวลาในการตอบสนองจะสูงขึ้นเล็กน้อยและทำให้อายุการใช้งานแบตเตอรี่ลดลง
คุณสามารถดูระดับฮาร์ดแวร์ที่รองรับได้จาก Camera2CameraInfo
ตัวอย่างเช่น รหัสต่อไปนี้จะตรวจสอบว่ากล้องหลังเริ่มต้นเป็นอุปกรณ์ LEVEL_3
หรือไม่
Kotlin
@androidx.annotation.OptIn(ExperimentalCamera2Interop::class) fun isBackCameraLevel3Device(cameraProvider: ProcessCameraProvider) : Boolean { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { return CameraSelector.DEFAULT_BACK_CAMERA .filter(cameraProvider.availableCameraInfos) .firstOrNull() ?.let { Camera2CameraInfo.from(it) } ?.getCameraCharacteristic(CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL) == CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL_3 } return false }
Java
@androidx.annotation.OptIn(markerClass = ExperimentalCamera2Interop.class) Boolean isBackCameraLevel3Device(ProcessCameraProvider cameraProvider) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { List\filteredCameraInfos = CameraSelector.DEFAULT_BACK_CAMERA .filter(cameraProvider.getAvailableCameraInfos()); if (!filteredCameraInfos.isEmpty()) { return Objects.equals( Camera2CameraInfo.from(filteredCameraInfos.get(0)).getCameraCharacteristic( CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL), CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL_3); } } return false; }
สิทธิ์
แอปของคุณจะต้องมี
CAMERA
หากต้องการบันทึกรูปภาพไปยังไฟล์ แอปจะต้องได้รับสิทธิ์WRITE_EXTERNAL_STORAGE
ด้วย ยกเว้นในอุปกรณ์ที่ใช้ Android 10 ขึ้นไป
สำหรับข้อมูลเพิ่มเติมเกี่ยวกับการกำหนดค่าสิทธิ์สำหรับแอป โปรดอ่านส่งคำขอ สิทธิ์ของแอป
ข้อกำหนด
CameraX มีข้อกำหนดขั้นต่ำเวอร์ชันต่อไปนี้
- Android API ระดับ 21
- คอมโพเนนต์สถาปัตยกรรม Android 1.1.1
สำหรับกิจกรรมที่คำนึงถึงวงจร ให้ใช้
FragmentActivity
หรือ
AppCompatActivity
ประกาศทรัพยากร Dependency
หากต้องการเพิ่มทรัพยากร Dependency ใน CameraX คุณต้องเพิ่มแพ็กเกจ Google Maven ลงในโปรเจ็กต์ของคุณ
เปิดไฟล์ settings.gradle
สำหรับโปรเจ็กต์และเพิ่มที่เก็บ google()
ดังต่อไปนี้
Groovy
dependencyResolutionManagement { repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS) repositories { google() mavenCentral() } }
Kotlin
dependencyResolutionManagement { repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS) repositories { google() mavenCentral() } }
เพิ่มโค้ดต่อไปนี้ไว้ที่ส่วนท้ายของบล็อก Android
ดึงดูด
android { compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } // For Kotlin projects kotlinOptions { jvmTarget = "1.8" } }
Kotlin
android { compileOptions { sourceCompatibility = JavaVersion.VERSION_1_8 targetCompatibility = JavaVersion.VERSION_1_8 } // For Kotlin projects kotlinOptions { jvmTarget = "1.8" } }
เพิ่มข้อมูลต่อไปนี้ลงในไฟล์ build.gradle
ของแต่ละโมดูลสําหรับแอป
ดึงดูด
dependencies { // CameraX core library using the camera2 implementation def camerax_version = "1.5.0-alpha01" // The following line is optional, as the core library is included indirectly by camera-camera2 implementation "androidx.camera:camera-core:${camerax_version}" implementation "androidx.camera:camera-camera2:${camerax_version}" // If you want to additionally use the CameraX Lifecycle library implementation "androidx.camera:camera-lifecycle:${camerax_version}" // If you want to additionally use the CameraX VideoCapture library implementation "androidx.camera:camera-video:${camerax_version}" // If you want to additionally use the CameraX View class implementation "androidx.camera:camera-view:${camerax_version}" // If you want to additionally add CameraX ML Kit Vision Integration implementation "androidx.camera:camera-mlkit-vision:${camerax_version}" // If you want to additionally use the CameraX Extensions library implementation "androidx.camera:camera-extensions:${camerax_version}" }
Kotlin
dependencies { // CameraX core library using the camera2 implementation val camerax_version = "1.5.0-alpha01" // The following line is optional, as the core library is included indirectly by camera-camera2 implementation("androidx.camera:camera-core:${camerax_version}") implementation("androidx.camera:camera-camera2:${camerax_version}") // If you want to additionally use the CameraX Lifecycle library implementation("androidx.camera:camera-lifecycle:${camerax_version}") // If you want to additionally use the CameraX VideoCapture library implementation("androidx.camera:camera-video:${camerax_version}") // If you want to additionally use the CameraX View class implementation("androidx.camera:camera-view:${camerax_version}") // If you want to additionally add CameraX ML Kit Vision Integration implementation("androidx.camera:camera-mlkit-vision:${camerax_version}") // If you want to additionally use the CameraX Extensions library implementation("androidx.camera:camera-extensions:${camerax_version}") }
สำหรับข้อมูลเพิ่มเติมเกี่ยวกับการกำหนดค่าแอปให้เป็นไปตามข้อกำหนดเหล่านี้ ดูการประกาศ ทรัพยากร Dependency
ความสามารถในการทำงานร่วมกันของ CameraX กับ Camera2
CameraX อยู่ใน Camera2 และ CameraX มีวิธีการอ่านและการเขียน ในการใช้งาน Camera2 ดูรายละเอียดทั้งหมดได้ในแพ็กเกจการทำงานร่วมกัน
สําหรับข้อมูลเพิ่มเติมเกี่ยวกับวิธีที่ CameraX กําหนดค่าคุณสมบัติของ Camera2 ให้ใช้
Camera2CameraInfo
เพื่ออ่าน CameraCharacteristics
ที่เกี่ยวข้อง คุณยังเลือกเขียน Camera2 ที่เกี่ยวข้องได้ด้วย
พร็อพเพอร์ตี้ในหนึ่งใน 2 เส้นทางต่อไปนี้
ใช้
Camera2CameraControl
, ซึ่งช่วยให้คุณตั้งค่าพร็อพเพอร์ตี้CaptureRequest
เช่น โหมดโฟกัสอัตโนมัติขยาย CameraX
UseCase
ด้วยCamera2Interop.Extender
ซึ่งจะช่วยให้คุณตั้งค่าคุณสมบัติใน CaptureRequest เช่นเดียวกับCamera2CameraControl
นอกจากนี้ยังให้คุณควบคุมเพิ่มเติมได้ด้วย เช่น ตั้งค่ากรณีการใช้งานของสตรีมเป็น ปรับกล้องให้เหมาะกับสถานการณ์การใช้งานของคุณ สำหรับข้อมูล โปรดดูที่ใช้กรณีการใช้งานของสตรีมเพื่อประสิทธิภาพที่ดีขึ้น ประสิทธิภาพ
ตัวอย่างโค้ดต่อไปนี้ใช้กรณีการใช้งานของสตรีมเพื่อเพิ่มประสิทธิภาพสำหรับวิดีโอคอล
ใช้ Camera2CameraInfo
เพื่อดึงข้อมูลว่ากรณีการใช้งานสตรีมวิดีโอคอลพร้อมใช้งานหรือไม่ จากนั้นใช้
Camera2Interop.Extender
เพื่อกำหนด Use Case ของสตรีมที่เกี่ยวข้อง
Kotlin
// Set underlying Camera2 stream use case to optimize for video calls. val videoCallStreamId = CameraMetadata.SCALER_AVAILABLE_STREAM_USE_CASES_VIDEO_CALL.toLong() // Check available CameraInfos to find the first one that supports // the video call stream use case. val frontCameraInfo = cameraProvider.getAvailableCameraInfos() .first { cameraInfo -> val isVideoCallStreamingSupported = Camera2CameraInfo.from(cameraInfo) .getCameraCharacteristic( CameraCharacteristics.SCALER_AVAILABLE_STREAM_USE_CASES )?.contains(videoCallStreamId) val isFrontFacing = (cameraInfo.getLensFacing() == CameraSelector.LENS_FACING_FRONT) (isVideoCallStreamingSupported == true) && isFrontFacing } val cameraSelector = frontCameraInfo.cameraSelector // Start with a Preview Builder. val previewBuilder = Preview.Builder() .setTargetAspectRatio(screenAspectRatio) .setTargetRotation(rotation) // Use Camera2Interop.Extender to set the video call stream use case. Camera2Interop.Extender(previewBuilder).setStreamUseCase(videoCallStreamId) // Bind the Preview UseCase and the corresponding CameraSelector. val preview = previewBuilder.build() camera = cameraProvider.bindToLifecycle(this, cameraSelector, preview)
Java
// Set underlying Camera2 stream use case to optimize for video calls. Long videoCallStreamId = CameraMetadata.SCALER_AVAILABLE_STREAM_USE_CASES_VIDEO_CALL.toLong(); // Check available CameraInfos to find the first one that supports // the video call stream use case. List<CameraInfo> cameraInfos = cameraProvider.getAvailableCameraInfos(); CameraInfo frontCameraInfo = null; for (cameraInfo in cameraInfos) { Long[] availableStreamUseCases = Camera2CameraInfo.from(cameraInfo) .getCameraCharacteristic( CameraCharacteristics.SCALER_AVAILABLE_STREAM_USE_CASES ); boolean isVideoCallStreamingSupported = Arrays.List(availableStreamUseCases) .contains(videoCallStreamId); boolean isFrontFacing = (cameraInfo.getLensFacing() == CameraSelector.LENS_FACING_FRONT); if (isVideoCallStreamingSupported && isFrontFacing) { frontCameraInfo = cameraInfo; } } if (frontCameraInfo == null) { // Handle case where video call streaming is not supported. } CameraSelector cameraSelector = frontCameraInfo.getCameraSelector(); // Start with a Preview Builder. Preview.Builder previewBuilder = Preview.Builder() .setTargetAspectRatio(screenAspectRatio) .setTargetRotation(rotation); // Use Camera2Interop.Extender to set the video call stream use case. Camera2Interop.Extender(previewBuilder).setStreamUseCase(videoCallStreamId); // Bind the Preview UseCase and the corresponding CameraSelector. Preview preview = previewBuilder.build() Camera camera = cameraProvider.bindToLifecycle(this, cameraSelector, preview)
แหล่งข้อมูลเพิ่มเติม
ดูข้อมูลเพิ่มเติมเกี่ยวกับ CameraX ได้ในแหล่งข้อมูลเพิ่มเติมต่อไปนี้
Codelab
ตัวอย่างโค้ด