From c1e3cced4bc8656fda0784b68de0f893b6884661 Mon Sep 17 00:00:00 2001 From: Mattia Iavarone Date: Sun, 4 Mar 2018 19:58:08 +0100 Subject: [PATCH] Refactor Previews --- .../cameraview/MockCameraPreview.java | 5 -- .../cameraview/CameraPreview.java | 68 ++++--------------- .../cameraview/SurfaceCameraPreview.java | 11 --- .../cameraview/TextureCameraPreview.java | 47 ++++++++++--- 4 files changed, 52 insertions(+), 79 deletions(-) diff --git a/cameraview/src/androidTest/java/com/otaliastudios/cameraview/MockCameraPreview.java b/cameraview/src/androidTest/java/com/otaliastudios/cameraview/MockCameraPreview.java index f3f8a4d7..27612161 100644 --- a/cameraview/src/androidTest/java/com/otaliastudios/cameraview/MockCameraPreview.java +++ b/cameraview/src/androidTest/java/com/otaliastudios/cameraview/MockCameraPreview.java @@ -30,11 +30,6 @@ public class MockCameraPreview extends CameraPreview { return new View(context); } - @Override - Surface getSurface() { - return null; - } - @Override Class getOutputClass() { return null; diff --git a/cameraview/src/main/views/com/otaliastudios/cameraview/CameraPreview.java b/cameraview/src/main/views/com/otaliastudios/cameraview/CameraPreview.java index cefd105d..b7210575 100644 --- a/cameraview/src/main/views/com/otaliastudios/cameraview/CameraPreview.java +++ b/cameraview/src/main/views/com/otaliastudios/cameraview/CameraPreview.java @@ -8,12 +8,12 @@ import android.view.ViewGroup; abstract class CameraPreview { - private final static CameraLogger LOG = CameraLogger.create(CameraPreview.class.getSimpleName()); + protected final static CameraLogger LOG = CameraLogger.create(CameraPreview.class.getSimpleName()); // Used for testing. Task mCropTask = new Task<>(); - // This is used to notify CameraImpl to recompute its camera Preview size. + // This is used to notify CameraController to recompute its camera Preview size. // After that, CameraView will need a new layout pass to adapt to the Preview size. interface SurfaceCallback { void onSurfaceAvailable(); @@ -22,16 +22,15 @@ abstract class CameraPreview { private SurfaceCallback mSurfaceCallback; private T mView; - private boolean mCropping; + protected boolean mCropping; - // As far as I can see, these are the view/surface dimensions. - // This live in the 'View' orientation. - private int mSurfaceWidth; - private int mSurfaceHeight; + // These are the surface dimensions in REF_VIEW. + protected int mSurfaceWidth; + protected int mSurfaceHeight; - // As far as I can see, these are the actual preview dimensions, as set in CameraParameters. - private int mDesiredWidth; - private int mDesiredHeight; + // These are the preview stream dimensions, in REF_VIEW. + protected int mDesiredWidth; + protected int mDesiredHeight; CameraPreview(Context context, ViewGroup parent, SurfaceCallback callback) { mView = onCreateView(context, parent); @@ -41,8 +40,6 @@ abstract class CameraPreview { @NonNull protected abstract T onCreateView(Context context, ViewGroup parent); - abstract Surface getSurface(); - @NonNull final T getView() { return mView; @@ -57,8 +54,8 @@ abstract class CameraPreview { // These must be alredy rotated, if needed, to be consistent with surface/view sizes. void setDesiredSize(int width, int height) { LOG.i("setDesiredSize:", "desiredW=", width, "desiredH=", height); - this.mDesiredWidth = width; - this.mDesiredHeight = height; + mDesiredWidth = width; + mDesiredHeight = height; crop(); } @@ -117,49 +114,14 @@ abstract class CameraPreview { * There might still be some absolute difference (e.g. same ratio but bigger / smaller). * However that should be already managed by the framework. */ - private final void crop() { + protected void crop() { + // The base implementation does not support cropping. mCropTask.start(); - - if (!supportsCropping()) { - mCropTask.end(null); - return; - }; - - getView().post(new Runnable() { - @Override - public void run() { - if (mDesiredHeight == 0 || mDesiredWidth == 0 || - mSurfaceHeight == 0 || mSurfaceWidth == 0) { - mCropTask.end(null); - return; - } - - float scaleX = 1f, scaleY = 1f; - AspectRatio current = AspectRatio.of(mSurfaceWidth, mSurfaceHeight); - AspectRatio target = AspectRatio.of(mDesiredWidth, mDesiredHeight); - if (current.toFloat() >= target.toFloat()) { - // We are too short. Must increase height. - scaleY = current.toFloat() / target.toFloat(); - } else { - // We must increase width. - scaleX = target.toFloat() / current.toFloat(); - } - applyCrop(scaleX, scaleY); - mCropping = scaleX > 1.02f || scaleY > 1.02f; - LOG.i("crop:", "applied scaleX=", scaleX); - LOG.i("crop:", "applied scaleY=", scaleY); - mCropTask.end(null); - } - }); - } - - protected void applyCrop(float scaleX, float scaleY) { - getView().setScaleX(scaleX); - getView().setScaleY(scaleY); + mCropTask.end(null); } boolean supportsCropping() { - return true; + return false; } /** diff --git a/cameraview/src/main/views/com/otaliastudios/cameraview/SurfaceCameraPreview.java b/cameraview/src/main/views/com/otaliastudios/cameraview/SurfaceCameraPreview.java index 5a0d5ea3..697c595f 100644 --- a/cameraview/src/main/views/com/otaliastudios/cameraview/SurfaceCameraPreview.java +++ b/cameraview/src/main/views/com/otaliastudios/cameraview/SurfaceCameraPreview.java @@ -63,11 +63,6 @@ class SurfaceCameraPreview extends CameraPreview { return root.findViewById(R.id.surface_view_root); } - @Override - Surface getSurface() { - return getOutput().getSurface(); - } - @Override SurfaceHolder getOutput() { return mSurfaceView.getHolder(); @@ -78,12 +73,6 @@ class SurfaceCameraPreview extends CameraPreview { return SurfaceHolder.class; } - @Override - boolean supportsCropping() { - return false; - } - - @Override protected void applyCrop(float scaleX, float scaleY) { /* float currWidth = getView().getWidth(); float currHeight = getView().getHeight(); diff --git a/cameraview/src/main/views/com/otaliastudios/cameraview/TextureCameraPreview.java b/cameraview/src/main/views/com/otaliastudios/cameraview/TextureCameraPreview.java index 60652886..10be709f 100644 --- a/cameraview/src/main/views/com/otaliastudios/cameraview/TextureCameraPreview.java +++ b/cameraview/src/main/views/com/otaliastudios/cameraview/TextureCameraPreview.java @@ -12,8 +12,6 @@ import android.view.ViewGroup; class TextureCameraPreview extends CameraPreview { - private Surface mSurface; - TextureCameraPreview(Context context, ViewGroup parent, SurfaceCallback callback) { super(context, parent, callback); } @@ -49,14 +47,6 @@ class TextureCameraPreview extends CameraPreview { return texture; } - @Override - Surface getSurface() { - if (mSurface == null) { // Check if valid? - mSurface = new Surface(getOutput()); - } - return mSurface; - } - @Override Class getOutputClass() { return SurfaceTexture.class; @@ -76,4 +66,41 @@ class TextureCameraPreview extends CameraPreview { } } + @Override + boolean supportsCropping() { + return true; + } + + @Override + protected void crop() { + mCropTask.start(); + getView().post(new Runnable() { + @Override + public void run() { + if (mDesiredHeight == 0 || mDesiredWidth == 0 || + mSurfaceHeight == 0 || mSurfaceWidth == 0) { + mCropTask.end(null); + return; + } + float scaleX = 1f, scaleY = 1f; + AspectRatio current = AspectRatio.of(mSurfaceWidth, mSurfaceHeight); + AspectRatio target = AspectRatio.of(mDesiredWidth, mDesiredHeight); + if (current.toFloat() >= target.toFloat()) { + // We are too short. Must increase height. + scaleY = current.toFloat() / target.toFloat(); + } else { + // We must increase width. + scaleX = target.toFloat() / current.toFloat(); + } + + getView().setScaleX(scaleX); + getView().setScaleY(scaleY); + + mCropping = scaleX > 1.02f || scaleY > 1.02f; + LOG.i("crop:", "applied scaleX=", scaleX); + LOG.i("crop:", "applied scaleY=", scaleY); + mCropTask.end(null); + } + }); + } }