แฟลชหน้าจอ

แฟลชหน้าจอหรือที่เรียกว่าแฟลชหน้าหรือแฟลชเซลฟีจะใช้หน้าจอโทรศัพท์ ความสว่างเพื่อให้วัตถุสว่างขึ้นเมื่อจับภาพด้วยกล้องหน้า ในสภาพแสงน้อย พร้อมใช้งานในแอปกล้องที่มาพร้อมเครื่องและโซเชียลมากมาย แอปสื่อ เพราะคนส่วนใหญ่ถือโทรศัพท์ไว้ใกล้ตัวเองมากพอเมื่อจัดให้ตัวเองอยู่ในเฟรม แนวตั้ง วิธีนี้ได้ผล

อย่างไรก็ตาม นักพัฒนาซอฟต์แวร์ใช้งานฟีเจอร์นี้อย่างถูกต้องได้ยาก จะรักษาคุณภาพการจับภาพที่ดีไว้ได้อย่างสอดคล้องกันในทุกอุปกรณ์ คู่มือนี้จะแสดง วิธีใช้ฟีเจอร์นี้อย่างถูกต้องโดยใช้ camera2, API เฟรมเวิร์กกล้อง Android ระดับต่ำ

เวิร์กโฟลว์ทั่วไป

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

วันที่ โฟลว์ชาร์ตแสดงวิธีใช้ UI ของแฟลชหน้าจอภายใน Camera2
รูปที่ 1 ขั้นตอนทั่วไปสำหรับการติดตั้งหน้าจอ Flash

ใช้ขั้นตอนต่อไปนี้เมื่อต้องจับภาพด้วยหน้าจอ ฟีเจอร์แฟลช

  1. ดำเนินการเปลี่ยนแปลง UI ที่จำเป็นสำหรับแฟลชหน้าจอ ซึ่งสามารถให้ ไฟสำหรับถ่ายภาพโดยใช้หน้าจอของอุปกรณ์ สำหรับกรณีการใช้งานทั่วไป Google แนะนำการเปลี่ยนแปลง UI ต่อไปนี้ตามที่ใช้ในการทดสอบของเรา:
    • หน้าจอแอปถูกวางซ้อนด้วยสีขาว
    • เพิ่มความสว่างของหน้าจอสูงสุดแล้ว
  2. ตั้งค่าโหมดการรับแสงอัตโนมัติ (AE) เป็น CONTROL_AE_MODE_ON_EXTERNAL_FLASH หาก ที่รองรับ
  3. เรียกให้แสดงลำดับการวัดค่าล่วงหน้าโดยใช้ CONTROL_AE_PRECAPTURE_TRIGGER
  4. รอให้การรับแสงอัตโนมัติ (AE) และสมดุลแสงขาวอัตโนมัติ (AWB) บรรจบกัน

  5. เมื่อเข้าหากันแล้ว ระบบจะใช้ขั้นตอนการจับภาพตามปกติของแอป

  6. ส่งคำขอจับภาพไปยังเฟรมเวิร์ก

  7. รอรับผลการจับภาพ

  8. รีเซ็ตโหมด AE หากมีการตั้งค่า CONTROL_AE_MODE_ON_EXTERNAL_FLASH

  9. ล้างการเปลี่ยนแปลง UI สำหรับแฟลชหน้าจอ

โค้ดตัวอย่างของ Camera2

หน้าจอแอปแบบหน้าปกซึ่งมีการวางซ้อนสีขาว

เพิ่มมุมมองในไฟล์ XML เลย์เอาต์ของแอปพลิเคชัน มุมมองมีปริมาณเพียงพอ การยกระดับให้อยู่เหนือองค์ประกอบ UI อื่นๆ ทั้งหมดระหว่างการแสดงแฟลชหน้าจอ การจับภาพ วิดีโอจะมองไม่เห็นโดยค่าเริ่มต้นและมองเห็นได้เมื่อหน้าจอเท่านั้น การเปลี่ยนแปลง UI ของ Flash จะถูกนำไปใช้

