Skip to content

Instantly share code, notes, and snippets.

@KushtrimPacaj
Created December 10, 2018 14:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KushtrimPacaj/ac5b59b5322dc5a1db0d75f31d6a2b30 to your computer and use it in GitHub Desktop.
Save KushtrimPacaj/ac5b59b5322dc5a1db0d75f31d6a2b30 to your computer and use it in GitHub Desktop.
Add Zoom to WebRTC Camera API
diff --git a/sdk/android/api/org/webrtc/Camera1Capturer.java b/sdk/android/api/org/webrtc/Camera1Capturer.java
index f178a3d5d3..992d4036cf 100644
--- a/sdk/android/api/org/webrtc/Camera1Capturer.java
+++ b/sdk/android/api/org/webrtc/Camera1Capturer.java
@@ -11,7 +11,8 @@
package org.webrtc;
import android.content.Context;
-import javax.annotation.Nullable;
+
+import java.util.List;
public class Camera1Capturer extends CameraCapturer {
private final boolean captureToTexture;
@@ -32,4 +33,102 @@ public class Camera1Capturer extends CameraCapturer {
surfaceTextureHelper, Camera1Enumerator.getCameraIndex(cameraName), width, height,
framerate);
}
+
+ /**
+ * Returns true if zoom is supported. Applications should call this
+ * before using other zoom methods.
+ *
+ * @return true if zoom is supported.
+ */
+ public boolean isZoomSupported() throws CameraException {
+ synchronized (stateLock) {
+ try {
+ return ((Camera1Session) currentSession).isZoomSupported();
+ } catch (Exception e) {
+ throw new CameraException(e);
+ }
+ }
+ }
+
+ /**
+ * Sets current zoom value.
+ *
+ * @param value zoom value. The valid range is 0 to {@link #getMaxZoom}.
+ */
+ public void setZoom(int value) throws CameraException {
+ synchronized (stateLock) {
+ try {
+ ((Camera1Session) currentSession).setZoom(value);
+ } catch (Exception e) {
+ throw new CameraException(e);
+ }
+ }
+ }
+
+ /**
+ * Gets the maximum zoom value allowed for snapshot. This is the maximum
+ * value that applications can set to {@link #setZoom(int)}.
+ * Applications should call {@link #isZoomSupported} before using this
+ * method. This value may change in different preview size. Applications
+ * should call this again after setting preview size.
+ *
+ * @return the maximum zoom value supported by the camera.
+ */
+ public int getZoom() throws CameraException {
+ synchronized (stateLock) {
+ try {
+ return ((Camera1Session) currentSession).getZoom();
+ } catch (Exception e) {
+ throw new CameraException(e);
+ }
+ }
+ }
+
+
+ /**
+ * Gets the maximum zoom value allowed for snapshot. This is the maximum
+ * value that applications can set to {@link #setZoom(int)}.
+ * Applications should call {@link #isZoomSupported} before using this
+ * method. This value may change in different preview size. Applications
+ * should call this again after setting preview size.
+ *
+ * @return the maximum zoom value supported by the camera.
+ */
+ public int getMaxZoom() throws CameraException {
+ synchronized (stateLock) {
+ try {
+ return ((Camera1Session) currentSession).getMaxZoom();
+ } catch (Exception e) {
+ throw new CameraException(e);
+ }
+ }
+ }
+
+ /**
+ * Gets the zoom ratios of all zoom values. Applications should check
+ * {@link #isZoomSupported} before using this method.
+ *
+ * @return the zoom ratios in 1/100 increments. Ex: a zoom of 3.2x is
+ * returned as 320. The number of elements is {@link
+ * #getMaxZoom} + 1. The list is sorted from small to large. The
+ * first element is always 100. The last element is the zoom
+ * ratio of the maximum zoom value.
+ */
+ public List<Integer> getZoomRatios() throws CameraException {
+ synchronized (stateLock) {
+ try {
+ return ((Camera1Session) currentSession).getZoomRatios();
+ } catch (Exception e) {
+ throw new CameraException(e);
+ }
+ }
+ }
+
+ public class CameraException extends Exception{
+
+ CameraException(Exception e){
+ super(e.getMessage(),e.getCause());
+ }
+ }
+
}
diff --git a/sdk/android/src/java/org/webrtc/Camera1Session.java b/sdk/android/src/java/org/webrtc/Camera1Session.java
index ad2b9bfc7e..8c046ec5ef 100644
--- a/sdk/android/src/java/org/webrtc/Camera1Session.java
+++ b/sdk/android/src/java/org/webrtc/Camera1Session.java
@@ -11,16 +11,15 @@
package org.webrtc;
import android.content.Context;
+import android.hardware.Camera;
import android.os.Handler;
import android.os.SystemClock;
-import android.view.Surface;
+import org.webrtc.CameraEnumerationAndroid.CaptureFormat;
+
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.List;
import java.util.concurrent.TimeUnit;
-import javax.annotation.Nullable;
-import org.webrtc.CameraEnumerationAndroid.CaptureFormat;
-import android.graphics.Matrix;
@SuppressWarnings("deprecation")
class Camera1Session implements CameraSession {
@@ -229,6 +228,29 @@ class Camera1Session implements CameraSession {
}
}
+ public boolean isZoomSupported() {
+ return camera.getParameters().isZoomSupported();
+ }
+
+ void setZoom(int value){
+ Camera.Parameters parameters = camera.getParameters();
+ parameters.setZoom(value);
+ camera.setParameters(parameters);
+ }
+
+ int getZoom(){
+ return camera.getParameters().getZoom();
+ }
+
+ int getMaxZoom(){
+ return camera.getParameters().getMaxZoom();
+ }
+
+ List<Integer> getZoomRatios(){
+ return camera.getParameters().getZoomRatios();
+ }
+
+
private void stopInternal() {
Logging.d(TAG, "Stop internal");
checkIsOnCameraThread();
diff --git a/sdk/android/src/java/org/webrtc/CameraCapturer.java b/sdk/android/src/java/org/webrtc/CameraCapturer.java
index 6addd3664d..6de9e26758 100644
--- a/sdk/android/src/java/org/webrtc/CameraCapturer.java
+++ b/sdk/android/src/java/org/webrtc/CameraCapturer.java
@@ -11,9 +11,11 @@
package org.webrtc;
import android.content.Context;
+import android.hardware.Camera;
import android.os.Handler;
import android.os.Looper;
import java.util.Arrays;
+import java.util.List;
import javax.annotation.Nullable;
@SuppressWarnings("deprecation")
@@ -180,9 +182,9 @@ abstract class CameraCapturer implements CameraVideoCapturer {
private org.webrtc.CapturerObserver capturerObserver;
@Nullable private SurfaceTextureHelper surfaceHelper;
- private final Object stateLock = new Object();
+ protected final Object stateLock = new Object();
private boolean sessionOpening; /* guarded by stateLock */
- @Nullable private CameraSession currentSession; /* guarded by stateLock */
+ @Nullable protected CameraSession currentSession; /* guarded by stateLock */
private String cameraName; /* guarded by stateLock */
private int width; /* guarded by stateLock */
private int height; /* guarded by stateLock */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment