From 3c345826ccde0ea9942fbd76a69be1cba71468c5 Mon Sep 17 00:00:00 2001 From: Mattia Iavarone Date: Mon, 24 Jun 2019 18:03:42 -0500 Subject: [PATCH] Create base Camera2 class --- .../cameraview/CameraOptions1Test.java | 3 +- .../cameraview/engine/Mapper1Test.java | 3 +- .../cameraview/CameraOptions.java | 2 +- .../otaliastudios/cameraview/CameraUtils.java | 3 +- .../otaliastudios/cameraview/CameraView.java | 5 +- .../cameraview/engine/Camera1Engine.java | 3 +- .../cameraview/engine/Camera2Engine.java | 151 ++++++++++++++++++ .../cameraview/engine/Mapper.java | 70 ++++++-- 8 files changed, 224 insertions(+), 16 deletions(-) create mode 100644 cameraview/src/main/java/com/otaliastudios/cameraview/engine/Camera2Engine.java diff --git a/cameraview/src/androidTest/java/com/otaliastudios/cameraview/CameraOptions1Test.java b/cameraview/src/androidTest/java/com/otaliastudios/cameraview/CameraOptions1Test.java index 9d13e999..8787b889 100644 --- a/cameraview/src/androidTest/java/com/otaliastudios/cameraview/CameraOptions1Test.java +++ b/cameraview/src/androidTest/java/com/otaliastudios/cameraview/CameraOptions1Test.java @@ -4,6 +4,7 @@ package com.otaliastudios.cameraview; import android.hardware.Camera; import com.otaliastudios.cameraview.controls.Audio; +import com.otaliastudios.cameraview.controls.Engine; import com.otaliastudios.cameraview.controls.Facing; import com.otaliastudios.cameraview.controls.Flash; import com.otaliastudios.cameraview.engine.Mapper; @@ -249,7 +250,7 @@ public class CameraOptions1Test extends BaseTest { } CameraOptions o = new CameraOptions(mock(Camera.Parameters.class), false); - Mapper m = Mapper.get(); + Mapper m = Mapper.get(Engine.CAMERA1); Collection s = o.getSupportedControls(Facing.class); assertEquals(s.size(), supported.size()); for (Facing facing : s) { diff --git a/cameraview/src/androidTest/java/com/otaliastudios/cameraview/engine/Mapper1Test.java b/cameraview/src/androidTest/java/com/otaliastudios/cameraview/engine/Mapper1Test.java index 86dbbcb6..e2cebd8b 100644 --- a/cameraview/src/androidTest/java/com/otaliastudios/cameraview/engine/Mapper1Test.java +++ b/cameraview/src/androidTest/java/com/otaliastudios/cameraview/engine/Mapper1Test.java @@ -4,6 +4,7 @@ package com.otaliastudios.cameraview.engine; import android.hardware.Camera; import com.otaliastudios.cameraview.BaseTest; +import com.otaliastudios.cameraview.controls.Engine; import com.otaliastudios.cameraview.controls.Facing; import com.otaliastudios.cameraview.controls.Flash; import com.otaliastudios.cameraview.controls.Hdr; @@ -22,7 +23,7 @@ import static org.junit.Assert.*; @SmallTest public class Mapper1Test extends BaseTest { - private Mapper mapper = Mapper.get(); + private Mapper mapper = Mapper.get(Engine.CAMERA1); @Test public void testMap() { diff --git a/cameraview/src/main/java/com/otaliastudios/cameraview/CameraOptions.java b/cameraview/src/main/java/com/otaliastudios/cameraview/CameraOptions.java index d71b8a0c..dc46ed9b 100644 --- a/cameraview/src/main/java/com/otaliastudios/cameraview/CameraOptions.java +++ b/cameraview/src/main/java/com/otaliastudios/cameraview/CameraOptions.java @@ -52,7 +52,7 @@ public class CameraOptions { @SuppressWarnings("deprecation") public CameraOptions(@NonNull Camera.Parameters params, boolean flipSizes) { List strings; - Mapper mapper = Mapper.get(); + Mapper mapper = Mapper.get(Engine.CAMERA1); // Facing Camera.CameraInfo cameraInfo = new Camera.CameraInfo(); diff --git a/cameraview/src/main/java/com/otaliastudios/cameraview/CameraUtils.java b/cameraview/src/main/java/com/otaliastudios/cameraview/CameraUtils.java index 80c39c72..27f682d8 100644 --- a/cameraview/src/main/java/com/otaliastudios/cameraview/CameraUtils.java +++ b/cameraview/src/main/java/com/otaliastudios/cameraview/CameraUtils.java @@ -9,6 +9,7 @@ import android.graphics.Matrix; import android.hardware.Camera; import android.os.Handler; +import com.otaliastudios.cameraview.controls.Engine; import com.otaliastudios.cameraview.controls.Facing; import com.otaliastudios.cameraview.engine.Mapper; import com.otaliastudios.cameraview.internal.utils.ExifHelper; @@ -60,7 +61,7 @@ public class CameraUtils { */ public static boolean hasCameraFacing(@SuppressWarnings("unused") @NonNull Context context, @NonNull Facing facing) { - int internal = Mapper.get().map(facing); + int internal = Mapper.get(Engine.CAMERA1).map(facing); Camera.CameraInfo cameraInfo = new Camera.CameraInfo(); for (int i = 0, count = Camera.getNumberOfCameras(); i < count; i++) { Camera.getCameraInfo(i, cameraInfo); diff --git a/cameraview/src/main/java/com/otaliastudios/cameraview/CameraView.java b/cameraview/src/main/java/com/otaliastudios/cameraview/CameraView.java index 318f01a4..ff6a09c9 100644 --- a/cameraview/src/main/java/com/otaliastudios/cameraview/CameraView.java +++ b/cameraview/src/main/java/com/otaliastudios/cameraview/CameraView.java @@ -36,6 +36,7 @@ import com.otaliastudios.cameraview.controls.ControlParser; import com.otaliastudios.cameraview.controls.Engine; import com.otaliastudios.cameraview.controls.Facing; import com.otaliastudios.cameraview.controls.Flash; +import com.otaliastudios.cameraview.engine.Camera2Engine; import com.otaliastudios.cameraview.markers.MarkerLayout; import com.otaliastudios.cameraview.engine.Camera1Engine; import com.otaliastudios.cameraview.engine.CameraEngine; @@ -233,8 +234,8 @@ public class CameraView extends FrameLayout implements LifecycleObserver { */ @NonNull protected CameraEngine instantiateCameraEngine(@NonNull Engine engine, @NonNull CameraEngine.Callback callback) { - if (mExperimental && engine == Engine.CAMERA2) { - throw new RuntimeException("TODO"); + if (mExperimental && engine == Engine.CAMERA2 && Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { + return new Camera2Engine(callback); } else { mEngine = Engine.CAMERA1; return new Camera1Engine(callback); 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 c9619453..2a9602d3 100644 --- a/cameraview/src/main/java/com/otaliastudios/cameraview/engine/Camera1Engine.java +++ b/cameraview/src/main/java/com/otaliastudios/cameraview/engine/Camera1Engine.java @@ -17,6 +17,7 @@ import android.view.SurfaceHolder; import com.otaliastudios.cameraview.CameraException; import com.otaliastudios.cameraview.CameraLogger; import com.otaliastudios.cameraview.CameraOptions; +import com.otaliastudios.cameraview.controls.Engine; import com.otaliastudios.cameraview.frame.Frame; import com.otaliastudios.cameraview.PictureResult; import com.otaliastudios.cameraview.VideoResult; @@ -75,7 +76,7 @@ public class Camera1Engine extends CameraEngine implements Camera.PreviewCallbac public Camera1Engine(@NonNull Callback callback) { super(callback); - mMapper = Mapper.get(); + mMapper = Mapper.get(Engine.CAMERA1); } private void schedule(@Nullable final Task task, final boolean ensureAvailable, final Runnable action) { diff --git a/cameraview/src/main/java/com/otaliastudios/cameraview/engine/Camera2Engine.java b/cameraview/src/main/java/com/otaliastudios/cameraview/engine/Camera2Engine.java new file mode 100644 index 00000000..820d8c1b --- /dev/null +++ b/cameraview/src/main/java/com/otaliastudios/cameraview/engine/Camera2Engine.java @@ -0,0 +1,151 @@ +package com.otaliastudios.cameraview.engine; + +import android.graphics.PointF; +import android.location.Location; +import android.os.Build; +import android.view.SurfaceHolder; + +import com.otaliastudios.cameraview.CameraException; +import com.otaliastudios.cameraview.CameraLogger; +import com.otaliastudios.cameraview.CameraOptions; +import com.otaliastudios.cameraview.PictureResult; +import com.otaliastudios.cameraview.VideoResult; +import com.otaliastudios.cameraview.controls.Audio; +import com.otaliastudios.cameraview.controls.Engine; +import com.otaliastudios.cameraview.controls.Facing; +import com.otaliastudios.cameraview.controls.Flash; +import com.otaliastudios.cameraview.controls.Hdr; +import com.otaliastudios.cameraview.controls.Mode; +import com.otaliastudios.cameraview.controls.WhiteBalance; +import com.otaliastudios.cameraview.gesture.Gesture; +import com.otaliastudios.cameraview.size.AspectRatio; + +import java.io.File; + +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.annotation.RequiresApi; + + +@RequiresApi(Build.VERSION_CODES.LOLLIPOP) +public class Camera2Engine extends CameraEngine { + + private static final String TAG = Camera2Engine.class.getSimpleName(); + private static final CameraLogger LOG = CameraLogger.create(TAG); + + public Camera2Engine(Callback callback) { + super(callback); + mMapper = Mapper.get(Engine.CAMERA2); + } + + @Override + public void onSurfaceAvailable() { + + } + + @Override + public void onSurfaceChanged() { + + } + + @Override + public void onSurfaceDestroyed() { + + } + + @Override + protected void onStart() { + + } + + @Override + protected void onStop() { + + } + + @Override + public void onBufferAvailable(@NonNull byte[] buffer) { + + } + + @Override + public void setMode(@NonNull Mode mode) { + + } + + @Override + public void setFacing(@NonNull Facing facing) { + + } + + @Override + public void setZoom(float zoom, @Nullable PointF[] points, boolean notify) { + + } + + @Override + public void setExposureCorrection(float EVvalue, @NonNull float[] bounds, @Nullable PointF[] points, boolean notify) { + + } + + @Override + public void setFlash(@NonNull Flash flash) { + + } + + @Override + public void setWhiteBalance(@NonNull WhiteBalance whiteBalance) { + + } + + @Override + public void setHdr(@NonNull Hdr hdr) { + + } + + @Override + public void setLocation(@Nullable Location location) { + + } + + @Override + public void setAudio(@NonNull Audio audio) { + + } + + @Override + public void takePicture(@NonNull PictureResult.Stub stub) { + + } + + @Override + public void takePictureSnapshot(@NonNull PictureResult.Stub stub, @NonNull AspectRatio viewAspectRatio) { + + } + + @Override + public void takeVideo(@NonNull VideoResult.Stub stub, @NonNull File file) { + + } + + @Override + public void takeVideoSnapshot(@NonNull VideoResult.Stub stub, @NonNull File file, @NonNull AspectRatio viewAspectRatio) { + + } + + @Override + public void stopVideo() { + + } + + @Override + public void startAutoFocus(@Nullable Gesture gesture, @NonNull PointF point) { + + } + + @Override + public void setPlaySounds(boolean playSounds) { + + } +} + diff --git a/cameraview/src/main/java/com/otaliastudios/cameraview/engine/Mapper.java b/cameraview/src/main/java/com/otaliastudios/cameraview/engine/Mapper.java index 9499e36e..55461bad 100644 --- a/cameraview/src/main/java/com/otaliastudios/cameraview/engine/Mapper.java +++ b/cameraview/src/main/java/com/otaliastudios/cameraview/engine/Mapper.java @@ -3,6 +3,7 @@ package com.otaliastudios.cameraview.engine; import android.hardware.Camera; import android.os.Build; +import com.otaliastudios.cameraview.controls.Engine; import com.otaliastudios.cameraview.controls.Facing; import com.otaliastudios.cameraview.controls.Flash; import com.otaliastudios.cameraview.controls.Hdr; @@ -10,11 +11,28 @@ import com.otaliastudios.cameraview.controls.WhiteBalance; import java.util.HashMap; +import androidx.annotation.NonNull; + /** * A Mapper maps camera engine constants to CameraView constants. */ public abstract class Mapper { + private static Mapper CAMERA1; + private static Mapper CAMERA2; + + public static Mapper get(@NonNull Engine engine) { + if (engine == Engine.CAMERA1) { + if (CAMERA1 == null) CAMERA1 = new Camera1Mapper(); + return CAMERA1; + } else if (engine == Engine.CAMERA2) { + if (CAMERA2 == null) CAMERA2 = new Camera2Mapper(); + return CAMERA2; + } else { + throw new IllegalArgumentException("Unknown engine."); + } + } + private Mapper() {} public abstract T map(Flash flash); @@ -43,15 +61,6 @@ public abstract class Mapper { return null; } - private static Mapper CAMERA1; - - public static Mapper get() { - if (CAMERA1 == null) { - CAMERA1 = new Camera1Mapper(); - } - return CAMERA1; - } - @SuppressWarnings("unchecked") private static class Camera1Mapper extends Mapper { @@ -120,4 +129,47 @@ public abstract class Mapper { return reverseLookup(HDR, cameraConstant); } } + + private static class Camera2Mapper extends Mapper { + + @Override + public T map(Flash flash) { + return null; + } + + @Override + public T map(Facing facing) { + return null; + } + + @Override + public T map(WhiteBalance whiteBalance) { + return null; + } + + @Override + public T map(Hdr hdr) { + return null; + } + + @Override + public Flash unmapFlash(T cameraConstant) { + return null; + } + + @Override + public Facing unmapFacing(T cameraConstant) { + return null; + } + + @Override + public WhiteBalance unmapWhiteBalance(T cameraConstant) { + return null; + } + + @Override + public Hdr unmapHdr(T cameraConstant) { + return null; + } + } }