ในตัวอย่างโค้ดต่อไปนี้ มีการใช้สีขาว (#FFFFFF) เป็นตัวอย่างสำหรับ มุมมอง แอปพลิเคชันสามารถเลือกสี หรือเสนอหลายสีให้กับผู้ใช้ ตามข้อกำหนด

<View
    android:id="@+id/white_color_overlay"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FFFFFF"
    android:visibility="invisible"
    android:elevation="8dp" />

เพิ่มความสว่างของหน้าจอ

การเปลี่ยนความสว่างของหน้าจอในแอป Android ทำได้หลายวิธี หนึ่ง โดยตรงก็คือการเปลี่ยน พารามิเตอร์ screenBrightness WindowManager ใน ข้อมูลอ้างอิงหน้าต่างกิจกรรม

Kotlin

private var previousBrightness: Float = -1.0f

private fun maximizeScreenBrightness() {
    activity?.window?.let { window ->
        window.attributes?.apply {
            previousBrightness = screenBrightness
            screenBrightness = 1f
            window.attributes = this
        }
    }
}

private fun restoreScreenBrightness() {
    activity?.window?.let { window ->
        window.attributes?.apply {
            screenBrightness = previousBrightness
            window.attributes = this
        }
    }
}

Java

private float mPreviousBrightness = -1.0f;

private void maximizeScreenBrightness() {
    if (getActivity() == null || getActivity().getWindow() == null) {
        return;
    }

    Window window = getActivity().getWindow();
    WindowManager.LayoutParams attributes = window.getAttributes();

    mPreviousBrightness = attributes.screenBrightness;
    attributes.screenBrightness = 1f;
    window.setAttributes(attributes);
}

private void restoreScreenBrightness() {
    if (getActivity() == null || getActivity().getWindow() == null) {
        return;
    }

    Window window = getActivity().getWindow();
    WindowManager.LayoutParams attributes = window.getAttributes();

    attributes.screenBrightness = mPreviousBrightness;
    window.setAttributes(attributes);
}

ตั้งค่าโหมด AE เป็น CONTROL_AE_MODE_ON_EXTERNAL_FLASH

CONTROL_AE_MODE_ON_EXTERNAL_FLASH พร้อมใช้งานใน API ระดับ 28 ขึ้นไป แต่โหมด AE นี้อาจไม่พร้อมให้บริการในอุปกรณ์บางเครื่อง ดังนั้นโปรดตรวจสอบว่าโหมด AE ที่พร้อมใช้งาน และกำหนดค่าให้สอดคล้องกัน หากต้องการตรวจสอบห้องว่าง ให้ใช้ CameraCharacteristics#CONTROL_AE_AVAILABLE_MODES

Kotlin

private val characteristics: CameraCharacteristics by lazy {
    cameraManager.getCameraCharacteristics(cameraId)
}

@RequiresApi(Build.VERSION_CODES.P)
private fun isExternalFlashAeModeAvailable() =
    characteristics.get(CameraCharacteristics.CONTROL_AE_AVAILABLE_MODES)
        ?.contains(CaptureRequest.CONTROL_AE_MODE_ON_EXTERNAL_FLASH) ?: false

Java

try {
    mCharacteristics = mCameraManager.getCameraCharacteristics(mCameraId);
} catch (CameraAccessException e) {
    e.printStackTrace();
}

@RequiresApi(Build.VERSION_CODES.P)
private boolean isExternalFlashAeModeAvailable() {
    int[] availableAeModes = mCharacteristics.get(CameraCharacteristics.CONTROL_AE_AVAILABLE_MODES);

    for (int aeMode : availableAeModes) {
        if (aeMode == CaptureRequest.CONTROL_AE_MODE_ON_EXTERNAL_FLASH) {
            return true;
        }
    }
    return false;
}

หากแอปพลิเคชันมีชุดคำขอจับภาพซ้ำ (จำเป็นสำหรับ แสดงตัวอย่าง) โหมด AE จะต้องตั้งค่าเป็นคำขอที่เกิดซ้ำ มิฉะนั้น ถูกลบล้างโดยค่าเริ่มต้นหรือโหมด AE อื่นๆ ที่ผู้ใช้ตั้งไว้ในการทำซ้ำครั้งถัดไป การจับภาพ หากเกิดเหตุการณ์นี้ขึ้น กล้องอาจไม่มีเวลาพอที่จะดำเนินการ ที่ปกติจะดำเนินการกับโหมด AE ของแฟลชภายนอก

เพื่อให้แน่ใจว่ากล้องจะดำเนินการตามคำขออัปเดตโหมด AE ได้โดยสมบูรณ์ โปรดตรวจสอบ ผลการบันทึกใน Callback จับภาพซ้ำและรอให้โหมด AE อัปเดตในผลลัพธ์

บันทึก Callback ที่อาจรอให้อัปเดตโหมด AE

ข้อมูลโค้ดต่อไปนี้แสดงให้เห็นวิธีดำเนินการดังกล่าว

Kotlin

private val repeatingCaptureCallback = object : CameraCaptureSession.CaptureCallback() {
    private var targetAeMode: Int? = null
    private var aeModeUpdateDeferred: CompletableDeferred? = null

    suspend fun awaitAeModeUpdate(targetAeMode: Int) {
        this.targetAeMode = targetAeMode
        aeModeUpdateDeferred = CompletableDeferred()
        // Makes the current coroutine wait until aeModeUpdateDeferred is completed. It is
        // completed once targetAeMode is found in the following capture callbacks
        aeModeUpdateDeferred?.await()
    }

    private fun process(result: CaptureResult) {
        // Checks if AE mode is updated and completes any awaiting Deferred
        aeModeUpdateDeferred?.let {
            val aeMode = result[CaptureResult.CONTROL_AE_MODE]
            if (aeMode == targetAeMode) {
                it.complete(Unit)
            }
        }
    }

    override fun onCaptureCompleted(
        session: CameraCaptureSession,
        request: CaptureRequest,
        result: TotalCaptureResult
    ) {
        super.onCaptureCompleted(session, request, result)
        process(result)
    }
}

Java

static class AwaitingCaptureCallback extends CameraCaptureSession.CaptureCallback {
    private int mTargetAeMode;
    private CountDownLatch mAeModeUpdateLatch = null;

    public void awaitAeModeUpdate(int targetAeMode) {
        mTargetAeMode = targetAeMode;
        mAeModeUpdateLatch = new CountDownLatch(1);
        // Makes the current thread wait until mAeModeUpdateLatch is released, it will be
        // released once targetAeMode is found in the capture callbacks below
        try {
            mAeModeUpdateLatch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    private void process(CaptureResult result) {
        // Checks if AE mode is updated and decrements the count of any awaiting latch
        if (mAeModeUpdateLatch != null) {
            int aeMode = result.get(CaptureResult.CONTROL_AE_MODE);
            if (aeMode == mTargetAeMode) {
                mAeModeUpdateLatch.countDown();
            }
        }
    }

    @Override
    public void onCaptureCompleted(@NonNull CameraCaptureSession session,
            @NonNull CaptureRequest request,
            @NonNull TotalCaptureResult result) {
        super.onCaptureCompleted(session, request, result);
        process(result);
    }
}

private final AwaitingCaptureCallback mRepeatingCaptureCallback = new AwaitingCaptureCallback();

ตั้งค่าคําขอซ้ำเพื่อเปิดหรือปิดใช้โหมด AE

เมื่อมี Callback ที่เป็นการบันทึก ตัวอย่างโค้ดต่อไปนี้จะแสดงวิธีตั้งค่า คำขอซ้ำ

Kotlin

/** [HandlerThread] where all camera operations run */
private val cameraThread = HandlerThread("CameraThread").apply { start() }

/** [Handler] corresponding to [cameraThread] */
private val cameraHandler = Handler(cameraThread.looper)

private suspend fun enableExternalFlashAeMode() {
    if (Build.VERSION.SDK_INT >= 28 && isExternalFlashAeModeAvailable()) {
        session.setRepeatingRequest(
            camera.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW).apply {
                addTarget(previewSurface)
                set(
                    CaptureRequest.CONTROL_AE_MODE,
                    CaptureRequest.CONTROL_AE_MODE_ON_EXTERNAL_FLASH
                )
            }.build(), repeatingCaptureCallback, cameraHandler
        )

        // Wait for the request to be processed by camera
        repeatingCaptureCallback.awaitAeModeUpdate(CaptureRequest.CONTROL_AE_MODE_ON_EXTERNAL_FLASH)
    }
}

private fun disableExternalFlashAeMode() {
    if (Build.VERSION.SDK_INT >= 28 && isExternalFlashAeModeAvailable()) {
        session.setRepeatingRequest(
            camera.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW).apply {
                addTarget(previewSurface)
            }.build(), repeatingCaptureCallback, cameraHandler
        )
    }
}

Java

private void setupCameraThread() {
    // HandlerThread where all camera operations run
    HandlerThread cameraThread = new HandlerThread("CameraThread");
    cameraThread.start();

    // Handler corresponding to cameraThread
    mCameraHandler = new Handler(cameraThread.getLooper());
}

private void enableExternalFlashAeMode() {
    if (Build.VERSION.SDK_INT >= 28 && isExternalFlashAeModeAvailable()) {
        try {
            CaptureRequest.Builder requestBuilder = mCamera.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
            requestBuilder.addTarget(mPreviewSurface);
            requestBuilder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON_EXTERNAL_FLASH);
            mSession.setRepeatingRequest(requestBuilder.build(), mRepeatingCaptureCallback, mCameraHandler);
        } catch (CameraAccessException e) {
            e.printStackTrace();
        }

        // Wait for the request to be processed by camera
        mRepeatingCaptureCallback.awaitAeModeUpdate(CaptureRequest.CONTROL_AE_MODE_ON_EXTERNAL_FLASH);
    }
}

