From 3b28133623280c7ac17aa0c15737e669378c85b4 Mon Sep 17 00:00:00 2001 From: "alexander.melnikov" Date: Thu, 26 Mar 2020 11:25:59 +0300 Subject: [PATCH] Added smarter logic for understand that need restart camera or preview when we switch mode --- .../otaliastudios/cameraview/CameraView.java | 12 +-------- .../cameraview/engine/Camera1Engine.java | 26 +++++++++++++++---- .../cameraview/engine/Camera2Engine.java | 2 +- .../cameraview/engine/CameraBaseEngine.java | 8 ++---- .../cameraview/engine/CameraEngine.java | 2 +- 5 files changed, 26 insertions(+), 24 deletions(-) diff --git a/cameraview/src/main/java/com/otaliastudios/cameraview/CameraView.java b/cameraview/src/main/java/com/otaliastudios/cameraview/CameraView.java index 4514624b..07c1d6b4 100644 --- a/cameraview/src/main/java/com/otaliastudios/cameraview/CameraView.java +++ b/cameraview/src/main/java/com/otaliastudios/cameraview/CameraView.java @@ -159,7 +159,6 @@ public class CameraView extends FrameLayout implements LifecycleObserver { @SuppressWarnings({"FieldCanBeLocal", "unused"}) private boolean mExperimental; private boolean mInEditor; - private boolean mChangeModeWithoutRestart; // Overlays @VisibleForTesting OverlayLayout mOverlayLayout; @@ -870,15 +869,6 @@ public class CameraView extends FrameLayout implements LifecycleObserver { mExperimental = experimental; } - /** - * Sets the flag if you want disable restart camera on change mode (Photo/Video), - * but only for {@link Engine#CAMERA1} - * @param changeModeWithoutRestart - true if you want disable restart - */ - public void setChangeModeWithoutRestart(boolean changeModeWithoutRestart) { - mChangeModeWithoutRestart = changeModeWithoutRestart; - } - /** * Shorthand for the appropriate set* method. * For example, if control is a {@link Grid}, this calls {@link #setGrid(Grid)}. @@ -1430,7 +1420,7 @@ public class CameraView extends FrameLayout implements LifecycleObserver { * @param mode desired session type. */ public void setMode(@NonNull Mode mode) { - mCameraEngine.setMode(mode, mChangeModeWithoutRestart); + mCameraEngine.setMode(mode); } /** diff --git a/cameraview/src/main/java/com/otaliastudios/cameraview/engine/Camera1Engine.java b/cameraview/src/main/java/com/otaliastudios/cameraview/engine/Camera1Engine.java index e61ec24b..90684e5a 100644 --- a/cameraview/src/main/java/com/otaliastudios/cameraview/engine/Camera1Engine.java +++ b/cameraview/src/main/java/com/otaliastudios/cameraview/engine/Camera1Engine.java @@ -148,12 +148,28 @@ public class Camera1Engine extends CameraBaseEngine implements @EngineThread @Override protected void prepareNewMode() { + if(previewAspectRatioEqualsForModes()) { + Camera.Parameters params = mCamera.getParameters(); + setPictureSize(params); + applyDefaultFocus(params); + params.setRecordingHint(getMode() == Mode.VIDEO); + mCamera.setParameters(params); + } else { + restartBind(); + } + } + + private boolean previewAspectRatioEqualsForModes() { + Size prevModeSize = mCaptureSize; mCaptureSize = computeCaptureSize(); - Camera.Parameters params = mCamera.getParameters(); - setPictureSize(params); - applyDefaultFocus(params); - params.setRecordingHint(getMode() == Mode.VIDEO); - mCamera.setParameters(params); + + boolean flip = getAngles().flip(Reference.SENSOR, Reference.VIEW); + AspectRatio prevModePreviewTargetRatio = AspectRatio.of(prevModeSize.getWidth(), prevModeSize.getHeight()); + AspectRatio targetRatio = AspectRatio.of(mCaptureSize.getWidth(), mCaptureSize.getHeight()); + if (flip) prevModePreviewTargetRatio = prevModePreviewTargetRatio.flip(); + if (flip) targetRatio = targetRatio.flip(); + + return prevModePreviewTargetRatio.equals(targetRatio); } //endregion diff --git a/cameraview/src/main/java/com/otaliastudios/cameraview/engine/Camera2Engine.java b/cameraview/src/main/java/com/otaliastudios/cameraview/engine/Camera2Engine.java index a00504f1..5eb7b665 100644 --- a/cameraview/src/main/java/com/otaliastudios/cameraview/engine/Camera2Engine.java +++ b/cameraview/src/main/java/com/otaliastudios/cameraview/engine/Camera2Engine.java @@ -393,7 +393,7 @@ public class Camera2Engine extends CameraBaseEngine implements @Override protected void prepareNewMode() { LOG.w("prepareNewMode:", "can't prepare new mode without restart for Engine2"); - restart(); + restartBind(); } //endregion diff --git a/cameraview/src/main/java/com/otaliastudios/cameraview/engine/CameraBaseEngine.java b/cameraview/src/main/java/com/otaliastudios/cameraview/engine/CameraBaseEngine.java index 1c1ac52d..2bf55f70 100644 --- a/cameraview/src/main/java/com/otaliastudios/cameraview/engine/CameraBaseEngine.java +++ b/cameraview/src/main/java/com/otaliastudios/cameraview/engine/CameraBaseEngine.java @@ -384,18 +384,14 @@ public abstract class CameraBaseEngine extends CameraEngine { * @param mode desired mode. */ @Override - public final void setMode(@NonNull Mode mode, final boolean withoutRestart) { + public final void setMode(@NonNull Mode mode) { if (mode != mMode) { mMode = mode; getOrchestrator().scheduleStateful("mode", CameraState.ENGINE, new Runnable() { @Override public void run() { - if (withoutRestart) { - prepareNewMode(); - } else { - restart(); - } + prepareNewMode(); } }); } diff --git a/cameraview/src/main/java/com/otaliastudios/cameraview/engine/CameraEngine.java b/cameraview/src/main/java/com/otaliastudios/cameraview/engine/CameraEngine.java index 967ef6cf..2817bbdb 100644 --- a/cameraview/src/main/java/com/otaliastudios/cameraview/engine/CameraEngine.java +++ b/cameraview/src/main/java/com/otaliastudios/cameraview/engine/CameraEngine.java @@ -664,7 +664,7 @@ public abstract class CameraEngine implements public abstract void setAudio(@NonNull Audio audio); @NonNull public abstract Audio getAudio(); - public abstract void setMode(@NonNull Mode mode, boolean withoutRestart); + public abstract void setMode(@NonNull Mode mode); @NonNull public abstract Mode getMode(); public abstract void setZoom(float zoom, @Nullable PointF[] points, boolean notify);