Skip to content

Commit 2564fea

Browse files
committed
Merge remote-tracking branch 'origin/autofocus_control_branch' into remotecontrolling
2 parents 13d592c + b2cccb3 commit 2564fea

File tree

6 files changed

+291
-19
lines changed

6 files changed

+291
-19
lines changed

PythonTools/RemoteController.py

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,28 @@ def startRec(self):
115115
self.save_user_prefs()
116116
sys.exit()
117117

118+
def enableFocus(self):
119+
try:
120+
if self.unlock_focus_btn.isEnabled():
121+
self.ws.send("UNLOCK_FOCUS")
122+
self.unlock_focus_btn.setEnabled(False)
123+
self.lock_focus_btn.setEnabled(True)
124+
except Exception as e:
125+
self.show_error_popup(f"Can't send enable focus command: {e}")
126+
self.save_user_prefs()
127+
sys.exit()
128+
129+
def lockFocus(self):
130+
try:
131+
if self.lock_focus_btn.isEnabled():
132+
self.ws.send("LOCK_FOCUS")
133+
self.unlock_focus_btn.setEnabled(True)
134+
self.lock_focus_btn.setEnabled(False)
135+
except Exception as e:
136+
self.show_error_popup(f"Can't send lock focus command: {e}")
137+
self.save_user_prefs()
138+
sys.exit()
139+
118140
def stopRec(self):
119141
if self.stop_btn.isEnabled() and not self.start_btn.isEnabled():
120142
self.stop_btn.setEnabled(False)
@@ -255,14 +277,28 @@ def setupUi(self, MainWindow):
255277
self.stop_btn.clicked.connect(self.stopRec)
256278
self.stop_btn.setEnabled(False)
257279

280+
self.unlock_focus_btn = QtWidgets.QPushButton()
281+
self.unlock_focus_btn.setFont(font)
282+
self.unlock_focus_btn.setObjectName("unlock_focus_button")
283+
self.unlock_focus_btn.clicked.connect(self.enableFocus)
284+
285+
self.lock_focus_btn = QtWidgets.QPushButton()
286+
self.lock_focus_btn.setFont(font)
287+
self.lock_focus_btn.setObjectName("lock_focus_button")
288+
self.lock_focus_btn.clicked.connect(self.lockFocus)
289+
self.lock_focus_btn.setEnabled(False)
290+
258291
record_layout = QHBoxLayout()
259292
record_layout.addStretch(1)
260293
record_layout.addWidget(self.start_btn, 1)
261294
record_layout.addWidget(self.record_label)
262295
# record_layout.addStretch(1)
263296
record_layout.addWidget(self.stop_btn, 1)
264297
record_layout.addStretch(1)
265-
298+
record_layout.addWidget(self.unlock_focus_btn, 1)
299+
record_layout.addStretch(1)
300+
record_layout.addWidget(self.lock_focus_btn, 1)
301+
record_layout.addStretch(1)
266302
#
267303
# CLIENTS STATUS
268304
self.status_clear_btn = QtWidgets.QPushButton()
@@ -428,6 +464,8 @@ def retranslateUi(self, MainWindow):
428464

429465
self.download_prefix_text.setPlaceholderText(_translate("MainWindow", " Enter Session Prefix"))
430466
self.download_btn.setText(_translate("MainWindow", "Download"))
467+
self.unlock_focus_btn.setText(_translate("MainWindow", "Unlock Focus"))
468+
self.lock_focus_btn.setText(_translate("MainWindow", "Lock Focus"))
431469

432470
self.prefix_list_btn.setText(_translate("MainWindow", "Prefix List"))
433471
self.phase_align_btn.setText(_translate("MainWindow", "Align Phases"))

app/src/main/java/com/googleresearch/capturesync/CaptureRequestFactory.java

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,30 @@
1717
package com.googleresearch.capturesync;
1818

1919
import static android.hardware.camera2.CameraDevice.TEMPLATE_PREVIEW;
20+
import static android.hardware.camera2.CameraDevice.TEMPLATE_MANUAL;
2021
import static android.hardware.camera2.CameraMetadata.CONTROL_AE_MODE_OFF;
21-
import static android.hardware.camera2.CameraMetadata.CONTROL_AF_MODE_AUTO;
22+
23+
import static android.hardware.camera2.CameraMetadata.CONTROL_AF_MODE_OFF;
24+
import static android.hardware.camera2.CameraMetadata.CONTROL_AF_TRIGGER_CANCEL;
25+
2226
import static android.hardware.camera2.CameraMetadata.CONTROL_AWB_MODE_AUTO;
2327
import static android.hardware.camera2.CameraMetadata.CONTROL_MODE_AUTO;
2428
import static android.hardware.camera2.CaptureRequest.CONTROL_AE_MODE;
2529
import static android.hardware.camera2.CaptureRequest.CONTROL_AF_MODE;
2630
import static android.hardware.camera2.CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE;
31+
import static android.hardware.camera2.CaptureRequest.CONTROL_AF_TRIGGER;
2732
import static android.hardware.camera2.CaptureRequest.CONTROL_AWB_MODE;
2833
import static android.hardware.camera2.CaptureRequest.CONTROL_MODE;
34+
import static android.hardware.camera2.CaptureRequest.LENS_FOCUS_DISTANCE;
2935
import static android.hardware.camera2.CaptureRequest.SENSOR_EXPOSURE_TIME;
3036
import static android.hardware.camera2.CaptureRequest.SENSOR_SENSITIVITY;
3137

3238
import android.hardware.camera2.CameraAccessException;
3339
import android.hardware.camera2.CameraDevice;
3440
import android.hardware.camera2.CaptureRequest;
3541
import android.view.Surface;
42+
import android.widget.Toast;
43+
3644
import com.googleresearch.capturesync.ImageMetadataSynchronizer.CaptureRequestTag;
3745
import java.util.ArrayList;
3846
import java.util.List;
@@ -55,27 +63,23 @@ public CaptureRequest.Builder makePreview(
5563
List<Surface> imageSurfaces,
5664
long sensorExposureTimeNs,
5765
int sensorSensitivity,
58-
boolean wantAutoExp)
66+
boolean wantAutoExp, boolean enableFocus)
5967
throws CameraAccessException {
6068

6169
CaptureRequest.Builder builder = device.createCaptureRequest(TEMPLATE_PREVIEW);
6270
if (wantAutoExp) {
6371
builder.set(CONTROL_AE_MODE, CONTROL_AWB_MODE_AUTO);
64-
6572
} else {
6673
// Manually set exposure and sensitivity using UI sliders on the leader.
6774
builder.set(CONTROL_AE_MODE, CONTROL_AE_MODE_OFF);
6875
builder.set(SENSOR_EXPOSURE_TIME, sensorExposureTimeNs);
6976
builder.set(SENSOR_SENSITIVITY, sensorSensitivity);
7077
}
71-
78+
builder.set(CONTROL_AF_TRIGGER, CONTROL_AF_TRIGGER_CANCEL);
79+
builder.set(CONTROL_AF_MODE, CONTROL_AF_MODE_OFF);
7280
// Auto white balance used, these could be locked and sent from the leader instead.
7381
builder.set(CONTROL_AWB_MODE, CONTROL_AWB_MODE_AUTO);
7482

75-
// Auto focus is used since different devices may have different manual focus values.
76-
//builder.set(CONTROL_AF_MODE, CONTROL_AF_MODE_CONTINUOUS_PICTURE);
77-
builder.set(CONTROL_AF_MODE, CONTROL_AF_MODE_AUTO);
78-
7983
if (viewfinderSurface != null) {
8084
builder.addTarget(viewfinderSurface);
8185
}
@@ -88,6 +92,7 @@ public CaptureRequest.Builder makePreview(
8892
return builder;
8993
}
9094

95+
9196
/**
9297
* An alternative capture request for video,
9398
* includes everything from preview + mediaRecorder
@@ -98,9 +103,9 @@ public CaptureRequest.Builder makeVideo(
98103
List<Surface> imageSurfaces,
99104
long sensorExposureTimeNs,
100105
int sensorSensitivity,
101-
boolean wantAutoExp)
106+
boolean wantAutoExp, boolean enableFocus)
102107
throws CameraAccessException {
103-
CaptureRequest.Builder builder = makePreview(viewfinderSurface, imageSurfaces, sensorExposureTimeNs, sensorSensitivity, wantAutoExp);
108+
CaptureRequest.Builder builder = makePreview(viewfinderSurface, imageSurfaces, sensorExposureTimeNs, sensorSensitivity, wantAutoExp, enableFocus);
104109
// Add recorder surface
105110
if (recorderSurface != null) {
106111
builder.addTarget(recorderSurface);
@@ -111,10 +116,13 @@ public CaptureRequest.Builder makeVideo(
111116

112117
public CaptureRequest.Builder makeFrameInjectionRequest(
113118
long desiredExposureTimeNs, List<Surface> imageSurfaces) throws CameraAccessException {
119+
114120
CaptureRequest.Builder builder = device.createCaptureRequest(TEMPLATE_PREVIEW);
115121
builder.set(CONTROL_MODE, CONTROL_MODE_AUTO);
116122
builder.set(CONTROL_AE_MODE, CONTROL_AE_MODE_OFF);
117123
builder.set(SENSOR_EXPOSURE_TIME, desiredExposureTimeNs);
124+
builder.set(CONTROL_AF_TRIGGER, CONTROL_AF_TRIGGER_CANCEL);
125+
builder.set(CONTROL_AF_MODE, CONTROL_AF_MODE_OFF);
118126
// TODO: Inserting frame duration directly would be more accurate than inserting exposure since
119127
// {@code frame duration ~ exposure + variable overhead}. However setting frame duration may not
120128
// be supported on many android devices, so we use exposure time here.

app/src/main/java/com/googleresearch/capturesync/ImageMetadataSynchronizer.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616

1717
package com.googleresearch.capturesync;
1818

19+
import static android.hardware.camera2.CameraMetadata.CONTROL_AF_MODE_AUTO;
20+
import static android.hardware.camera2.CameraMetadata.CONTROL_AF_MODE_OFF;
21+
import static android.hardware.camera2.CaptureRequest.CONTROL_AF_MODE;
22+
1923
import android.hardware.camera2.CameraCaptureSession;
2024
import android.hardware.camera2.CameraCaptureSession.CaptureCallback;
2125
import android.hardware.camera2.CaptureRequest;
@@ -377,6 +381,13 @@ private void createCaptureCallback() {
377381
@Override
378382
public void onCaptureCompleted(
379383
CameraCaptureSession session, CaptureRequest request, TotalCaptureResult result) {
384+
Float focusDistance = result.get(CaptureResult.LENS_FOCUS_DISTANCE);
385+
if (focusDistance != null) {
386+
context.setCurrentFocusDistance(focusDistance);
387+
} else {
388+
focusDistance = context.getCurrentFocusDistance();
389+
context.setCurrentFocusDistance(0.0f);
390+
}
380391
if (closed) {
381392
return;
382393
}

0 commit comments

Comments
 (0)