private void disableExternalFlashAeMode() {
    if (Build.VERSION.SDK_INT >= 28 && isExternalFlashAeModeAvailable()) {
        try {
            CaptureRequest.Builder requestBuilder = mCamera.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
            requestBuilder.addTarget(mPreviewSurface);
            mSession.setRepeatingRequest(requestBuilder.build(), mRepeatingCaptureCallback, mCameraHandler);
        } catch (CameraAccessException e) {
            e.printStackTrace();
        }
    }
}

เรียกใช้ลำดับก่อนการจับภาพ

ในการเรียกใช้ลำดับการวัดปริมาณล่วงหน้า คุณสามารถส่ง CaptureRequest กำหนดค่า CONTROL_AE_PRECAPTURE_TRIGGER_START สำหรับคำขอ สิ่งที่คุณต้องทำ รอให้คำขอได้รับการประมวลผล จากนั้นรอ AE และ AWB บรรจบกัน

แม้ว่าทริกเกอร์ Precapture ที่มีคำขอจับภาพเดียว แต่กำลังรอ AE และการบรรจบกันของ AWB จำเป็นต้องมีความซับซ้อนมากกว่า คุณสามารถติดตาม สถานะ AE และสถานะ AWB ใช้บันทึก Callback ที่ตั้งค่าเป็นคำขอซ้ำ

