Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
CameraStillPhotoPrecaptureCallback.java
Go to the documentation of this file.
1// Copyright (C) 2026 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3package org.qtproject.qt.android.multimedia.qffmpeg;
4
5import android.hardware.camera2.CameraAccessException;
6import android.hardware.camera2.CameraCaptureSession;
7import android.hardware.camera2.CaptureFailure;
8import android.hardware.camera2.CaptureResult;
9import android.hardware.camera2.CaptureRequest;
10import android.hardware.camera2.TotalCaptureResult;
11import android.os.SystemClock;
12import android.util.Log;
13
14// This callback class is meant to be used a repeating request during still photo capture, when
15// waiting for the auto-focus and auto-exposure calibration to lock in. Once done,
16// it will finalize the still photo and then return the camera to previewing.
17//
18// All the events here are invoked from the background processing thread.
19class CameraStillPhotoPrecaptureCallback extends CameraCaptureSession.CaptureCallback {
20 QtCamera2 mMainCameraObject = null;
21
22 // Holds a copy of the camera settings that were to be used when the still photo
23 // was started.
24 CameraSettings mCameraSettings = null;
25 boolean mWaitForAutoFocus = false;
26 boolean mWaitForAutoExposure = false;
27 // Testing has showed that this repeating request will keep on invoking methods on this
28 // callback object, even after we have submitted a new request. This can make it hard
29 // to differentiate between callbacks to this object, or new instances that have been
30 // resubmitted for capture. This boolean tracks whether we should be processing incoming
31 // events.
32 boolean mShouldProcessIncomingEvents = true;
33
34 // Auto-focus and auto-exposure is not guaranteed to ever settle,
35 // i.e when facing completely black scene. Use timeout if this happens.
36 private static final long PRECAPTURE_TIMEOUT_MS = 3000;
37 private final long mPrecaptureDeadline = SystemClock.elapsedRealtime() + PRECAPTURE_TIMEOUT_MS;
38
44
45 CameraStillPhotoPrecaptureCallback(
46 QtCamera2 mainCameraObject,
47 CameraSettings cameraSettings,
48 boolean waitForAutoFocus,
49 boolean waitForAutoExposure)
50 {
51 assert(mainCameraObject != null);
52 assert(cameraSettings != null);
53
54 mMainCameraObject = mainCameraObject;
55 mCameraSettings = cameraSettings;
56 mWaitForAutoFocus = waitForAutoFocus;
57 mWaitForAutoExposure = waitForAutoExposure;
58 }
59
60 boolean capturingWithAutoFlash() {
61 return mWaitForAutoExposure
62 && mCameraSettings.mStillPhotoFlashMode == CaptureRequest.CONTROL_AE_MODE_ON_AUTO_FLASH;
63 }
64
65 private void onCaptureFailureEvent() {
66 mShouldProcessIncomingEvents = false;
67
68 mMainCameraObject.onStillPhotoCaptureFailed(mMainCameraObject.getQAndroidCameraPtr());
69
70 synchronized (mMainCameraObject.mSyncedMembers) {
71 mMainCameraObject.mSyncedMembers.endStillPhoto();
72 }
73
74 // Try to reset our camera to regular preview
75 try {
76 mMainCameraObject.setRepeatingRequestToPreview();
77 } catch (Exception e) {
78 // TODO: If we fail to go back into preview, we can clean up the camera session and
79 // set the QCamera to inactive.
80 }
81 }
82
83 @Override
84 public void onCaptureFailed(
85 CameraCaptureSession session,
86 CaptureRequest request,
87 CaptureFailure failure)
88 {
89 onCaptureFailureEvent();
90 }
91
92 // Returns the operation we should do as a result of processing the result.
93 private PrecaptureOperation determinePrecaptureOperation(CaptureResult result) {
94 Integer afState = result.get(CaptureResult.CONTROL_AF_STATE);
95 Integer aeState = result.get(CaptureResult.CONTROL_AE_STATE);
96
97 // If we are calibrating with auto flash and we receive FLASH_REQUIRED,
98 // we don't have to care about auto focus. Just transition straight to resubmitting
99 // the still photo request with flash forced on.
100 if (capturingWithAutoFlash() && aeState == CaptureResult.CONTROL_AE_STATE_FLASH_REQUIRED) {
101 return PrecaptureOperation.RESUBMIT_WITH_FORCED_FLASH;
102 }
103
104 // If we're not waiting for anything, finalize still photo immediately
105 if (!mWaitForAutoFocus && !mWaitForAutoExposure) {
107 }
108
109 // Wait for focus only
110 if (mWaitForAutoFocus && QtCamera2.afStateIsReadyForCapture(afState)
111 && !mWaitForAutoExposure)
112 {
114 }
115
116 // Wait for exposure only
117 if (!mWaitForAutoFocus
118 && mWaitForAutoExposure && QtCamera2.aeStateIsReadyForCapture(aeState))
119 {
121 }
122
123 // Wait for focus and exposure
124 if (mWaitForAutoFocus && QtCamera2.afStateIsReadyForCapture(afState)
125 && mWaitForAutoExposure && QtCamera2.aeStateIsReadyForCapture(aeState))
126 {
128 }
129
130 if (SystemClock.elapsedRealtime() < mPrecaptureDeadline)
132
133 Log.w(
134 QtCamera2.LOG_TAG,
135 "Auto-focus and auto-exposure did not settle within " + PRECAPTURE_TIMEOUT_MS +
136 " ms, capturing anyway. AF state: " + afState + ", AE state: " + aeState);
138 }
139
140 @Override
141 public void onCaptureCompleted(
142 CameraCaptureSession s,
143 CaptureRequest r,
144 TotalCaptureResult result)
145 {
146 if (!mShouldProcessIncomingEvents)
147 return;
148
149 final PrecaptureOperation operation = determinePrecaptureOperation(result);
150 try {
151 switch (operation) {
152 case FINALIZE_CAPTURE:
153 mMainCameraObject.finalizeStillPhoto(mCameraSettings);
154 break;
155 case RESUBMIT_WITH_FORCED_FLASH:
156 // Submit a new still photo capture as if we were forcing flash on.
157 CameraSettings newCameraSettings = new CameraSettings(mCameraSettings);
158 newCameraSettings.mStillPhotoFlashMode = CaptureRequest.CONTROL_AE_MODE_ON_ALWAYS_FLASH;
159 mMainCameraObject.submitNewStillPhotoCapture(newCameraSettings);
160 break;
161 default:
162 // Do nothing; wait for next result
163 break;
164 }
165 } catch (Exception e) {
166 onCaptureFailureEvent();
167 }
168
169 if (operation != PrecaptureOperation.WAIT) {
170 mShouldProcessIncomingEvents = false;
171 }
172 }
173}
#define assert
GLboolean r
GLdouble s
[6]
Definition qopenglext.h:235
GLuint64EXT * result
[6]
QNetworkRequest request(url)
[0]