diff --git a/camerakit/src/main/api16/com/flurgle/camerakit/Camera1.java b/camerakit/src/main/api16/com/flurgle/camerakit/Camera1.java index ca9e1f48..84b0cc5b 100644 --- a/camerakit/src/main/api16/com/flurgle/camerakit/Camera1.java +++ b/camerakit/src/main/api16/com/flurgle/camerakit/Camera1.java @@ -6,6 +6,7 @@ import android.hardware.Camera; import android.media.CamcorderProfile; import android.media.MediaRecorder; import android.os.Handler; +import android.support.annotation.Nullable; import android.view.MotionEvent; import android.view.SurfaceHolder; import android.view.View; @@ -36,6 +37,7 @@ public class Camera1 extends CameraImpl { private int mCameraId; private Camera mCamera; private Camera.Parameters mCameraParameters; + private CameraProperties mCameraProperties; private Camera.CameraInfo mCameraInfo; private Size mPreviewSize; private Size mCaptureSize; @@ -309,6 +311,12 @@ public class Camera1 extends CameraImpl { return mCamera != null; } + @Nullable + @Override + CameraProperties getCameraProperties() { + return mCameraProperties; + } + // Internal: private void openCamera() { @@ -319,6 +327,7 @@ public class Camera1 extends CameraImpl { mCamera = Camera.open(mCameraId); mCameraParameters = mCamera.getParameters(); + collectCameraProperties(); adjustCameraParameters(); mCamera.setDisplayOrientation(calculatePreviewRotation()); @@ -391,6 +400,11 @@ public class Camera1 extends CameraImpl { mCamera.setParameters(mCameraParameters); } + private void collectCameraProperties() { + mCameraProperties = new CameraProperties(mCameraParameters.getVerticalViewAngle(), + mCameraParameters.getHorizontalViewAngle()); + } + private TreeSet findCommonAspectRatios(List previewSizes, List captureSizes) { Set previewAspectRatios = new HashSet<>(); for (Camera.Size size : previewSizes) { diff --git a/camerakit/src/main/api21/com/flurgle/camerakit/Camera2.java b/camerakit/src/main/api21/com/flurgle/camerakit/Camera2.java index 6936a411..828c01bb 100644 --- a/camerakit/src/main/api21/com/flurgle/camerakit/Camera2.java +++ b/camerakit/src/main/api21/com/flurgle/camerakit/Camera2.java @@ -3,14 +3,18 @@ package com.flurgle.camerakit; import android.annotation.TargetApi; import android.content.Context; import android.graphics.ImageFormat; +import android.graphics.PointF; import android.hardware.camera2.CameraAccessException; import android.hardware.camera2.CameraCharacteristics; import android.hardware.camera2.CameraDevice; import android.hardware.camera2.CameraManager; import android.hardware.camera2.params.StreamConfigurationMap; +import android.support.annotation.Nullable; import android.util.Log; +import android.util.SizeF; import java.util.ArrayList; +import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.TreeSet; @@ -28,6 +32,8 @@ class Camera2 extends CameraImpl { private Size mCaptureSize; private Size mPreviewSize; + private final HashMap mCameraPropertyMap = new HashMap<>(); + Camera2(CameraListener callback, PreviewImpl preview, Context context) { super(callback, preview); preview.setCallback(new PreviewImpl.Callback() { @@ -38,6 +44,36 @@ class Camera2 extends CameraImpl { }); mCameraManager = (CameraManager) context.getSystemService(Context.CAMERA_SERVICE); + + // Get all view angles + try { + for (final String cameraId : mCameraManager.getCameraIdList()) { + CameraCharacteristics characteristics = + mCameraManager.getCameraCharacteristics(cameraId); + @SuppressWarnings("ConstantConditions") + int orientation = characteristics.get(CameraCharacteristics.LENS_FACING); + if (orientation == CameraCharacteristics.LENS_FACING_BACK) { + float[] maxFocus = characteristics.get( + CameraCharacteristics.LENS_INFO_AVAILABLE_FOCAL_LENGTHS); + if (maxFocus == null) { + continue; + } + SizeF size = characteristics.get( + CameraCharacteristics.SENSOR_INFO_PHYSICAL_SIZE); + if (size == null) { + continue; + } + float w = size.getWidth(); + float h = size.getHeight(); + mCameraPropertyMap.put(cameraId, new CameraProperties( + (float) Math.toDegrees(2*Math.atan(size.getWidth()/(maxFocus[0]*2))), + (float) Math.toDegrees(2*Math.atan(size.getHeight()/(maxFocus[0]*2))) + )); + } + } + } catch (CameraAccessException e) { + throw new RuntimeException("Failed to get camera view angles", e); + } } // CameraImpl: @@ -196,6 +232,14 @@ class Camera2 extends CameraImpl { return mCamera != null; } + @Nullable + @Override + CameraProperties getCameraProperties() { + if (mCamera == null) { + return null; + } + return mCameraPropertyMap.get(mCamera.getId()); + } // Internal private List getAvailableCaptureResolutions() { diff --git a/camerakit/src/main/base/com/flurgle/camerakit/CameraImpl.java b/camerakit/src/main/base/com/flurgle/camerakit/CameraImpl.java index 90b5ed8b..5a5aecc8 100644 --- a/camerakit/src/main/base/com/flurgle/camerakit/CameraImpl.java +++ b/camerakit/src/main/base/com/flurgle/camerakit/CameraImpl.java @@ -1,5 +1,7 @@ package com.flurgle.camerakit; +import android.support.annotation.Nullable; + abstract class CameraImpl { protected final CameraListener mCameraListener; @@ -30,4 +32,7 @@ abstract class CameraImpl { abstract Size getPreviewResolution(); abstract boolean isCameraOpened(); + @Nullable + abstract CameraProperties getCameraProperties(); + } diff --git a/camerakit/src/main/base/com/flurgle/camerakit/CameraProperties.java b/camerakit/src/main/base/com/flurgle/camerakit/CameraProperties.java new file mode 100644 index 00000000..6af4d501 --- /dev/null +++ b/camerakit/src/main/base/com/flurgle/camerakit/CameraProperties.java @@ -0,0 +1,14 @@ +package com.flurgle.camerakit; + +/** + * Simple pojo containing various camera properties. + */ +public class CameraProperties { + public final float verticalViewingAngle; + public final float horizontalViewingAngle; + + public CameraProperties(float verticalViewingAngle, float horizontalViewingAngle) { + this.verticalViewingAngle = verticalViewingAngle; + this.horizontalViewingAngle = horizontalViewingAngle; + } +} diff --git a/camerakit/src/main/java/com/flurgle/camerakit/CameraView.java b/camerakit/src/main/java/com/flurgle/camerakit/CameraView.java index cc3b530e..a20adc03 100644 --- a/camerakit/src/main/java/com/flurgle/camerakit/CameraView.java +++ b/camerakit/src/main/java/com/flurgle/camerakit/CameraView.java @@ -258,6 +258,11 @@ public class CameraView extends FrameLayout { mCameraImpl.stop(); } + @Nullable + public CameraProperties getCameraProperties() { + return mCameraImpl.getCameraProperties(); + } + @Facing public int getFacing() { return mFacing;