การอัปเดต Callback ที่เกิดซ้ำตัวเดียวกันจะช่วยให้คุณเขียนโค้ดได้ง่ายๆ แอปพลิเคชันมักกำหนดให้มีหน้าตัวอย่างที่สร้างขึ้นคำขอซ้ำ ขณะตั้งค่ากล้อง คุณสามารถตั้งค่าการเรียกกลับการจับภาพซ้ำเป็น คำขอนั้นเริ่มต้น 1 ครั้ง จากนั้นใช้ซ้ำเพื่อตรวจสอบผลลัพธ์ การรอคอย

บันทึกการอัปเดตโค้ด Callback เพื่อรอ Convergence

หากต้องการอัปเดต Callback ที่บันทึกซ้ำ ให้ใช้ข้อมูลโค้ดต่อไปนี้

Kotlin

private val repeatingCaptureCallback = object : CameraCaptureSession.CaptureCallback() {
    private var targetAeMode: Int? = null
    private var aeModeUpdateDeferred: CompletableDeferred? = null

    private var convergenceDeferred: CompletableDeferred? = null

    suspend fun awaitAeModeUpdate(targetAeMode: Int) {
        this.targetAeMode = targetAeMode
        aeModeUpdateDeferred = CompletableDeferred()
        // Makes the current coroutine wait until aeModeUpdateDeferred is completed. It is
        // completed once targetAeMode is found in the following capture callbacks
        aeModeUpdateDeferred?.await()
    }

    suspend fun awaitAeAwbConvergence() {
        convergenceDeferred = CompletableDeferred()
        // Makes the current coroutine wait until convergenceDeferred is completed, it will be
        // completed once both AE & AWB are reported as converged in the capture callbacks below
        convergenceDeferred?.await()
    }

    private fun process(result: CaptureResult) {
        // Checks if AE mode is updated and completes any awaiting Deferred
        aeModeUpdateDeferred?.let {
            val aeMode = result[CaptureResult.CONTROL_AE_MODE]
            if (aeMode == targetAeMode) {
                it.complete(Unit)
            }
        }

        // Checks for convergence and completes any awaiting Deferred
        convergenceDeferred?.let {
            val aeState = result[CaptureResult.CONTROL_AE_STATE]
            val awbState = result[CaptureResult.CONTROL_AWB_STATE]

            val isAeReady = (
                    aeState == null // May be null in some devices (e.g. legacy camera HW level)
                            || aeState == CaptureResult.CONTROL_AE_STATE_CONVERGED
                            || aeState == CaptureResult.CONTROL_AE_STATE_FLASH_REQUIRED
                    )

            val isAwbReady = (
                    awbState == null // May be null in some devices (e.g. legacy camera HW level)
                            || awbState == CaptureResult.CONTROL_AWB_STATE_CONVERGED
                    )

            if (isAeReady && isAwbReady) {
                // if any non-null convergenceDeferred is set, complete it
                it.complete(Unit)
            }
        }
    }

    override fun onCaptureCompleted(
        session: CameraCaptureSession,
        request: CaptureRequest,
        result: TotalCaptureResult
    ) {
        super.onCaptureCompleted(session, request, result)
        process(result)
    }
}

