mirror of
https://github.com/Genymobile/scrcpy.git
synced 2026-02-19 12:54:30 +01:00
Add shortcuts to change the camera zoom
MOD+up and MOD+zoom change the camera zoom. TODO ref 6243 Signed-off-by: Romain Vimont <rom@rom1v.com>
This commit is contained in:
@@ -26,6 +26,8 @@ public final class ControlMessage {
|
||||
public static final int TYPE_START_APP = 16;
|
||||
public static final int TYPE_RESET_VIDEO = 17;
|
||||
public static final int TYPE_CAMERA_SET_TORCH = 18;
|
||||
public static final int TYPE_CAMERA_ZOOM_IN = 19;
|
||||
public static final int TYPE_CAMERA_ZOOM_OUT = 20;
|
||||
|
||||
public static final long SEQUENCE_INVALID = 0;
|
||||
|
||||
|
||||
@@ -47,6 +47,8 @@ public class ControlMessageReader {
|
||||
case ControlMessage.TYPE_ROTATE_DEVICE:
|
||||
case ControlMessage.TYPE_OPEN_HARD_KEYBOARD_SETTINGS:
|
||||
case ControlMessage.TYPE_RESET_VIDEO:
|
||||
case ControlMessage.TYPE_CAMERA_ZOOM_IN:
|
||||
case ControlMessage.TYPE_CAMERA_ZOOM_OUT:
|
||||
return ControlMessage.createEmpty(type);
|
||||
case ControlMessage.TYPE_UHID_CREATE:
|
||||
return parseUhidCreate();
|
||||
|
||||
@@ -376,6 +376,12 @@ public class Controller implements AsyncProcessor, VirtualDisplayListener {
|
||||
case ControlMessage.TYPE_CAMERA_SET_TORCH:
|
||||
cameraCapture.setTorchEnabled(msg.getOn());
|
||||
return true;
|
||||
case ControlMessage.TYPE_CAMERA_ZOOM_IN:
|
||||
cameraCapture.zoomIn();
|
||||
return true;
|
||||
case ControlMessage.TYPE_CAMERA_ZOOM_OUT:
|
||||
cameraCapture.zoomOut();
|
||||
return true;
|
||||
default:
|
||||
// fall through
|
||||
}
|
||||
|
||||
@@ -54,6 +54,8 @@ public class CameraCapture extends SurfaceCapture {
|
||||
0, 1, 0, 1, // column 4
|
||||
};
|
||||
|
||||
private static final float ZOOM_FACTOR = 1 + 1 / 16f;
|
||||
|
||||
private final String explicitCameraId;
|
||||
private final CameraFacing cameraFacing;
|
||||
private final Size explicitSize;
|
||||
@@ -472,6 +474,36 @@ public class CameraCapture extends SurfaceCapture {
|
||||
});
|
||||
}
|
||||
|
||||
private void zoom(boolean in) {
|
||||
cameraHandler.post(() -> {
|
||||
assertCameraThread();
|
||||
if (currentSession != null && requestBuilder != null) {
|
||||
// Always align to log values
|
||||
double z = Math.round(Math.log(zoom) / Math.log(ZOOM_FACTOR));
|
||||
double dir = in ? 1 : -1;
|
||||
zoom = (float) Math.pow(ZOOM_FACTOR, z + dir);
|
||||
|
||||
try {
|
||||
zoom = clampZoom(zoom);
|
||||
Ln.i("Set camera zoom: " + zoom);
|
||||
requestBuilder.set(CaptureRequest.CONTROL_ZOOM_RATIO, zoom);
|
||||
CaptureRequest request = requestBuilder.build();
|
||||
setRepeatingRequest(currentSession, request);
|
||||
} catch (CameraAccessException e) {
|
||||
Ln.e("Camera error", e);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void zoomIn() {
|
||||
zoom(true);
|
||||
}
|
||||
|
||||
public void zoomOut() {
|
||||
zoom(false);
|
||||
}
|
||||
|
||||
private float clampZoom(float value) {
|
||||
assertCameraThread();
|
||||
if (zoomRange == null) {
|
||||
|
||||
@@ -440,6 +440,38 @@ public class ControlMessageReaderTest {
|
||||
Assert.assertEquals(-1, bis.read()); // EOS
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParseCameraZoomIn() throws IOException {
|
||||
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
||||
DataOutputStream dos = new DataOutputStream(bos);
|
||||
dos.writeByte(ControlMessage.TYPE_CAMERA_ZOOM_IN);
|
||||
byte[] packet = bos.toByteArray();
|
||||
|
||||
ByteArrayInputStream bis = new ByteArrayInputStream(packet);
|
||||
ControlMessageReader reader = new ControlMessageReader(bis);
|
||||
|
||||
ControlMessage event = reader.read();
|
||||
Assert.assertEquals(ControlMessage.TYPE_CAMERA_ZOOM_IN, event.getType());
|
||||
|
||||
Assert.assertEquals(-1, bis.read()); // EOS
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParseCameraZoomOut() throws IOException {
|
||||
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
||||
DataOutputStream dos = new DataOutputStream(bos);
|
||||
dos.writeByte(ControlMessage.TYPE_CAMERA_ZOOM_OUT);
|
||||
byte[] packet = bos.toByteArray();
|
||||
|
||||
ByteArrayInputStream bis = new ByteArrayInputStream(packet);
|
||||
ControlMessageReader reader = new ControlMessageReader(bis);
|
||||
|
||||
ControlMessage event = reader.read();
|
||||
Assert.assertEquals(ControlMessage.TYPE_CAMERA_ZOOM_OUT, event.getType());
|
||||
|
||||
Assert.assertEquals(-1, bis.read()); // EOS
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMultiEvents() throws IOException {
|
||||
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
||||
|
||||
Reference in New Issue
Block a user