Java

static class AwaitingCaptureCallback extends CameraCaptureSession.CaptureCallback {
    private int mTargetAeMode;
    private CountDownLatch mAeModeUpdateLatch = null;

    private CountDownLatch mConvergenceLatch = null;

    public void awaitAeModeUpdate(int targetAeMode) {
        mTargetAeMode = targetAeMode;
        mAeModeUpdateLatch = new CountDownLatch(1);
        // Makes the current thread wait until mAeModeUpdateLatch is released, it will be
        // released once targetAeMode is found in the capture callbacks below
        try {
            mAeModeUpdateLatch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public void awaitAeAwbConvergence() {
        mConvergenceLatch = new CountDownLatch(1);
        // Makes the current coroutine wait until mConvergenceLatch is released, it will be
        // released once both AE & AWB are reported as converged in the capture callbacks below
        try {
            mConvergenceLatch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    private void process(CaptureResult result) {
        // Checks if AE mode is updated and decrements the count of any awaiting latch
        if (mAeModeUpdateLatch != null) {
            int aeMode = result.get(CaptureResult.CONTROL_AE_MODE);
            if (aeMode == mTargetAeMode) {
                mAeModeUpdateLatch.countDown();
            }
        }

        // Checks for convergence and decrements the count of any awaiting latch
        if (mConvergenceLatch != null) {
            Integer aeState = result.get(CaptureResult.CONTROL_AE_STATE);
            Integer awbState = result.get(CaptureResult.CONTROL_AWB_STATE);

            boolean isAeReady = (
                    aeState == null // May be null in some devices (e.g. legacy camera HW level)
                            || aeState == CaptureResult.CONTROL_AE_STATE_CONVERGED
                            || aeState == CaptureResult.CONTROL_AE_STATE_FLASH_REQUIRED
            );

            boolean isAwbReady = (
                    awbState == null // May be null in some devices (e.g. legacy camera HW level)
                            || awbState == CaptureResult.CONTROL_AWB_STATE_CONVERGED
            );

            if (isAeReady && isAwbReady) {
                mConvergenceLatch.countDown();
                mConvergenceLatch = null;
            }
        }
    }

    @Override
    public void onCaptureCompleted(@NonNull CameraCaptureSession session,
            @NonNull CaptureRequest request,
            @NonNull TotalCaptureResult result) {
        super.onCaptureCompleted(session, request, result);
        process(result);
    }
}

ตั้งค่า Callback เป็นคำขอซ้ำระหว่างการตั้งค่ากล้อง

ตัวอย่างโค้ดต่อไปนี้ช่วยให้คุณตั้งค่า Callback เป็นคำขอซ้ำได้ ในระหว่างการเริ่มต้น

Kotlin

// Open the selected camera
camera = openCamera(cameraManager, cameraId, cameraHandler)

// Creates list of Surfaces where the camera will output frames
val targets = listOf(previewSurface, imageReaderSurface)

// Start a capture session using our open camera and list of Surfaces where frames will go
session = createCameraCaptureSession(camera, targets, cameraHandler)

val captureRequest = camera.createCaptureRequest(
        CameraDevice.TEMPLATE_PREVIEW).apply { addTarget(previewSurface) }

// This will keep sending the capture request as frequently as possible until the
// session is torn down or session.stopRepeating() is called
session.setRepeatingRequest(captureRequest.build(), repeatingCaptureCallback, cameraHandler)

Java

// Open the selected camera
mCamera = openCamera(mCameraManager, mCameraId, mCameraHandler);

// Creates list of Surfaces where the camera will output frames
List targets = new ArrayList<>(Arrays.asList(mPreviewSurface, mImageReaderSurface));

// Start a capture session using our open camera and list of Surfaces where frames will go
mSession = createCaptureSession(mCamera, targets, mCameraHandler);

try {
    CaptureRequest.Builder requestBuilder = mCamera.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
    requestBuilder.addTarget(mPreviewSurface);

    // This will keep sending the capture request as frequently as possible until the
    // session is torn down or session.stopRepeating() is called
    mSession.setRepeatingRequest(requestBuilder.build(), mRepeatingCaptureCallback, mCameraHandler);
} catch (CameraAccessException e) {
    e.printStackTrace();
}

การทริกเกอร์และรอลำดับล่วงหน้า

เมื่อตั้งค่า Callback แล้ว คุณจะใช้ตัวอย่างโค้ดต่อไปนี้เพื่อเรียกและรอลำดับล่วงหน้าได้

Kotlin

private suspend fun runPrecaptureSequence() {
    // Creates a new capture request with CONTROL_AE_PRECAPTURE_TRIGGER_START
    val captureRequest = session.device.createCaptureRequest(
        CameraDevice.TEMPLATE_PREVIEW
    ).apply {
        addTarget(previewSurface)
        set(
            CaptureRequest.CONTROL_AE_PRECAPTURE_TRIGGER,
            CaptureRequest.CONTROL_AE_PRECAPTURE_TRIGGER_START
        )
    }

    val precaptureDeferred = CompletableDeferred()
    session.capture(captureRequest.build(), object: CameraCaptureSession.CaptureCallback() {
        override fun onCaptureCompleted(
            session: CameraCaptureSession,
            request: CaptureRequest,
            result: TotalCaptureResult
        ) {
            // Waiting for this callback ensures the precapture request has been processed
            precaptureDeferred.complete(Unit)
        }
    }, cameraHandler)

    precaptureDeferred.await()

    // Precapture trigger request has been processed, we can wait for AE & AWB convergence now
    repeatingCaptureCallback.awaitAeAwbConvergence()
}

Java

private void runPrecaptureSequence() {
    // Creates a new capture request with CONTROL_AE_PRECAPTURE_TRIGGER_START
    try {
        CaptureRequest.Builder requestBuilder =
                mSession.getDevice().createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
        requestBuilder.addTarget(mPreviewSurface);
        requestBuilder.set(CaptureRequest.CONTROL_AE_PRECAPTURE_TRIGGER,
                CaptureRequest.CONTROL_AE_PRECAPTURE_TRIGGER_START);

        CountDownLatch precaptureLatch = new CountDownLatch(1);
        mSession.capture(requestBuilder.build(), new CameraCaptureSession.CaptureCallback() {
            @Override
            public void onCaptureCompleted(@NonNull CameraCaptureSession session,
                                           @NonNull CaptureRequest request,
                                           @NonNull TotalCaptureResult result) {
                Log.d(TAG, "CONTROL_AE_PRECAPTURE_TRIGGER_START processed");
                // Waiting for this callback ensures the precapture request has been processed
                precaptureLatch.countDown();
            }
        }, mCameraHandler);

        precaptureLatch.await();

        // Precapture trigger request has been processed, we can wait for AE & AWB convergence now
        mRepeatingCaptureCallback.awaitAeAwbConvergence();
    } catch (CameraAccessException | InterruptedException e) {
        e.printStackTrace();
    }
}

ต่อทุกอย่างเข้าด้วยกัน

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

Kotlin

// User clicks captureButton to take picture
captureButton.setOnClickListener { v ->
    // Apply the screen flash related UI changes
    whiteColorOverlayView.visibility = View.VISIBLE
    maximizeScreenBrightness()

    // Perform I/O heavy operations in a different scope
    lifecycleScope.launch(Dispatchers.IO) {
        // Enable external flash AE mode and wait for it to be processed
        enableExternalFlashAeMode()

        // Run precapture sequence and wait for it to complete
        runPrecaptureSequence()

        // Start taking picture and wait for it to complete
        takePhoto()

        disableExternalFlashAeMode()
        v.post {
            // Clear the screen flash related UI changes
            restoreScreenBrightness()
            whiteColorOverlayView.visibility = View.INVISIBLE
        }
    }
}

Java

// User clicks captureButton to take picture
mCaptureButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        // Apply the screen flash related UI changes
        mWhiteColorOverlayView.setVisibility(View.VISIBLE);
        maximizeScreenBrightness();

        // Perform heavy operations in a different thread
        Executors.newSingleThreadExecutor().execute(() -> {
            // Enable external flash AE mode and wait for it to be processed
            enableExternalFlashAeMode();

            // Run precapture sequence and wait for it to complete
            runPrecaptureSequence();

            // Start taking picture and wait for it to complete
            takePhoto();

            disableExternalFlashAeMode();

            v.post(() -> {
                // Clear the screen flash related UI changes
                restoreScreenBrightness();
                mWhiteColorOverlayView.setVisibility(View.INVISIBLE);
            });
        });
    }
});

รูปภาพตัวอย่าง

ดูสิ่งที่จะเกิดขึ้นเมื่อแฟลชหน้าจอแสดงได้จากตัวอย่างต่อไปนี้ ไม่ถูกต้อง และนำไปใช้อย่างถูกต้องเมื่อใด

เมื่อทำผิด

หากใช้แฟลชหน้าจอไม่ถูกต้อง คุณจะได้รับผลลัพธ์ที่ไม่สอดคล้องกัน การจับภาพ อุปกรณ์ต่างๆ และสภาพแสง บ่อยครั้งที่ภาพจากภาพที่ถ่าย ปัญหาการรับแสงหรือสีย้อมสีที่ไม่ดี สำหรับอุปกรณ์บางประเภท ข้อบกพร่องประเภทนี้ เห็นได้ชัดเจนมากขึ้นในสภาพแสงหนึ่งๆ เช่น แสงน้อย แทนที่สภาพแวดล้อมที่มืดสนิท

โดยตารางต่อไปนี้จะแสดงตัวอย่างของปัญหาดังกล่าว โดยจะได้รับใน โครงสร้างพื้นฐานของห้องทดลอง CameraX โดยมีแหล่งกำเนิดแสงเป็นสีขาวนวล สี แหล่งแสงสีขาวโทนอุ่นนี้ช่วยให้คุณเห็นได้ว่าสีฟ้าอ่อนนั้น ปัญหาที่เกิดขึ้นจริง ไม่ใช่ผลกระทบข้างเคียงของแหล่งกำเนิดแสง

สภาพแวดล้อม แสงน้อย สว่างเกินไป สีจาง
สภาพแวดล้อมที่มืด (ไม่มีแหล่งกำเนิดแสงนอกจากโทรศัพท์) รูปภาพเกือบมืดสนิท รูปภาพสว่างเกินไป รูปภาพที่มีโทนม่วง
แสงน้อย (แหล่งแสงเพิ่มเติมประมาณ 3 ลักซ์) รูปภาพค่อนข้างมืด รูปภาพสว่างเกินไป รูปภาพที่มีสีฟ้าอมน้ำเงิน

เมื่อทำถูกต้องแล้ว

เมื่อมีการใช้การใช้งานมาตรฐานสำหรับอุปกรณ์และเงื่อนไขเดียวกัน คุณจะเห็นผลลัพธ์ในตารางต่อไปนี้

สภาพแวดล้อม แสงน้อย (คงที่) การมองเห็นมาก (คงที่) การย้อมสี (คงที่)
สภาพแวดล้อมที่มืด (ไม่มีแหล่งกำเนิดแสงนอกจากโทรศัพท์) ล้างรูปภาพ ล้างรูปภาพ ล้างรูปภาพโดยไม่แต้มสี
แสงน้อย (แหล่งแสงเพิ่มเติมประมาณ 3 ลักซ์) ล้างรูปภาพ ล้างรูปภาพ รูปภาพคมชัดโดยไม่ต้องแต้มสี

จากที่สังเกตพบว่า คุณภาพของรูปดีขึ้นอย่างมากตามมาตรฐาน การใช้งานของคุณ