From 6614785f7b6570178f13a33ff7afd089dc372de3 Mon Sep 17 00:00:00 2001 From: Dionysis Date: Sat, 15 Jul 2017 22:54:35 +0200 Subject: [PATCH 01/11] Add scale type selectors --- .../io/fotoapparat/FotoapparatBuilder.java | 8 + .../io/fotoapparat/parameter/ScaleType.java | 20 + .../selector/ScaleTypeSelectors.java | 28 ++ .../fotoapparat/FotoapparatBuilderTest.java | 468 +++++++++--------- .../selector/ScaleTypeSelectorsTest.java | 57 +++ 5 files changed, 358 insertions(+), 223 deletions(-) create mode 100644 fotoapparat/src/main/java/io/fotoapparat/parameter/ScaleType.java create mode 100644 fotoapparat/src/main/java/io/fotoapparat/parameter/selector/ScaleTypeSelectors.java create mode 100644 fotoapparat/src/test/java/io/fotoapparat/parameter/selector/ScaleTypeSelectorsTest.java diff --git a/fotoapparat/src/main/java/io/fotoapparat/FotoapparatBuilder.java b/fotoapparat/src/main/java/io/fotoapparat/FotoapparatBuilder.java index d797b6cc..69aae9cc 100644 --- a/fotoapparat/src/main/java/io/fotoapparat/FotoapparatBuilder.java +++ b/fotoapparat/src/main/java/io/fotoapparat/FotoapparatBuilder.java @@ -82,6 +82,14 @@ public FotoapparatBuilder previewSize(SelectorFunction selector) { return this; } + /** + * @param selector selects size of preview stream (in pixels) from list of available sizes. + */ + public FotoapparatBuilder previewStyle(SelectorFunction selector) { + previewSizeSelector = selector; + return this; + } + /** * @param selector selects focus mode from list of available modes. */ diff --git a/fotoapparat/src/main/java/io/fotoapparat/parameter/ScaleType.java b/fotoapparat/src/main/java/io/fotoapparat/parameter/ScaleType.java new file mode 100644 index 00000000..461c2266 --- /dev/null +++ b/fotoapparat/src/main/java/io/fotoapparat/parameter/ScaleType.java @@ -0,0 +1,20 @@ +package io.fotoapparat.parameter; + +/** + * The style of the preview. + */ +public enum ScaleType { + + /** + * The preview will be scaled so as it's one dimensions will be equal and the other equal or + * larger than the corresponding dimension of the view + */ + CENTER_CROP, + + /** + * The preview will be scaled so as it's one dimensions will be equal and the other equal or + * smaller than the corresponding dimension of the view + */ + CENTER_INSIDE + +} diff --git a/fotoapparat/src/main/java/io/fotoapparat/parameter/selector/ScaleTypeSelectors.java b/fotoapparat/src/main/java/io/fotoapparat/parameter/selector/ScaleTypeSelectors.java new file mode 100644 index 00000000..c0f2023b --- /dev/null +++ b/fotoapparat/src/main/java/io/fotoapparat/parameter/selector/ScaleTypeSelectors.java @@ -0,0 +1,28 @@ +package io.fotoapparat.parameter.selector; + +import io.fotoapparat.parameter.ScaleType; + +/** + * Selector functions for {@link ScaleType}. + */ +public class ScaleTypeSelectors { + + /** + * @return {@link SelectorFunction} which provides a preview style which will be scaled so as + * it's one dimensions will be equal and the other equal or larger than the corresponding + * dimension of the view. + */ + public static SelectorFunction centerCropped() { + return Selectors.single(ScaleType.CENTER_CROP); + } + + /** + * @return {@link SelectorFunction} which provides a preview style which will be scaled so as + * it's one dimensions will be equal and the other equal or smaller than the corresponding + * dimension of the view. + */ + public static SelectorFunction centerInside() { + return Selectors.single(ScaleType.CENTER_INSIDE); + } + +} diff --git a/fotoapparat/src/test/java/io/fotoapparat/FotoapparatBuilderTest.java b/fotoapparat/src/test/java/io/fotoapparat/FotoapparatBuilderTest.java index d936c2bd..9f665dd0 100644 --- a/fotoapparat/src/test/java/io/fotoapparat/FotoapparatBuilderTest.java +++ b/fotoapparat/src/test/java/io/fotoapparat/FotoapparatBuilderTest.java @@ -28,227 +28,249 @@ @RunWith(MockitoJUnitRunner.class) public class FotoapparatBuilderTest { - @Mock - Context context; - @Mock - CameraProvider cameraProvider; - @Mock - CameraRenderer cameraRenderer; - - @Mock - SelectorFunction photoSizeSelector; - @Mock - SelectorFunction previewSizeSelector; - @Mock - SelectorFunction lensPositionSelector; - @Mock - SelectorFunction focusModeSelector; - @Mock - SelectorFunction flashSelector; - - @Mock - FrameProcessor frameProcessor; - - @Mock - Logger logger; - - @Before - public void setUp() throws Exception { - given(context.getSystemService(Context.WINDOW_SERVICE)) - .willReturn(Mockito.mock(WindowManager.class)); - } - - @Test - public void onlyMandatoryParameters() throws Exception { - // Given - FotoapparatBuilder builder = builderWithMandatoryArguments(); - - // When - Fotoapparat result = builder.build(); - - // Then - assertNotNull(result); - } - - @Test - public void cameraProvider_HasDefault() throws Exception { - // When - FotoapparatBuilder builder = builderWithMandatoryArguments(); - - // Then - assertNotNull(builder.cameraProvider); - } - - @Test - public void cameraProvider_IsConfigurable() throws Exception { - // When - FotoapparatBuilder builder = builderWithMandatoryArguments() - .cameraProvider(cameraProvider); - - // Then - assertEquals( - cameraProvider, - builder.cameraProvider - ); - } - - @Test - public void logger_HasDefault() throws Exception { - // When - FotoapparatBuilder builder = builderWithMandatoryArguments(); - - // Then - assertNotNull(builder.logger); - } - - @Test - public void logger_IsConfigurable() throws Exception { - // When - FotoapparatBuilder builder = builderWithMandatoryArguments() - .logger(logger); - - // Then - assertEquals( - logger, - builder.logger - ); - } - - @Test - public void focusMode_HasDefault() throws Exception { - // When - FotoapparatBuilder builder = builderWithMandatoryArguments(); - - // Then - assertNotNull(builder.focusModeSelector); - } - - @Test - public void focusMode_IsConfigurable() throws Exception { - // When - FotoapparatBuilder builder = builderWithMandatoryArguments() - .focusMode(focusModeSelector); - - // Then - assertEquals( - focusModeSelector, - builder.focusModeSelector - ); - } - - @Test - public void flashMode_IsConfigurable() throws Exception { - // When - FotoapparatBuilder builder = builderWithMandatoryArguments() - .flash(flashSelector); - - // Then - assertEquals( - flashSelector, - builder.flashSelector - ); - } - - @Test - public void frameProcessor_NullByDefault() throws Exception { - // When - FotoapparatBuilder builder = builderWithMandatoryArguments(); - - // Then - assertNull(builder.frameProcessor); - } - - @Test - public void frameProcessor_IsConfigurable() throws Exception { - // When - FotoapparatBuilder builder = builderWithMandatoryArguments() - .frameProcessor(frameProcessor); - - // Then - assertEquals( - frameProcessor, - builder.frameProcessor - ); - } - - @Test - public void photoSize_IsConfigurable() throws Exception { - // When - FotoapparatBuilder builder = builderWithMandatoryArguments() - .photoSize(photoSizeSelector); - - // Then - assertEquals( - photoSizeSelector, - builder.photoSizeSelector - ); - } - - @Test - public void previewSize_HasDefault() throws Exception { - // When - FotoapparatBuilder builder = builderWithMandatoryArguments(); - - // Then - assertNotNull(builder.previewSizeSelector); - } - - @Test - public void previewSize_IsConfigurable() throws Exception { - // When - FotoapparatBuilder builder = builderWithMandatoryArguments() - .previewSize(previewSizeSelector); - - // Then - assertEquals( - previewSizeSelector, - builder.previewSizeSelector - ); - } - - @Test(expected = IllegalStateException.class) - public void rendererIsMandatory() throws Exception { - // Given - FotoapparatBuilder builder = builderWithMandatoryArguments() - .into(null); - - // When - builder.build(); - - // Then - // Expect exception - } - - @Test(expected = IllegalStateException.class) - public void lensPositionIsMandatory() throws Exception { - // Given - FotoapparatBuilder builder = builderWithMandatoryArguments() - .lensPosition(null); - - // When - builder.build(); - - // Then - // Expect exception - } - - @Test(expected = IllegalStateException.class) - public void photoSizeIsMandatory() throws Exception { - // Given - FotoapparatBuilder builder = builderWithMandatoryArguments() - .photoSize(null); - - // When - builder.build(); - - // Then - // Expect exception - } - - private FotoapparatBuilder builderWithMandatoryArguments() { - return new FotoapparatBuilder(context) - .lensPosition(lensPositionSelector) - .photoSize(photoSizeSelector) - .into(cameraRenderer); - } + @Mock + Context context; + @Mock + CameraProvider cameraProvider; + @Mock + CameraRenderer cameraRenderer; + + @Mock + SelectorFunction photoSizeSelector; + @Mock + SelectorFunction previewSizeSelector; + @Mock + SelectorFunction lensPositionSelector; + @Mock + SelectorFunction focusModeSelector; + @Mock + SelectorFunction flashSelector; + + @Mock + FrameProcessor frameProcessor; + + @Mock + Logger logger; + + @Before + public void setUp() throws Exception { + given(context.getSystemService(Context.WINDOW_SERVICE)) + .willReturn(Mockito.mock(WindowManager.class)); + } + + @Test + public void onlyMandatoryParameters() throws Exception { + // Given + FotoapparatBuilder builder = builderWithMandatoryArguments(); + + // When + Fotoapparat result = builder.build(); + + // Then + assertNotNull(result); + } + + @Test + public void cameraProvider_HasDefault() throws Exception { + // When + FotoapparatBuilder builder = builderWithMandatoryArguments(); + + // Then + assertNotNull(builder.cameraProvider); + } + + @Test + public void cameraProvider_IsConfigurable() throws Exception { + // When + FotoapparatBuilder builder = builderWithMandatoryArguments() + .cameraProvider(cameraProvider); + + // Then + assertEquals( + cameraProvider, + builder.cameraProvider + ); + } + + @Test + public void logger_HasDefault() throws Exception { + // When + FotoapparatBuilder builder = builderWithMandatoryArguments(); + + // Then + assertNotNull(builder.logger); + } + + @Test + public void logger_IsConfigurable() throws Exception { + // When + FotoapparatBuilder builder = builderWithMandatoryArguments() + .logger(logger); + + // Then + assertEquals( + logger, + builder.logger + ); + } + + @Test + public void focusMode_HasDefault() throws Exception { + // When + FotoapparatBuilder builder = builderWithMandatoryArguments(); + + // Then + assertNotNull(builder.focusModeSelector); + } + + @Test + public void focusMode_IsConfigurable() throws Exception { + // When + FotoapparatBuilder builder = builderWithMandatoryArguments() + .focusMode(focusModeSelector); + + // Then + assertEquals( + focusModeSelector, + builder.focusModeSelector + ); + } + + @Test + public void flashMode_IsConfigurable() throws Exception { + // When + FotoapparatBuilder builder = builderWithMandatoryArguments() + .flash(flashSelector); + + // Then + assertEquals( + flashSelector, + builder.flashSelector + ); + } + + @Test + public void frameProcessor_NullByDefault() throws Exception { + // When + FotoapparatBuilder builder = builderWithMandatoryArguments(); + + // Then + assertNull(builder.frameProcessor); + } + + @Test + public void frameProcessor_IsConfigurable() throws Exception { + // When + FotoapparatBuilder builder = builderWithMandatoryArguments() + .frameProcessor(frameProcessor); + + // Then + assertEquals( + frameProcessor, + builder.frameProcessor + ); + } + + @Test + public void photoSize_IsConfigurable() throws Exception { + // When + FotoapparatBuilder builder = builderWithMandatoryArguments() + .photoSize(photoSizeSelector); + + // Then + assertEquals( + photoSizeSelector, + builder.photoSizeSelector + ); + } + + @Test + public void previewSize_HasDefault() throws Exception { + // When + FotoapparatBuilder builder = builderWithMandatoryArguments(); + + // Then + assertNotNull(builder.previewSizeSelector); + } + + @Test + public void previewSize_IsConfigurable() throws Exception { + // When + FotoapparatBuilder builder = builderWithMandatoryArguments() + .previewSize(previewSizeSelector); + + // Then + assertEquals( + previewSizeSelector, + builder.previewSizeSelector + ); + } + + @Test + public void previewStyle_HasDefault() throws Exception { + // When + FotoapparatBuilder builder = builderWithMandatoryArguments(); + + // Then + assertNotNull(builder.previewSizeSelector); + } + + @Test + public void previewStyle_IsConfigurable() throws Exception { + // When + FotoapparatBuilder builder = builderWithMandatoryArguments() + .previewStyle(previewSizeSelector); + + // Then + assertEquals( + previewSizeSelector, + builder.previewSizeSelector + ); + } + + @Test(expected = IllegalStateException.class) + public void rendererIsMandatory() throws Exception { + // Given + FotoapparatBuilder builder = builderWithMandatoryArguments() + .into(null); + + // When + builder.build(); + + // Then + // Expect exception + } + + @Test(expected = IllegalStateException.class) + public void lensPositionIsMandatory() throws Exception { + // Given + FotoapparatBuilder builder = builderWithMandatoryArguments() + .lensPosition(null); + + // When + builder.build(); + + // Then + // Expect exception + } + + @Test(expected = IllegalStateException.class) + public void photoSizeIsMandatory() throws Exception { + // Given + FotoapparatBuilder builder = builderWithMandatoryArguments() + .photoSize(null); + + // When + builder.build(); + + // Then + // Expect exception + } + + private FotoapparatBuilder builderWithMandatoryArguments() { + return new FotoapparatBuilder(context) + .lensPosition(lensPositionSelector) + .photoSize(photoSizeSelector) + .into(cameraRenderer); + } } \ No newline at end of file diff --git a/fotoapparat/src/test/java/io/fotoapparat/parameter/selector/ScaleTypeSelectorsTest.java b/fotoapparat/src/test/java/io/fotoapparat/parameter/selector/ScaleTypeSelectorsTest.java new file mode 100644 index 00000000..e881a264 --- /dev/null +++ b/fotoapparat/src/test/java/io/fotoapparat/parameter/selector/ScaleTypeSelectorsTest.java @@ -0,0 +1,57 @@ +package io.fotoapparat.parameter.selector; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.junit.MockitoJUnitRunner; + +import java.util.List; + +import io.fotoapparat.parameter.ScaleType; + +import static java.util.Arrays.asList; +import static junit.framework.Assert.assertEquals; + +@RunWith(MockitoJUnitRunner.class) +public class ScaleTypeSelectorsTest { + + + @Test + public void select_crop() throws Exception { + // Given + List availableScaleTypes = asList( + ScaleType.CENTER_CROP, + ScaleType.CENTER_INSIDE + ); + + // When + ScaleType result = ScaleTypeSelectors + .centerCropped() + .select(availableScaleTypes); + + // Then + assertEquals( + ScaleType.CENTER_CROP, + result + ); + } + + @Test + public void select_centerInside() throws Exception { + // Given + List availableScaleTypes = asList( + ScaleType.CENTER_CROP, + ScaleType.CENTER_INSIDE + ); + + // When + ScaleType result = ScaleTypeSelectors + .centerInside() + .select(availableScaleTypes); + + // Then + assertEquals( + ScaleType.CENTER_INSIDE, + result + ); + } +} \ No newline at end of file From 37ec1addde49bfe0050fcefc7864e9edb45a949e Mon Sep 17 00:00:00 2001 From: Dionysis Date: Sat, 15 Jul 2017 22:57:46 +0200 Subject: [PATCH 02/11] Add scale type in the builder --- .../main/java/io/fotoapparat/FotoapparatBuilder.java | 9 ++++++--- .../java/io/fotoapparat/FotoapparatBuilderTest.java | 11 +++++++---- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/fotoapparat/src/main/java/io/fotoapparat/FotoapparatBuilder.java b/fotoapparat/src/main/java/io/fotoapparat/FotoapparatBuilder.java index 69aae9cc..26ad1bcb 100644 --- a/fotoapparat/src/main/java/io/fotoapparat/FotoapparatBuilder.java +++ b/fotoapparat/src/main/java/io/fotoapparat/FotoapparatBuilder.java @@ -10,6 +10,7 @@ import io.fotoapparat.parameter.Flash; import io.fotoapparat.parameter.FocusMode; import io.fotoapparat.parameter.LensPosition; +import io.fotoapparat.parameter.ScaleType; import io.fotoapparat.parameter.Size; import io.fotoapparat.parameter.selector.FlashSelectors; import io.fotoapparat.parameter.selector.SelectorFunction; @@ -24,6 +25,7 @@ import static io.fotoapparat.parameter.selector.LensPositionSelectors.back; import static io.fotoapparat.parameter.selector.LensPositionSelectors.external; import static io.fotoapparat.parameter.selector.LensPositionSelectors.front; +import static io.fotoapparat.parameter.selector.ScaleTypeSelectors.centerCropped; import static io.fotoapparat.parameter.selector.Selectors.firstAvailable; import static io.fotoapparat.parameter.selector.SizeSelectors.biggestSize; @@ -43,6 +45,7 @@ public class FotoapparatBuilder { ); SelectorFunction photoSizeSelector = biggestSize(); SelectorFunction previewSizeSelector = biggestSize(); + SelectorFunction previewScaleTypeSelector = centerCropped(); SelectorFunction focusModeSelector = firstAvailable( continuousFocus(), autoFocus(), @@ -83,10 +86,10 @@ public FotoapparatBuilder previewSize(SelectorFunction selector) { } /** - * @param selector selects size of preview stream (in pixels) from list of available sizes. + * @param selector selects scale type of preview inside the view. */ - public FotoapparatBuilder previewStyle(SelectorFunction selector) { - previewSizeSelector = selector; + public FotoapparatBuilder previewScaleType(SelectorFunction selector) { + previewScaleTypeSelector = selector; return this; } diff --git a/fotoapparat/src/test/java/io/fotoapparat/FotoapparatBuilderTest.java b/fotoapparat/src/test/java/io/fotoapparat/FotoapparatBuilderTest.java index 9f665dd0..735366fc 100644 --- a/fotoapparat/src/test/java/io/fotoapparat/FotoapparatBuilderTest.java +++ b/fotoapparat/src/test/java/io/fotoapparat/FotoapparatBuilderTest.java @@ -15,6 +15,7 @@ import io.fotoapparat.parameter.Flash; import io.fotoapparat.parameter.FocusMode; import io.fotoapparat.parameter.LensPosition; +import io.fotoapparat.parameter.ScaleType; import io.fotoapparat.parameter.Size; import io.fotoapparat.parameter.selector.SelectorFunction; import io.fotoapparat.preview.FrameProcessor; @@ -40,6 +41,8 @@ public class FotoapparatBuilderTest { @Mock SelectorFunction previewSizeSelector; @Mock + SelectorFunction previewScaleTypeSelector; + @Mock SelectorFunction lensPositionSelector; @Mock SelectorFunction focusModeSelector; @@ -212,19 +215,19 @@ public void previewStyle_HasDefault() throws Exception { FotoapparatBuilder builder = builderWithMandatoryArguments(); // Then - assertNotNull(builder.previewSizeSelector); + assertNotNull(builder.previewScaleTypeSelector); } @Test public void previewStyle_IsConfigurable() throws Exception { // When FotoapparatBuilder builder = builderWithMandatoryArguments() - .previewStyle(previewSizeSelector); + .previewScaleType(previewScaleTypeSelector); // Then assertEquals( - previewSizeSelector, - builder.previewSizeSelector + previewScaleTypeSelector, + builder.previewScaleTypeSelector ); } From 628781b7067f36bb70a5b1eb94db3fdb0a266880 Mon Sep 17 00:00:00 2001 From: Dionysis Date: Sun, 16 Jul 2017 15:40:50 +0200 Subject: [PATCH 03/11] Camera 1 now supports preview scale type --- .../main/java/io/fotoapparat/Fotoapparat.java | 1 + .../io/fotoapparat/hardware/Capabilities.java | 13 ++ .../io/fotoapparat/hardware/v1/Camera1.java | 8 +- .../v1/capabilities/CapabilitiesFactory.java | 137 ++++++++------- .../v2/capabilities/CapabilitiesFactory.java | 156 ++++++++++-------- .../RendererParametersProvider.java | 4 +- .../io/fotoapparat/parameter/Parameters.java | 5 + .../parameter/RendererParameters.java | 17 +- .../io/fotoapparat/parameter/ScaleType.java | 2 +- .../provider/InitialParametersProvider.java | 19 +++ .../routine/StartCameraRoutine.java | 62 +++---- .../fotoapparat/view/TextureRendererView.java | 43 ++++- .../fotoapparat/hardware/v2/Camera2Test.java | 3 +- .../RendererParametersProviderTest.java | 4 +- .../InitialParametersProviderTest.java | 25 +++ .../task/GetCapabilitiesTaskTest.java | 1 + 16 files changed, 315 insertions(+), 185 deletions(-) diff --git a/fotoapparat/src/main/java/io/fotoapparat/Fotoapparat.java b/fotoapparat/src/main/java/io/fotoapparat/Fotoapparat.java index c47e237e..007dfb3e 100644 --- a/fotoapparat/src/main/java/io/fotoapparat/Fotoapparat.java +++ b/fotoapparat/src/main/java/io/fotoapparat/Fotoapparat.java @@ -80,6 +80,7 @@ static Fotoapparat create(FotoapparatBuilder builder) { cameraDevice, builder.photoSizeSelector, builder.previewSizeSelector, + builder.previewScaleTypeSelector, builder.focusModeSelector, builder.flashSelector, parametersValidator diff --git a/fotoapparat/src/main/java/io/fotoapparat/hardware/Capabilities.java b/fotoapparat/src/main/java/io/fotoapparat/hardware/Capabilities.java index 125f305c..dd13801e 100644 --- a/fotoapparat/src/main/java/io/fotoapparat/hardware/Capabilities.java +++ b/fotoapparat/src/main/java/io/fotoapparat/hardware/Capabilities.java @@ -7,6 +7,7 @@ import io.fotoapparat.parameter.Flash; import io.fotoapparat.parameter.FocusMode; +import io.fotoapparat.parameter.ScaleType; import io.fotoapparat.parameter.Size; /** @@ -19,16 +20,20 @@ public class Capabilities { @NonNull private final Set previewSizes; @NonNull + private Set previewScaleTypes; + @NonNull private final Set focusModes; @NonNull private final Set flashModes; public Capabilities(@NonNull Set photoSizes, @NonNull Set previewSizes, + @NonNull Set previewScaleTypes, @NonNull Set focusModes, @NonNull Set flashModes) { this.photoSizes = photoSizes; this.previewSizes = previewSizes; + this.previewScaleTypes = previewScaleTypes; this.focusModes = focusModes; this.flashModes = flashModes; } @@ -40,6 +45,7 @@ public static Capabilities empty() { return new Capabilities( Collections.emptySet(), Collections.emptySet(), + Collections.emptySet(), Collections.emptySet(), Collections.emptySet() ); @@ -59,6 +65,13 @@ public Set supportedPreviewSizes() { return previewSizes; } + /** + * @return list of supported preview scale types; + */ + public Set supportedPreviewScaleTypes() { + return previewScaleTypes; + } + /** * @return list of supported focus modes. */ diff --git a/fotoapparat/src/main/java/io/fotoapparat/hardware/v1/Camera1.java b/fotoapparat/src/main/java/io/fotoapparat/hardware/v1/Camera1.java index 1a3e1205..1483ce4e 100644 --- a/fotoapparat/src/main/java/io/fotoapparat/hardware/v1/Camera1.java +++ b/fotoapparat/src/main/java/io/fotoapparat/hardware/v1/Camera1.java @@ -21,7 +21,9 @@ import io.fotoapparat.log.Logger; import io.fotoapparat.parameter.LensPosition; import io.fotoapparat.parameter.Parameters; +import io.fotoapparat.parameter.Parameters.Type; import io.fotoapparat.parameter.RendererParameters; +import io.fotoapparat.parameter.ScaleType; import io.fotoapparat.parameter.Size; import io.fotoapparat.photo.Photo; import io.fotoapparat.preview.PreviewStream; @@ -43,6 +45,7 @@ public class Camera1 implements CameraDevice { private Throwable lastStacktrace; private int imageRotation; + private ScaleType previewScaleType; public Camera1(Logger logger) { this.capabilitiesFactory = new CapabilitiesFactory(); @@ -176,6 +179,8 @@ public void updateParameters(Parameters parameters) { ); camera.setParameters(cameraParameters); + + previewScaleType = parameters.getValue(Type.PREVIEW_SCALE_TYPE); } @Override @@ -249,7 +254,8 @@ public RendererParameters getRendererParameters() { return new RendererParameters( previewSize(), - imageRotation + imageRotation, + previewScaleType ); } diff --git a/fotoapparat/src/main/java/io/fotoapparat/hardware/v1/capabilities/CapabilitiesFactory.java b/fotoapparat/src/main/java/io/fotoapparat/hardware/v1/capabilities/CapabilitiesFactory.java index b2027da2..290f381e 100644 --- a/fotoapparat/src/main/java/io/fotoapparat/hardware/v1/capabilities/CapabilitiesFactory.java +++ b/fotoapparat/src/main/java/io/fotoapparat/hardware/v1/capabilities/CapabilitiesFactory.java @@ -13,6 +13,7 @@ import io.fotoapparat.hardware.v1.Camera1; import io.fotoapparat.parameter.Flash; import io.fotoapparat.parameter.FocusMode; +import io.fotoapparat.parameter.ScaleType; import io.fotoapparat.parameter.Size; /** @@ -21,69 +22,77 @@ @SuppressWarnings("deprecation") public class CapabilitiesFactory { - /** - * @return {@link Capabilities} from given camera parameters. - */ - public Capabilities fromParameters(Camera.Parameters parameters) { - return new Capabilities( - extractPictureSizes(parameters), - extractPreviewSizes(parameters), - extractFocusModes(parameters), - extractFlashModes(parameters) - ); - } - - private Set extractPreviewSizes(Camera.Parameters parameters) { - return mapSizes(parameters.getSupportedPreviewSizes()); - } - - private Set extractPictureSizes(Camera.Parameters parameters) { - return mapSizes(parameters.getSupportedPictureSizes()); - } - - private Set mapSizes(Collection sizes) { - HashSet result = new HashSet<>(); - - for (Camera.Size size : sizes) { - result.add(new Size( - size.width, - size.height - )); - } - - return result; - } - - private Set extractFlashModes(Camera.Parameters parameters) { - HashSet result = new HashSet<>(); - - for (String flashMode : supportedFlashModes(parameters)) { - result.add( - FlashCapability.toFlash(flashMode) - ); - } - - return result; - } - - @NonNull - private List supportedFlashModes(Camera.Parameters parameters) { - List supportedFlashModes = parameters.getSupportedFlashModes(); - return supportedFlashModes != null - ? supportedFlashModes - : Collections.singletonList(Camera.Parameters.FLASH_MODE_OFF); - } - - private Set extractFocusModes(Camera.Parameters parameters) { - HashSet result = new HashSet<>(); - - for (String focusMode : parameters.getSupportedFocusModes()) { - result.add( - FocusCapability.toFocusMode(focusMode) - ); - } - - return result; - } + /** + * @return {@link Capabilities} from given camera parameters. + */ + public Capabilities fromParameters(Camera.Parameters parameters) { + return new Capabilities( + extractPictureSizes(parameters), + extractPreviewSizes(parameters), + setPreviewScaleTypes(), + extractFocusModes(parameters), + extractFlashModes(parameters) + ); + } + + private Set extractPreviewSizes(Camera.Parameters parameters) { + return mapSizes(parameters.getSupportedPreviewSizes()); + } + + private Set extractPictureSizes(Camera.Parameters parameters) { + return mapSizes(parameters.getSupportedPictureSizes()); + } + + private Set mapSizes(Collection sizes) { + HashSet result = new HashSet<>(); + + for (Camera.Size size : sizes) { + result.add(new Size( + size.width, + size.height + )); + } + + return result; + } + + private Set extractFlashModes(Camera.Parameters parameters) { + HashSet result = new HashSet<>(); + + for (String flashMode : supportedFlashModes(parameters)) { + result.add( + FlashCapability.toFlash(flashMode) + ); + } + + return result; + } + + @NonNull + private List supportedFlashModes(Camera.Parameters parameters) { + List supportedFlashModes = parameters.getSupportedFlashModes(); + return supportedFlashModes != null + ? supportedFlashModes + : Collections.singletonList(Camera.Parameters.FLASH_MODE_OFF); + } + + private Set extractFocusModes(Camera.Parameters parameters) { + HashSet result = new HashSet<>(); + + for (String focusMode : parameters.getSupportedFocusModes()) { + result.add( + FocusCapability.toFocusMode(focusMode) + ); + } + + return result; + } + + private Set setPreviewScaleTypes() { + HashSet previewScaleTypes = new HashSet<>(); + previewScaleTypes.add(ScaleType.CENTER_CROP); + previewScaleTypes.add(ScaleType.CENTER_INSIDE); + return previewScaleTypes; + } } diff --git a/fotoapparat/src/main/java/io/fotoapparat/hardware/v2/capabilities/CapabilitiesFactory.java b/fotoapparat/src/main/java/io/fotoapparat/hardware/v2/capabilities/CapabilitiesFactory.java index be8f6735..bc0c49a3 100644 --- a/fotoapparat/src/main/java/io/fotoapparat/hardware/v2/capabilities/CapabilitiesFactory.java +++ b/fotoapparat/src/main/java/io/fotoapparat/hardware/v2/capabilities/CapabilitiesFactory.java @@ -13,9 +13,11 @@ import io.fotoapparat.hardware.v2.parameters.converters.FocusConverter; import io.fotoapparat.parameter.Flash; import io.fotoapparat.parameter.FocusMode; +import io.fotoapparat.parameter.ScaleType; import io.fotoapparat.parameter.Size; import static io.fotoapparat.hardware.v2.parameters.converters.FlashConverter.exposureModeToFlash; +import static java.util.Arrays.asList; /** * Creates the {@link Capabilities} of a {@link io.fotoapparat.hardware.v2.Camera2}. @@ -23,78 +25,86 @@ @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) public class CapabilitiesFactory implements CapabilitiesOperator { - private final CameraConnection cameraConnection; - - public CapabilitiesFactory(CameraConnection cameraConnection) { - this.cameraConnection = cameraConnection; - } - - @Override - public Capabilities getCapabilities() { - return new Capabilities( - availableJpegSizes(), - availablePreviewSizes(), - availableFocusModes(), - availableFlashModes() - ); - } - - @SuppressWarnings("ConstantConditions") - private Set availableJpegSizes() { - return characteristics().getJpegOutputSizes(); - } - - @SuppressWarnings("ConstantConditions") - private Set availablePreviewSizes() { - HashSet filteredOutputSizes = new HashSet<>(); - for (Size outputSize : characteristics().getSurfaceOutputSizes()) { - if (outputSize.width <= PreviewSizeInfo.MAX_PREVIEW_WIDTH && outputSize.height <= PreviewSizeInfo.MAX_PREVIEW_HEIGHT) { - filteredOutputSizes.add(outputSize); - } - } - - return filteredOutputSizes; - } - - @SuppressWarnings("ConstantConditions") - private Set availableFocusModes() { - Set focusModes = new HashSet<>(); - for (int afMode : characteristics().autoFocusModes()) { - focusModes.add(FocusConverter.afModeToFocus(afMode)); - } - - return focusModes; - } - - @SuppressWarnings("ConstantConditions") - private Set availableFlashModes() { - if (characteristics().isFlashAvailable()) { - return availableFlashUnitModes(); - } - return Collections.singleton(Flash.OFF); - - } - - @SuppressWarnings("ConstantConditions") - private Set availableFlashUnitModes() { - Set flashes = new HashSet<>(); - flashes.add(Flash.OFF); - flashes.add(Flash.TORCH); - - int[] autoExposureModes = characteristics().autoExposureModes(); - - for (int autoExposureMode : autoExposureModes) { - Flash flash = exposureModeToFlash(autoExposureMode); - if (flash != null) { - flashes.add(flash); - } - } - - return flashes; - } - - private Characteristics characteristics() { - return cameraConnection.getCharacteristics(); - } + private final CameraConnection cameraConnection; + + public CapabilitiesFactory(CameraConnection cameraConnection) { + this.cameraConnection = cameraConnection; + } + + @Override + public Capabilities getCapabilities() { + return new Capabilities( + availableJpegSizes(), + availablePreviewSizes(), + availablePreviewScaleTypes(), + availableFocusModes(), + availableFlashModes() + ); + } + + private Set availablePreviewScaleTypes() { + return new HashSet<>(asList( + ScaleType.CENTER_INSIDE, + ScaleType.CENTER_CROP + )); + } + + @SuppressWarnings("ConstantConditions") + private Set availableJpegSizes() { + return characteristics().getJpegOutputSizes(); + } + + @SuppressWarnings("ConstantConditions") + private Set availablePreviewSizes() { + HashSet filteredOutputSizes = new HashSet<>(); + for (Size outputSize : characteristics().getSurfaceOutputSizes()) { + if (outputSize.width <= PreviewSizeInfo.MAX_PREVIEW_WIDTH && outputSize.height <= PreviewSizeInfo.MAX_PREVIEW_HEIGHT) { + filteredOutputSizes.add(outputSize); + } + } + + return filteredOutputSizes; + } + + @SuppressWarnings("ConstantConditions") + private Set availableFocusModes() { + Set focusModes = new HashSet<>(); + for (int afMode : characteristics().autoFocusModes()) { + focusModes.add(FocusConverter.afModeToFocus(afMode)); + } + + return focusModes; + } + + @SuppressWarnings("ConstantConditions") + private Set availableFlashModes() { + if (characteristics().isFlashAvailable()) { + return availableFlashUnitModes(); + } + return Collections.singleton(Flash.OFF); + + } + + @SuppressWarnings("ConstantConditions") + private Set availableFlashUnitModes() { + Set flashes = new HashSet<>(); + flashes.add(Flash.OFF); + flashes.add(Flash.TORCH); + + int[] autoExposureModes = characteristics().autoExposureModes(); + + for (int autoExposureMode : autoExposureModes) { + Flash flash = exposureModeToFlash(autoExposureMode); + if (flash != null) { + flashes.add(flash); + } + } + + return flashes; + } + + private Characteristics characteristics() { + return cameraConnection.getCharacteristics(); + } } diff --git a/fotoapparat/src/main/java/io/fotoapparat/hardware/v2/parameters/RendererParametersProvider.java b/fotoapparat/src/main/java/io/fotoapparat/hardware/v2/parameters/RendererParametersProvider.java index 191f710a..e2ab9dcb 100644 --- a/fotoapparat/src/main/java/io/fotoapparat/hardware/v2/parameters/RendererParametersProvider.java +++ b/fotoapparat/src/main/java/io/fotoapparat/hardware/v2/parameters/RendererParametersProvider.java @@ -3,6 +3,7 @@ import io.fotoapparat.hardware.operators.RendererParametersOperator; import io.fotoapparat.hardware.v2.orientation.OrientationManager; import io.fotoapparat.parameter.RendererParameters; +import io.fotoapparat.parameter.ScaleType; /** * Provides {@link RendererParameters} for camera v2. @@ -23,7 +24,8 @@ public RendererParametersProvider(ParametersProvider parametersProvider, public RendererParameters getRendererParameters() { return new RendererParameters( parametersProvider.getPreviewSize(), - orientationManager.getPhotoOrientation() + orientationManager.getPhotoOrientation(), + ScaleType.CENTER_CROP ); } diff --git a/fotoapparat/src/main/java/io/fotoapparat/parameter/Parameters.java b/fotoapparat/src/main/java/io/fotoapparat/parameter/Parameters.java index 661c6562..4eb3908a 100644 --- a/fotoapparat/src/main/java/io/fotoapparat/parameter/Parameters.java +++ b/fotoapparat/src/main/java/io/fotoapparat/parameter/Parameters.java @@ -79,6 +79,11 @@ public enum Type { */ PREVIEW_SIZE(Size.class), + /** + * Scale type of the preview stream frames. Expected type: {@link ScaleType}. + */ + PREVIEW_SCALE_TYPE(ScaleType.class), + /** * Focus mode of the camera. Expected type: {@link FocusMode}. */ diff --git a/fotoapparat/src/main/java/io/fotoapparat/parameter/RendererParameters.java b/fotoapparat/src/main/java/io/fotoapparat/parameter/RendererParameters.java index f12d6f25..36f73cd4 100644 --- a/fotoapparat/src/main/java/io/fotoapparat/parameter/RendererParameters.java +++ b/fotoapparat/src/main/java/io/fotoapparat/parameter/RendererParameters.java @@ -19,10 +19,18 @@ public class RendererParameters { */ public final int frameRotation; + + /** + * The type of the scaling the preview. + */ + public final ScaleType scaleType; + public RendererParameters(Size previewSize, - int frameRotation) { + int frameRotation, + ScaleType scaleType) { this.previewSize = previewSize; this.frameRotation = frameRotation; + this.scaleType = scaleType; } @Override @@ -32,14 +40,17 @@ public boolean equals(Object o) { RendererParameters that = (RendererParameters) o; - return frameRotation == that.frameRotation && (previewSize != null ? previewSize.equals(that.previewSize) : that.previewSize == null); - + if (frameRotation != that.frameRotation) return false; + if (previewSize != null ? !previewSize.equals(that.previewSize) : that.previewSize != null) + return false; + return scaleType == that.scaleType; } @Override public int hashCode() { int result = previewSize != null ? previewSize.hashCode() : 0; result = 31 * result + frameRotation; + result = 31 * result + (scaleType != null ? scaleType.hashCode() : 0); return result; } } diff --git a/fotoapparat/src/main/java/io/fotoapparat/parameter/ScaleType.java b/fotoapparat/src/main/java/io/fotoapparat/parameter/ScaleType.java index 461c2266..9c067f74 100644 --- a/fotoapparat/src/main/java/io/fotoapparat/parameter/ScaleType.java +++ b/fotoapparat/src/main/java/io/fotoapparat/parameter/ScaleType.java @@ -1,7 +1,7 @@ package io.fotoapparat.parameter; /** - * The style of the preview. + * The scale type of the preview relatively to the view. */ public enum ScaleType { diff --git a/fotoapparat/src/main/java/io/fotoapparat/parameter/provider/InitialParametersProvider.java b/fotoapparat/src/main/java/io/fotoapparat/parameter/provider/InitialParametersProvider.java index 0338b8af..2d82372d 100644 --- a/fotoapparat/src/main/java/io/fotoapparat/parameter/provider/InitialParametersProvider.java +++ b/fotoapparat/src/main/java/io/fotoapparat/parameter/provider/InitialParametersProvider.java @@ -6,6 +6,7 @@ import io.fotoapparat.parameter.Flash; import io.fotoapparat.parameter.FocusMode; import io.fotoapparat.parameter.Parameters; +import io.fotoapparat.parameter.ScaleType; import io.fotoapparat.parameter.Size; import io.fotoapparat.parameter.selector.SelectorFunction; import io.fotoapparat.parameter.selector.Selectors; @@ -21,18 +22,21 @@ public class InitialParametersProvider { private final CapabilitiesOperator capabilitiesOperator; private final SelectorFunction photoSizeSelector; private final SelectorFunction previewSizeSelector; + private final SelectorFunction previewScaleTypeSelector; private final SelectorFunction focusModeSelector; private final SelectorFunction flashSelector; public InitialParametersProvider(CapabilitiesOperator capabilitiesOperator, SelectorFunction photoSizeSelector, SelectorFunction previewSizeSelector, + SelectorFunction previewScaleTypeSelector, SelectorFunction focusModeSelector, SelectorFunction flashSelector, InitialParametersValidator parametersValidator) { this.capabilitiesOperator = capabilitiesOperator; this.photoSizeSelector = photoSizeSelector; this.previewSizeSelector = previewSizeSelector; + this.previewScaleTypeSelector = previewScaleTypeSelector; this.focusModeSelector = focusModeSelector; this.flashSelector = flashSelector; this.parametersValidator = parametersValidator; @@ -48,6 +52,7 @@ public Parameters initialParameters() { putPictureSize(capabilities, parameters); putPreviewSize(capabilities, parameters); + putPreviewScaleType(capabilities, parameters); putFocusMode(capabilities, parameters); putFlash(capabilities, parameters); @@ -56,6 +61,7 @@ public Parameters initialParameters() { return parameters; } + private void putPreviewSize(Capabilities capabilities, Parameters parameters) { Size photoSize = photoSize(capabilities); @@ -70,6 +76,13 @@ private void putPreviewSize(Capabilities capabilities, Parameters parameters) { ); } + private void putPreviewScaleType(Capabilities capabilities, Parameters parameters) { + parameters.putValue( + Parameters.Type.PREVIEW_SCALE_TYPE, + previewScaleType(capabilities) + ); + } + private SelectorFunction previewWithSameAspectRatio(Size photoSize) { return aspectRatio( photoSize.getAspectRatio(), @@ -90,6 +103,12 @@ private Size photoSize(Capabilities capabilities) { ); } + private ScaleType previewScaleType(Capabilities capabilities) { + return previewScaleTypeSelector.select( + capabilities.supportedPreviewScaleTypes() + ); + } + private void putFocusMode(Capabilities capabilities, Parameters parameters) { parameters.putValue( Parameters.Type.FOCUS_MODE, diff --git a/fotoapparat/src/main/java/io/fotoapparat/routine/StartCameraRoutine.java b/fotoapparat/src/main/java/io/fotoapparat/routine/StartCameraRoutine.java index 3e022cff..f0a586ab 100644 --- a/fotoapparat/src/main/java/io/fotoapparat/routine/StartCameraRoutine.java +++ b/fotoapparat/src/main/java/io/fotoapparat/routine/StartCameraRoutine.java @@ -12,38 +12,38 @@ */ public class StartCameraRoutine implements Runnable { - private final CameraDevice cameraDevice; - private final CameraRenderer cameraRenderer; - private final SelectorFunction lensPositionSelector; - private final ScreenOrientationProvider screenOrientationProvider; - private final InitialParametersProvider initialParametersProvider; + private final CameraDevice cameraDevice; + private final CameraRenderer cameraRenderer; + private final SelectorFunction lensPositionSelector; + private final ScreenOrientationProvider screenOrientationProvider; + private final InitialParametersProvider initialParametersProvider; - public StartCameraRoutine(CameraDevice cameraDevice, - CameraRenderer cameraRenderer, - SelectorFunction lensPositionSelector, - ScreenOrientationProvider screenOrientationProvider, - InitialParametersProvider initialParametersProvider) { - this.cameraDevice = cameraDevice; - this.cameraRenderer = cameraRenderer; - this.lensPositionSelector = lensPositionSelector; - this.screenOrientationProvider = screenOrientationProvider; - this.initialParametersProvider = initialParametersProvider; - } + public StartCameraRoutine(CameraDevice cameraDevice, + CameraRenderer cameraRenderer, + SelectorFunction lensPositionSelector, + ScreenOrientationProvider screenOrientationProvider, + InitialParametersProvider initialParametersProvider) { + this.cameraDevice = cameraDevice; + this.cameraRenderer = cameraRenderer; + this.lensPositionSelector = lensPositionSelector; + this.screenOrientationProvider = screenOrientationProvider; + this.initialParametersProvider = initialParametersProvider; + } - @Override - public void run() { - LensPosition lensPosition = lensPositionSelector.select( - cameraDevice.getAvailableLensPositions() - ); + @Override + public void run() { + LensPosition lensPosition = lensPositionSelector.select( + cameraDevice.getAvailableLensPositions() + ); - cameraDevice.open(lensPosition); - cameraDevice.updateParameters( - initialParametersProvider.initialParameters() - ); - cameraDevice.setDisplayOrientation( - screenOrientationProvider.getScreenRotation() - ); - cameraRenderer.attachCamera(cameraDevice); - cameraDevice.startPreview(); - } + cameraDevice.open(lensPosition); + cameraDevice.updateParameters( + initialParametersProvider.initialParameters() + ); + cameraDevice.setDisplayOrientation( + screenOrientationProvider.getScreenRotation() + ); + cameraRenderer.attachCamera(cameraDevice); + cameraDevice.startPreview(); + } } diff --git a/fotoapparat/src/main/java/io/fotoapparat/view/TextureRendererView.java b/fotoapparat/src/main/java/io/fotoapparat/view/TextureRendererView.java index 8fead785..a3a69c03 100644 --- a/fotoapparat/src/main/java/io/fotoapparat/view/TextureRendererView.java +++ b/fotoapparat/src/main/java/io/fotoapparat/view/TextureRendererView.java @@ -16,6 +16,7 @@ import io.fotoapparat.hardware.CameraDevice; import io.fotoapparat.parameter.RendererParameters; +import io.fotoapparat.parameter.ScaleType; import io.fotoapparat.parameter.Size; /** @@ -28,7 +29,7 @@ class TextureRendererView extends FrameLayout implements CameraRenderer { private SurfaceTexture surfaceTexture; private TextureView textureView; - private Size previewSize = null; + private RendererParameters rendererParameters = null; public TextureRendererView(@NonNull Context context) { super(context); @@ -82,15 +83,11 @@ public void attachCamera(CameraDevice camera) { camera.setDisplaySurface(textureView); } - private void updateLayout(CameraDevice camera) { - final Size previewSize = toPreviewSize( - camera.getRendererParameters() - ); - + private void updateLayout(final CameraDevice camera) { post(new Runnable() { @Override public void run() { - TextureRendererView.this.previewSize = previewSize; + rendererParameters = camera.getRendererParameters(); requestLayout(); } @@ -117,11 +114,41 @@ private void awaitSurfaceTexture() { @Override protected void onLayout(boolean changed, int left, int top, int right, int bottom) { - if (previewSize == null) { + if (rendererParameters == null) { super.onLayout(changed, left, top, right, bottom); return; } + Size previewSize = toPreviewSize(rendererParameters); + + if (rendererParameters.scaleType == ScaleType.CENTER_INSIDE) { + centerInside(previewSize); + } else { + centerCrop(previewSize); + } + } + + private void centerInside(Size previewSize) { + final float scale = Math.min( + getMeasuredWidth() / (float) previewSize.width, + getMeasuredHeight() / (float) previewSize.height + ); + + final int width = (int) (previewSize.width * scale); + final int height = (int) (previewSize.height * scale); + + final int extraX = Math.max(0, getMeasuredWidth() - width); + final int extraY = Math.max(0, getMeasuredHeight() - height); + + getChildAt(0).layout( + extraX / 2, + extraY / 2, + width + (extraX / 2), + height + (extraY / 2) + ); + } + + private void centerCrop(Size previewSize) { final float scale = Math.max( getMeasuredWidth() / (float) previewSize.width, getMeasuredHeight() / (float) previewSize.height diff --git a/fotoapparat/src/test/java/io/fotoapparat/hardware/v2/Camera2Test.java b/fotoapparat/src/test/java/io/fotoapparat/hardware/v2/Camera2Test.java index c956d0cb..ccf15a95 100644 --- a/fotoapparat/src/test/java/io/fotoapparat/hardware/v2/Camera2Test.java +++ b/fotoapparat/src/test/java/io/fotoapparat/hardware/v2/Camera2Test.java @@ -155,6 +155,7 @@ public void getCapabilities() throws Exception { Capabilities capabilities = new Capabilities( Collections.emptySet(), Collections.emptySet(), + previewScaleTypes, singleton(FocusMode.MACRO), Collections.emptySet() ); @@ -225,7 +226,7 @@ public void measureExposure() throws Exception { @Test public void getRendererParameters() throws Exception { // Given - RendererParameters rendererParameters = new RendererParameters(new Size(1920, 1080), 0); + RendererParameters rendererParameters = new RendererParameters(new Size(1920, 1080), 0, scaleType); given(rendererParametersOperator.getRendererParameters()) .willReturn(rendererParameters); diff --git a/fotoapparat/src/test/java/io/fotoapparat/hardware/v2/parameters/RendererParametersProviderTest.java b/fotoapparat/src/test/java/io/fotoapparat/hardware/v2/parameters/RendererParametersProviderTest.java index d6c3e5ac..799e4f39 100644 --- a/fotoapparat/src/test/java/io/fotoapparat/hardware/v2/parameters/RendererParametersProviderTest.java +++ b/fotoapparat/src/test/java/io/fotoapparat/hardware/v2/parameters/RendererParametersProviderTest.java @@ -42,8 +42,8 @@ public void getParameters() throws Exception { assertEquals( new RendererParameters( size, - sensorOrientation - ), + sensorOrientation, + scaleType), rendererParameters ); diff --git a/fotoapparat/src/test/java/io/fotoapparat/parameter/provider/InitialParametersProviderTest.java b/fotoapparat/src/test/java/io/fotoapparat/parameter/provider/InitialParametersProviderTest.java index 1b8ca292..4c81d69e 100644 --- a/fotoapparat/src/test/java/io/fotoapparat/parameter/provider/InitialParametersProviderTest.java +++ b/fotoapparat/src/test/java/io/fotoapparat/parameter/provider/InitialParametersProviderTest.java @@ -13,6 +13,7 @@ import io.fotoapparat.parameter.Flash; import io.fotoapparat.parameter.FocusMode; import io.fotoapparat.parameter.Parameters; +import io.fotoapparat.parameter.ScaleType; import io.fotoapparat.parameter.Size; import io.fotoapparat.parameter.selector.SelectorFunction; @@ -30,6 +31,11 @@ public class InitialParametersProviderTest { static final Size PREVIEW_SIZE = new Size(2000, 1500); static final Size PREVIEW_SIZE_WRONG_ASPECT_RATIO = new Size(1000, 1000); + static final Set ALL_PREVIEW_SCALE_TYPE = asSet( + ScaleType.CENTER_CROP, + ScaleType.CENTER_INSIDE + ); + static final Set FOCUS_MODES = asSet(FocusMode.FIXED); static final Set FLASH = asSet(Flash.AUTO_RED_EYE); @@ -47,6 +53,8 @@ public class InitialParametersProviderTest { @Mock SelectorFunction previewSizeSelector; @Mock + SelectorFunction previewScaleTypeSelector; + @Mock SelectorFunction focusModeSelector; @Mock SelectorFunction flashModeSelector; @@ -61,6 +69,7 @@ public void setUp() throws Exception { cameraDevice, photoSizeSelector, previewSizeSelector, + previewScaleTypeSelector, focusModeSelector, flashModeSelector, initialParametersValidator @@ -70,6 +79,7 @@ public void setUp() throws Exception { .willReturn(new Capabilities( PHOTO_SIZES, ALL_PREVIEW_SIZES, + ALL_PREVIEW_SCALE_TYPE, FOCUS_MODES, FLASH )); @@ -78,6 +88,8 @@ public void setUp() throws Exception { .willReturn(PHOTO_SIZE); given(previewSizeSelector.select(VALID_PREVIEW_SIZES)) .willReturn(PREVIEW_SIZE); + given(previewScaleTypeSelector.select(ALL_PREVIEW_SCALE_TYPE)) + .willReturn(ScaleType.CENTER_CROP); given(focusModeSelector.select(FOCUS_MODES)) .willReturn(FocusMode.FIXED); given(flashModeSelector.select(FLASH)) @@ -120,6 +132,18 @@ public void selectPhotoSize() throws Exception { ); } + @Test + public void selectPreviewScaleType() throws Exception { + // When + Parameters parameters = testee.initialParameters(); + + // Then + assertEquals( + ScaleType.CENTER_CROP, + parameters.getValue(Parameters.Type.PREVIEW_SCALE_TYPE) + ); + } + @Test public void selectPreviewSize_WithValidAspectRatio() throws Exception { // When @@ -147,6 +171,7 @@ public void selectPreviewSize_SameAspectRatioNotAvailable() throws Exception { .willReturn(new Capabilities( photoSizes, ALL_PREVIEW_SIZES, + ALL_PREVIEW_SCALE_TYPE, FOCUS_MODES, FLASH )); diff --git a/fotoapparat/src/test/java/io/fotoapparat/task/GetCapabilitiesTaskTest.java b/fotoapparat/src/test/java/io/fotoapparat/task/GetCapabilitiesTaskTest.java index 87a0f963..f0a4212a 100644 --- a/fotoapparat/src/test/java/io/fotoapparat/task/GetCapabilitiesTaskTest.java +++ b/fotoapparat/src/test/java/io/fotoapparat/task/GetCapabilitiesTaskTest.java @@ -27,6 +27,7 @@ public class GetCapabilitiesTaskTest { private static final Capabilities CAPABILITIES = new Capabilities( Collections.singleton(new Size(1400, 1080)), Collections.singleton(new Size(1400, 1080)), + previewScaleTypes, Collections.singleton(FocusMode.CONTINUOUS_FOCUS), Collections.singleton(Flash.OFF) ); From 898769b1a4ff5e09e1973387614f1d0aadadbfa5 Mon Sep 17 00:00:00 2001 From: Dionysis Date: Sun, 16 Jul 2017 20:24:27 +0200 Subject: [PATCH 04/11] Camera 2 tests --- .../test/java/io/fotoapparat/hardware/v2/Camera2Test.java | 5 +++-- .../v2/parameters/RendererParametersProviderTest.java | 4 +++- .../java/io/fotoapparat/task/GetCapabilitiesTaskTest.java | 3 ++- gradle.properties | 2 +- gradle/wrapper/gradle-wrapper.properties | 2 +- 5 files changed, 10 insertions(+), 6 deletions(-) diff --git a/fotoapparat/src/test/java/io/fotoapparat/hardware/v2/Camera2Test.java b/fotoapparat/src/test/java/io/fotoapparat/hardware/v2/Camera2Test.java index ccf15a95..51e6db95 100644 --- a/fotoapparat/src/test/java/io/fotoapparat/hardware/v2/Camera2Test.java +++ b/fotoapparat/src/test/java/io/fotoapparat/hardware/v2/Camera2Test.java @@ -31,6 +31,7 @@ import io.fotoapparat.parameter.LensPosition; import io.fotoapparat.parameter.Parameters; import io.fotoapparat.parameter.RendererParameters; +import io.fotoapparat.parameter.ScaleType; import io.fotoapparat.parameter.Size; import io.fotoapparat.photo.Photo; import io.fotoapparat.preview.PreviewStream; @@ -155,7 +156,7 @@ public void getCapabilities() throws Exception { Capabilities capabilities = new Capabilities( Collections.emptySet(), Collections.emptySet(), - previewScaleTypes, + Collections.emptySet(), singleton(FocusMode.MACRO), Collections.emptySet() ); @@ -226,7 +227,7 @@ public void measureExposure() throws Exception { @Test public void getRendererParameters() throws Exception { // Given - RendererParameters rendererParameters = new RendererParameters(new Size(1920, 1080), 0, scaleType); + RendererParameters rendererParameters = new RendererParameters(new Size(1920, 1080), 0, ScaleType.CENTER_CROP); given(rendererParametersOperator.getRendererParameters()) .willReturn(rendererParameters); diff --git a/fotoapparat/src/test/java/io/fotoapparat/hardware/v2/parameters/RendererParametersProviderTest.java b/fotoapparat/src/test/java/io/fotoapparat/hardware/v2/parameters/RendererParametersProviderTest.java index 799e4f39..dc5e7f0d 100644 --- a/fotoapparat/src/test/java/io/fotoapparat/hardware/v2/parameters/RendererParametersProviderTest.java +++ b/fotoapparat/src/test/java/io/fotoapparat/hardware/v2/parameters/RendererParametersProviderTest.java @@ -8,6 +8,7 @@ import io.fotoapparat.hardware.v2.orientation.OrientationManager; import io.fotoapparat.parameter.RendererParameters; +import io.fotoapparat.parameter.ScaleType; import io.fotoapparat.parameter.Size; import static junit.framework.Assert.assertEquals; @@ -43,7 +44,8 @@ public void getParameters() throws Exception { new RendererParameters( size, sensorOrientation, - scaleType), + ScaleType.CENTER_CROP + ), rendererParameters ); diff --git a/fotoapparat/src/test/java/io/fotoapparat/task/GetCapabilitiesTaskTest.java b/fotoapparat/src/test/java/io/fotoapparat/task/GetCapabilitiesTaskTest.java index f0a4212a..a004d82b 100644 --- a/fotoapparat/src/test/java/io/fotoapparat/task/GetCapabilitiesTaskTest.java +++ b/fotoapparat/src/test/java/io/fotoapparat/task/GetCapabilitiesTaskTest.java @@ -13,6 +13,7 @@ import io.fotoapparat.hardware.Capabilities; import io.fotoapparat.parameter.Flash; import io.fotoapparat.parameter.FocusMode; +import io.fotoapparat.parameter.ScaleType; import io.fotoapparat.parameter.Size; import io.fotoapparat.parameter.provider.GetCapabilitiesTask; @@ -27,7 +28,7 @@ public class GetCapabilitiesTaskTest { private static final Capabilities CAPABILITIES = new Capabilities( Collections.singleton(new Size(1400, 1080)), Collections.singleton(new Size(1400, 1080)), - previewScaleTypes, + Collections.singleton(ScaleType.CENTER_CROP), Collections.singleton(FocusMode.CONTINUOUS_FOCUS), Collections.singleton(Flash.OFF) ); diff --git a/gradle.properties b/gradle.properties index fadf8f22..1c15c7f7 100644 --- a/gradle.properties +++ b/gradle.properties @@ -17,7 +17,7 @@ mockitoVersion=2.2.28 #Gradle Properties -gradleVersion=3.3 +gradleVersion=4.0 org.gradle.daemon=true org.gradle.parallel=true org.gradle.jvmargs=-Xmx1536m diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 961ee8b3..13970809 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-4.0-all.zip From 0b0ded639b0beac667d589ba869079f05db06415 Mon Sep 17 00:00:00 2001 From: Dionysis Date: Sun, 16 Jul 2017 20:29:13 +0200 Subject: [PATCH 05/11] Update readme and example --- README.md | 1 + sample/src/main/java/io/fotoapparat/sample/MainActivity.java | 2 ++ 2 files changed, 3 insertions(+) diff --git a/README.md b/README.md index 445d8ac6..12467d55 100644 --- a/README.md +++ b/README.md @@ -47,6 +47,7 @@ Fotoapparat .with(context) .into(cameraView) // view which will draw the camera preview .photoSize(biggestSize()) // we want to have the biggest photo possible + .previewScaleType(centerCropped()) // we want the preview to fill the view .lensPosition(back()) // we want back camera .focusMode(firstAvailable( // (optional) use the first focus mode which is supported by device continuousFocus(), diff --git a/sample/src/main/java/io/fotoapparat/sample/MainActivity.java b/sample/src/main/java/io/fotoapparat/sample/MainActivity.java index 41065958..3bf5dacc 100644 --- a/sample/src/main/java/io/fotoapparat/sample/MainActivity.java +++ b/sample/src/main/java/io/fotoapparat/sample/MainActivity.java @@ -30,6 +30,7 @@ import static io.fotoapparat.parameter.selector.FocusModeSelectors.continuousFocus; import static io.fotoapparat.parameter.selector.FocusModeSelectors.fixed; import static io.fotoapparat.parameter.selector.LensPositionSelectors.lensPosition; +import static io.fotoapparat.parameter.selector.ScaleTypeSelectors.centerCropped; import static io.fotoapparat.parameter.selector.Selectors.firstAvailable; import static io.fotoapparat.parameter.selector.SizeSelectors.biggestSize; import static io.fotoapparat.result.transformer.SizeTransformers.scaled; @@ -92,6 +93,7 @@ private Fotoapparat createFotoapparat(LensPosition position) { .with(this) .into(cameraView) .photoSize(standardRatio(biggestSize())) + .previewScaleType(centerCropped()) .lensPosition(lensPosition(position)) .focusMode(firstAvailable( continuousFocus(), From f3d67273a24c5405c721afeb6c0a140dd21a7966 Mon Sep 17 00:00:00 2001 From: Dionysis Date: Sun, 16 Jul 2017 20:32:56 +0200 Subject: [PATCH 06/11] Jdoc typos --- .../src/main/java/io/fotoapparat/parameter/ScaleType.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/fotoapparat/src/main/java/io/fotoapparat/parameter/ScaleType.java b/fotoapparat/src/main/java/io/fotoapparat/parameter/ScaleType.java index 9c067f74..c058bbf3 100644 --- a/fotoapparat/src/main/java/io/fotoapparat/parameter/ScaleType.java +++ b/fotoapparat/src/main/java/io/fotoapparat/parameter/ScaleType.java @@ -6,13 +6,13 @@ public enum ScaleType { /** - * The preview will be scaled so as it's one dimensions will be equal and the other equal or + * The preview will be scaled so as its one dimensions will be equal and the other one equal or * larger than the corresponding dimension of the view */ CENTER_CROP, /** - * The preview will be scaled so as it's one dimensions will be equal and the other equal or + * The preview will be scaled so as its one dimensions will be equal and the other one equal or * smaller than the corresponding dimension of the view */ CENTER_INSIDE From 57729b9dd81b41face53dc8f0e45b918e0f7685e Mon Sep 17 00:00:00 2001 From: Dionysis Date: Sun, 16 Jul 2017 21:40:16 +0200 Subject: [PATCH 07/11] Remove traveling param and use it in the start routine --- .../main/java/io/fotoapparat/Fotoapparat.java | 2 +- .../io/fotoapparat/FotoapparatBuilder.java | 9 +- .../io/fotoapparat/hardware/Capabilities.java | 13 -- .../io/fotoapparat/hardware/v1/Camera1.java | 3 +- .../v1/capabilities/CapabilitiesFactory.java | 9 -- .../v2/capabilities/CapabilitiesFactory.java | 10 -- .../RendererParametersProvider.java | 4 +- .../parameter/RendererParameters.java | 16 +- .../provider/InitialParametersProvider.java | 18 --- .../selector/ScaleTypeSelectors.java | 19 ++- .../routine/StartCameraRoutine.java | 5 + .../io/fotoapparat/view/CameraRenderer.java | 7 + .../java/io/fotoapparat/view/CameraView.java | 6 + .../fotoapparat/view/TextureRendererView.java | 20 ++- .../fotoapparat/FotoapparatBuilderTest.java | 10 +- .../fotoapparat/hardware/v2/Camera2Test.java | 4 +- .../RendererParametersProviderTest.java | 4 +- .../InitialParametersProviderTest.java | 25 --- .../selector/ScaleTypeSelectorsTest.java | 21 +-- .../routine/StartCameraRoutineTest.java | 149 ++++++++++-------- .../task/GetCapabilitiesTaskTest.java | 2 - 21 files changed, 138 insertions(+), 218 deletions(-) diff --git a/fotoapparat/src/main/java/io/fotoapparat/Fotoapparat.java b/fotoapparat/src/main/java/io/fotoapparat/Fotoapparat.java index 007dfb3e..96af7ccb 100644 --- a/fotoapparat/src/main/java/io/fotoapparat/Fotoapparat.java +++ b/fotoapparat/src/main/java/io/fotoapparat/Fotoapparat.java @@ -80,7 +80,6 @@ static Fotoapparat create(FotoapparatBuilder builder) { cameraDevice, builder.photoSizeSelector, builder.previewSizeSelector, - builder.previewScaleTypeSelector, builder.focusModeSelector, builder.flashSelector, parametersValidator @@ -89,6 +88,7 @@ static Fotoapparat create(FotoapparatBuilder builder) { StartCameraRoutine startCameraRoutine = new StartCameraRoutine( cameraDevice, builder.renderer, + builder.scaleType, builder.lensPositionSelector, screenOrientationProvider, initialParametersProvider diff --git a/fotoapparat/src/main/java/io/fotoapparat/FotoapparatBuilder.java b/fotoapparat/src/main/java/io/fotoapparat/FotoapparatBuilder.java index 26ad1bcb..e9e0067b 100644 --- a/fotoapparat/src/main/java/io/fotoapparat/FotoapparatBuilder.java +++ b/fotoapparat/src/main/java/io/fotoapparat/FotoapparatBuilder.java @@ -45,7 +45,6 @@ public class FotoapparatBuilder { ); SelectorFunction photoSizeSelector = biggestSize(); SelectorFunction previewSizeSelector = biggestSize(); - SelectorFunction previewScaleTypeSelector = centerCropped(); SelectorFunction focusModeSelector = firstAvailable( continuousFocus(), autoFocus(), @@ -53,6 +52,8 @@ public class FotoapparatBuilder { ); SelectorFunction flashSelector = FlashSelectors.off(); + ScaleType scaleType = centerCropped(); + FrameProcessor frameProcessor = null; Logger logger = Loggers.none(); @@ -86,10 +87,10 @@ public FotoapparatBuilder previewSize(SelectorFunction selector) { } /** - * @param selector selects scale type of preview inside the view. + * @param scaleType of preview inside the view. */ - public FotoapparatBuilder previewScaleType(SelectorFunction selector) { - previewScaleTypeSelector = selector; + public FotoapparatBuilder previewScaleType(ScaleType scaleType) { + this.scaleType = scaleType; return this; } diff --git a/fotoapparat/src/main/java/io/fotoapparat/hardware/Capabilities.java b/fotoapparat/src/main/java/io/fotoapparat/hardware/Capabilities.java index dd13801e..125f305c 100644 --- a/fotoapparat/src/main/java/io/fotoapparat/hardware/Capabilities.java +++ b/fotoapparat/src/main/java/io/fotoapparat/hardware/Capabilities.java @@ -7,7 +7,6 @@ import io.fotoapparat.parameter.Flash; import io.fotoapparat.parameter.FocusMode; -import io.fotoapparat.parameter.ScaleType; import io.fotoapparat.parameter.Size; /** @@ -20,20 +19,16 @@ public class Capabilities { @NonNull private final Set previewSizes; @NonNull - private Set previewScaleTypes; - @NonNull private final Set focusModes; @NonNull private final Set flashModes; public Capabilities(@NonNull Set photoSizes, @NonNull Set previewSizes, - @NonNull Set previewScaleTypes, @NonNull Set focusModes, @NonNull Set flashModes) { this.photoSizes = photoSizes; this.previewSizes = previewSizes; - this.previewScaleTypes = previewScaleTypes; this.focusModes = focusModes; this.flashModes = flashModes; } @@ -45,7 +40,6 @@ public static Capabilities empty() { return new Capabilities( Collections.emptySet(), Collections.emptySet(), - Collections.emptySet(), Collections.emptySet(), Collections.emptySet() ); @@ -65,13 +59,6 @@ public Set supportedPreviewSizes() { return previewSizes; } - /** - * @return list of supported preview scale types; - */ - public Set supportedPreviewScaleTypes() { - return previewScaleTypes; - } - /** * @return list of supported focus modes. */ diff --git a/fotoapparat/src/main/java/io/fotoapparat/hardware/v1/Camera1.java b/fotoapparat/src/main/java/io/fotoapparat/hardware/v1/Camera1.java index 1483ce4e..35829284 100644 --- a/fotoapparat/src/main/java/io/fotoapparat/hardware/v1/Camera1.java +++ b/fotoapparat/src/main/java/io/fotoapparat/hardware/v1/Camera1.java @@ -254,8 +254,7 @@ public RendererParameters getRendererParameters() { return new RendererParameters( previewSize(), - imageRotation, - previewScaleType + imageRotation ); } diff --git a/fotoapparat/src/main/java/io/fotoapparat/hardware/v1/capabilities/CapabilitiesFactory.java b/fotoapparat/src/main/java/io/fotoapparat/hardware/v1/capabilities/CapabilitiesFactory.java index 290f381e..61f04672 100644 --- a/fotoapparat/src/main/java/io/fotoapparat/hardware/v1/capabilities/CapabilitiesFactory.java +++ b/fotoapparat/src/main/java/io/fotoapparat/hardware/v1/capabilities/CapabilitiesFactory.java @@ -13,7 +13,6 @@ import io.fotoapparat.hardware.v1.Camera1; import io.fotoapparat.parameter.Flash; import io.fotoapparat.parameter.FocusMode; -import io.fotoapparat.parameter.ScaleType; import io.fotoapparat.parameter.Size; /** @@ -29,7 +28,6 @@ public Capabilities fromParameters(Camera.Parameters parameters) { return new Capabilities( extractPictureSizes(parameters), extractPreviewSizes(parameters), - setPreviewScaleTypes(), extractFocusModes(parameters), extractFlashModes(parameters) ); @@ -88,11 +86,4 @@ private Set extractFocusModes(Camera.Parameters parameters) { return result; } - private Set setPreviewScaleTypes() { - HashSet previewScaleTypes = new HashSet<>(); - previewScaleTypes.add(ScaleType.CENTER_CROP); - previewScaleTypes.add(ScaleType.CENTER_INSIDE); - return previewScaleTypes; - } - } diff --git a/fotoapparat/src/main/java/io/fotoapparat/hardware/v2/capabilities/CapabilitiesFactory.java b/fotoapparat/src/main/java/io/fotoapparat/hardware/v2/capabilities/CapabilitiesFactory.java index bc0c49a3..277745df 100644 --- a/fotoapparat/src/main/java/io/fotoapparat/hardware/v2/capabilities/CapabilitiesFactory.java +++ b/fotoapparat/src/main/java/io/fotoapparat/hardware/v2/capabilities/CapabilitiesFactory.java @@ -13,11 +13,9 @@ import io.fotoapparat.hardware.v2.parameters.converters.FocusConverter; import io.fotoapparat.parameter.Flash; import io.fotoapparat.parameter.FocusMode; -import io.fotoapparat.parameter.ScaleType; import io.fotoapparat.parameter.Size; import static io.fotoapparat.hardware.v2.parameters.converters.FlashConverter.exposureModeToFlash; -import static java.util.Arrays.asList; /** * Creates the {@link Capabilities} of a {@link io.fotoapparat.hardware.v2.Camera2}. @@ -36,19 +34,11 @@ public Capabilities getCapabilities() { return new Capabilities( availableJpegSizes(), availablePreviewSizes(), - availablePreviewScaleTypes(), availableFocusModes(), availableFlashModes() ); } - private Set availablePreviewScaleTypes() { - return new HashSet<>(asList( - ScaleType.CENTER_INSIDE, - ScaleType.CENTER_CROP - )); - } - @SuppressWarnings("ConstantConditions") private Set availableJpegSizes() { return characteristics().getJpegOutputSizes(); diff --git a/fotoapparat/src/main/java/io/fotoapparat/hardware/v2/parameters/RendererParametersProvider.java b/fotoapparat/src/main/java/io/fotoapparat/hardware/v2/parameters/RendererParametersProvider.java index e2ab9dcb..191f710a 100644 --- a/fotoapparat/src/main/java/io/fotoapparat/hardware/v2/parameters/RendererParametersProvider.java +++ b/fotoapparat/src/main/java/io/fotoapparat/hardware/v2/parameters/RendererParametersProvider.java @@ -3,7 +3,6 @@ import io.fotoapparat.hardware.operators.RendererParametersOperator; import io.fotoapparat.hardware.v2.orientation.OrientationManager; import io.fotoapparat.parameter.RendererParameters; -import io.fotoapparat.parameter.ScaleType; /** * Provides {@link RendererParameters} for camera v2. @@ -24,8 +23,7 @@ public RendererParametersProvider(ParametersProvider parametersProvider, public RendererParameters getRendererParameters() { return new RendererParameters( parametersProvider.getPreviewSize(), - orientationManager.getPhotoOrientation(), - ScaleType.CENTER_CROP + orientationManager.getPhotoOrientation() ); } diff --git a/fotoapparat/src/main/java/io/fotoapparat/parameter/RendererParameters.java b/fotoapparat/src/main/java/io/fotoapparat/parameter/RendererParameters.java index 36f73cd4..0acd1b88 100644 --- a/fotoapparat/src/main/java/io/fotoapparat/parameter/RendererParameters.java +++ b/fotoapparat/src/main/java/io/fotoapparat/parameter/RendererParameters.java @@ -19,18 +19,10 @@ public class RendererParameters { */ public final int frameRotation; - - /** - * The type of the scaling the preview. - */ - public final ScaleType scaleType; - public RendererParameters(Size previewSize, - int frameRotation, - ScaleType scaleType) { + int frameRotation) { this.previewSize = previewSize; this.frameRotation = frameRotation; - this.scaleType = scaleType; } @Override @@ -40,17 +32,13 @@ public boolean equals(Object o) { RendererParameters that = (RendererParameters) o; - if (frameRotation != that.frameRotation) return false; - if (previewSize != null ? !previewSize.equals(that.previewSize) : that.previewSize != null) - return false; - return scaleType == that.scaleType; + return frameRotation == that.frameRotation && (previewSize != null ? previewSize.equals(that.previewSize) : that.previewSize == null); } @Override public int hashCode() { int result = previewSize != null ? previewSize.hashCode() : 0; result = 31 * result + frameRotation; - result = 31 * result + (scaleType != null ? scaleType.hashCode() : 0); return result; } } diff --git a/fotoapparat/src/main/java/io/fotoapparat/parameter/provider/InitialParametersProvider.java b/fotoapparat/src/main/java/io/fotoapparat/parameter/provider/InitialParametersProvider.java index 2d82372d..4314174f 100644 --- a/fotoapparat/src/main/java/io/fotoapparat/parameter/provider/InitialParametersProvider.java +++ b/fotoapparat/src/main/java/io/fotoapparat/parameter/provider/InitialParametersProvider.java @@ -6,7 +6,6 @@ import io.fotoapparat.parameter.Flash; import io.fotoapparat.parameter.FocusMode; import io.fotoapparat.parameter.Parameters; -import io.fotoapparat.parameter.ScaleType; import io.fotoapparat.parameter.Size; import io.fotoapparat.parameter.selector.SelectorFunction; import io.fotoapparat.parameter.selector.Selectors; @@ -22,21 +21,18 @@ public class InitialParametersProvider { private final CapabilitiesOperator capabilitiesOperator; private final SelectorFunction photoSizeSelector; private final SelectorFunction previewSizeSelector; - private final SelectorFunction previewScaleTypeSelector; private final SelectorFunction focusModeSelector; private final SelectorFunction flashSelector; public InitialParametersProvider(CapabilitiesOperator capabilitiesOperator, SelectorFunction photoSizeSelector, SelectorFunction previewSizeSelector, - SelectorFunction previewScaleTypeSelector, SelectorFunction focusModeSelector, SelectorFunction flashSelector, InitialParametersValidator parametersValidator) { this.capabilitiesOperator = capabilitiesOperator; this.photoSizeSelector = photoSizeSelector; this.previewSizeSelector = previewSizeSelector; - this.previewScaleTypeSelector = previewScaleTypeSelector; this.focusModeSelector = focusModeSelector; this.flashSelector = flashSelector; this.parametersValidator = parametersValidator; @@ -52,7 +48,6 @@ public Parameters initialParameters() { putPictureSize(capabilities, parameters); putPreviewSize(capabilities, parameters); - putPreviewScaleType(capabilities, parameters); putFocusMode(capabilities, parameters); putFlash(capabilities, parameters); @@ -76,13 +71,6 @@ private void putPreviewSize(Capabilities capabilities, Parameters parameters) { ); } - private void putPreviewScaleType(Capabilities capabilities, Parameters parameters) { - parameters.putValue( - Parameters.Type.PREVIEW_SCALE_TYPE, - previewScaleType(capabilities) - ); - } - private SelectorFunction previewWithSameAspectRatio(Size photoSize) { return aspectRatio( photoSize.getAspectRatio(), @@ -103,12 +91,6 @@ private Size photoSize(Capabilities capabilities) { ); } - private ScaleType previewScaleType(Capabilities capabilities) { - return previewScaleTypeSelector.select( - capabilities.supportedPreviewScaleTypes() - ); - } - private void putFocusMode(Capabilities capabilities, Parameters parameters) { parameters.putValue( Parameters.Type.FOCUS_MODE, diff --git a/fotoapparat/src/main/java/io/fotoapparat/parameter/selector/ScaleTypeSelectors.java b/fotoapparat/src/main/java/io/fotoapparat/parameter/selector/ScaleTypeSelectors.java index c0f2023b..e7337a73 100644 --- a/fotoapparat/src/main/java/io/fotoapparat/parameter/selector/ScaleTypeSelectors.java +++ b/fotoapparat/src/main/java/io/fotoapparat/parameter/selector/ScaleTypeSelectors.java @@ -8,21 +8,20 @@ public class ScaleTypeSelectors { /** - * @return {@link SelectorFunction} which provides a preview style which will be scaled so as - * it's one dimensions will be equal and the other equal or larger than the corresponding - * dimension of the view. + * @return {@link ScaleType} which will make the preview to be scaled so as its one dimensions + * will be equal and the other one equal or larger than the corresponding dimension of the + * view. */ - public static SelectorFunction centerCropped() { - return Selectors.single(ScaleType.CENTER_CROP); + public static ScaleType centerCropped() { + return ScaleType.CENTER_CROP; } /** - * @return {@link SelectorFunction} which provides a preview style which will be scaled so as - * it's one dimensions will be equal and the other equal or smaller than the corresponding - * dimension of the view. + * @return {@link ScaleType} which will make the preview to so as its one dimensions will be + * equal and the other one equal or smaller than the corresponding dimension of the view */ - public static SelectorFunction centerInside() { - return Selectors.single(ScaleType.CENTER_INSIDE); + public static ScaleType centerInside() { + return ScaleType.CENTER_INSIDE; } } diff --git a/fotoapparat/src/main/java/io/fotoapparat/routine/StartCameraRoutine.java b/fotoapparat/src/main/java/io/fotoapparat/routine/StartCameraRoutine.java index f0a586ab..1b08f8d7 100644 --- a/fotoapparat/src/main/java/io/fotoapparat/routine/StartCameraRoutine.java +++ b/fotoapparat/src/main/java/io/fotoapparat/routine/StartCameraRoutine.java @@ -3,6 +3,7 @@ import io.fotoapparat.hardware.CameraDevice; import io.fotoapparat.hardware.orientation.ScreenOrientationProvider; import io.fotoapparat.parameter.LensPosition; +import io.fotoapparat.parameter.ScaleType; import io.fotoapparat.parameter.provider.InitialParametersProvider; import io.fotoapparat.parameter.selector.SelectorFunction; import io.fotoapparat.view.CameraRenderer; @@ -14,17 +15,20 @@ public class StartCameraRoutine implements Runnable { private final CameraDevice cameraDevice; private final CameraRenderer cameraRenderer; + private final ScaleType scaleType; private final SelectorFunction lensPositionSelector; private final ScreenOrientationProvider screenOrientationProvider; private final InitialParametersProvider initialParametersProvider; public StartCameraRoutine(CameraDevice cameraDevice, CameraRenderer cameraRenderer, + ScaleType scaleType, SelectorFunction lensPositionSelector, ScreenOrientationProvider screenOrientationProvider, InitialParametersProvider initialParametersProvider) { this.cameraDevice = cameraDevice; this.cameraRenderer = cameraRenderer; + this.scaleType = scaleType; this.lensPositionSelector = lensPositionSelector; this.screenOrientationProvider = screenOrientationProvider; this.initialParametersProvider = initialParametersProvider; @@ -43,6 +47,7 @@ public void run() { cameraDevice.setDisplayOrientation( screenOrientationProvider.getScreenRotation() ); + cameraRenderer.setScaleType(scaleType); cameraRenderer.attachCamera(cameraDevice); cameraDevice.startPreview(); } diff --git a/fotoapparat/src/main/java/io/fotoapparat/view/CameraRenderer.java b/fotoapparat/src/main/java/io/fotoapparat/view/CameraRenderer.java index b9dcb40c..5d8052f6 100644 --- a/fotoapparat/src/main/java/io/fotoapparat/view/CameraRenderer.java +++ b/fotoapparat/src/main/java/io/fotoapparat/view/CameraRenderer.java @@ -1,12 +1,19 @@ package io.fotoapparat.view; import io.fotoapparat.hardware.CameraDevice; +import io.fotoapparat.parameter.ScaleType; /** * Renders the stream from {@link io.fotoapparat.Fotoapparat}. */ public interface CameraRenderer { + /** + * Sets the scale type of the preview to the renderer. This method will be called from camera + * thread, so it is safe to perform blocking operations here. + */ + void setScaleType(ScaleType scaleType); + /** * Attaches renderer to camera, so that it will display the preview when camera is started. This * method will be called from camera thread, so it is safe to perform blocking operations here. diff --git a/fotoapparat/src/main/java/io/fotoapparat/view/CameraView.java b/fotoapparat/src/main/java/io/fotoapparat/view/CameraView.java index d08abf8e..464b6c36 100644 --- a/fotoapparat/src/main/java/io/fotoapparat/view/CameraView.java +++ b/fotoapparat/src/main/java/io/fotoapparat/view/CameraView.java @@ -11,6 +11,7 @@ import android.widget.FrameLayout; import io.fotoapparat.hardware.CameraDevice; +import io.fotoapparat.parameter.ScaleType; /** * Displays stream from camera. @@ -55,6 +56,11 @@ private void init() { addView(rendererView); } + @Override + public void setScaleType(ScaleType scaleType) { + rendererView.setScaleType(scaleType); + } + @Override public void attachCamera(CameraDevice camera) { rendererView.attachCamera(camera); diff --git a/fotoapparat/src/main/java/io/fotoapparat/view/TextureRendererView.java b/fotoapparat/src/main/java/io/fotoapparat/view/TextureRendererView.java index a3a69c03..42d6ab74 100644 --- a/fotoapparat/src/main/java/io/fotoapparat/view/TextureRendererView.java +++ b/fotoapparat/src/main/java/io/fotoapparat/view/TextureRendererView.java @@ -29,7 +29,8 @@ class TextureRendererView extends FrameLayout implements CameraRenderer { private SurfaceTexture surfaceTexture; private TextureView textureView; - private RendererParameters rendererParameters = null; + private Size previewSize = null; + private ScaleType scaleType; public TextureRendererView(@NonNull Context context) { super(context); @@ -75,6 +76,11 @@ private void tryToInitializeSurfaceTexture(TextureView textureView) { } } + @Override + public void setScaleType(ScaleType scaleType) { + this.scaleType = scaleType; + } + @Override public void attachCamera(CameraDevice camera) { awaitSurfaceTexture(); @@ -84,10 +90,14 @@ public void attachCamera(CameraDevice camera) { } private void updateLayout(final CameraDevice camera) { + final Size previewSize = toPreviewSize( + camera.getRendererParameters() + ); + post(new Runnable() { @Override public void run() { - rendererParameters = camera.getRendererParameters(); + TextureRendererView.this.previewSize = previewSize; requestLayout(); } @@ -114,14 +124,12 @@ private void awaitSurfaceTexture() { @Override protected void onLayout(boolean changed, int left, int top, int right, int bottom) { - if (rendererParameters == null) { + if (previewSize == null || scaleType == null) { super.onLayout(changed, left, top, right, bottom); return; } - Size previewSize = toPreviewSize(rendererParameters); - - if (rendererParameters.scaleType == ScaleType.CENTER_INSIDE) { + if (scaleType == ScaleType.CENTER_INSIDE) { centerInside(previewSize); } else { centerCrop(previewSize); diff --git a/fotoapparat/src/test/java/io/fotoapparat/FotoapparatBuilderTest.java b/fotoapparat/src/test/java/io/fotoapparat/FotoapparatBuilderTest.java index 735366fc..bd3be97d 100644 --- a/fotoapparat/src/test/java/io/fotoapparat/FotoapparatBuilderTest.java +++ b/fotoapparat/src/test/java/io/fotoapparat/FotoapparatBuilderTest.java @@ -41,8 +41,6 @@ public class FotoapparatBuilderTest { @Mock SelectorFunction previewSizeSelector; @Mock - SelectorFunction previewScaleTypeSelector; - @Mock SelectorFunction lensPositionSelector; @Mock SelectorFunction focusModeSelector; @@ -215,19 +213,19 @@ public void previewStyle_HasDefault() throws Exception { FotoapparatBuilder builder = builderWithMandatoryArguments(); // Then - assertNotNull(builder.previewScaleTypeSelector); + assertNotNull(builder.scaleType); } @Test public void previewStyle_IsConfigurable() throws Exception { // When FotoapparatBuilder builder = builderWithMandatoryArguments() - .previewScaleType(previewScaleTypeSelector); + .previewScaleType(ScaleType.CENTER_INSIDE); // Then assertEquals( - previewScaleTypeSelector, - builder.previewScaleTypeSelector + ScaleType.CENTER_INSIDE, + builder.scaleType ); } diff --git a/fotoapparat/src/test/java/io/fotoapparat/hardware/v2/Camera2Test.java b/fotoapparat/src/test/java/io/fotoapparat/hardware/v2/Camera2Test.java index 51e6db95..c956d0cb 100644 --- a/fotoapparat/src/test/java/io/fotoapparat/hardware/v2/Camera2Test.java +++ b/fotoapparat/src/test/java/io/fotoapparat/hardware/v2/Camera2Test.java @@ -31,7 +31,6 @@ import io.fotoapparat.parameter.LensPosition; import io.fotoapparat.parameter.Parameters; import io.fotoapparat.parameter.RendererParameters; -import io.fotoapparat.parameter.ScaleType; import io.fotoapparat.parameter.Size; import io.fotoapparat.photo.Photo; import io.fotoapparat.preview.PreviewStream; @@ -156,7 +155,6 @@ public void getCapabilities() throws Exception { Capabilities capabilities = new Capabilities( Collections.emptySet(), Collections.emptySet(), - Collections.emptySet(), singleton(FocusMode.MACRO), Collections.emptySet() ); @@ -227,7 +225,7 @@ public void measureExposure() throws Exception { @Test public void getRendererParameters() throws Exception { // Given - RendererParameters rendererParameters = new RendererParameters(new Size(1920, 1080), 0, ScaleType.CENTER_CROP); + RendererParameters rendererParameters = new RendererParameters(new Size(1920, 1080), 0); given(rendererParametersOperator.getRendererParameters()) .willReturn(rendererParameters); diff --git a/fotoapparat/src/test/java/io/fotoapparat/hardware/v2/parameters/RendererParametersProviderTest.java b/fotoapparat/src/test/java/io/fotoapparat/hardware/v2/parameters/RendererParametersProviderTest.java index dc5e7f0d..d6c3e5ac 100644 --- a/fotoapparat/src/test/java/io/fotoapparat/hardware/v2/parameters/RendererParametersProviderTest.java +++ b/fotoapparat/src/test/java/io/fotoapparat/hardware/v2/parameters/RendererParametersProviderTest.java @@ -8,7 +8,6 @@ import io.fotoapparat.hardware.v2.orientation.OrientationManager; import io.fotoapparat.parameter.RendererParameters; -import io.fotoapparat.parameter.ScaleType; import io.fotoapparat.parameter.Size; import static junit.framework.Assert.assertEquals; @@ -43,8 +42,7 @@ public void getParameters() throws Exception { assertEquals( new RendererParameters( size, - sensorOrientation, - ScaleType.CENTER_CROP + sensorOrientation ), rendererParameters ); diff --git a/fotoapparat/src/test/java/io/fotoapparat/parameter/provider/InitialParametersProviderTest.java b/fotoapparat/src/test/java/io/fotoapparat/parameter/provider/InitialParametersProviderTest.java index 4c81d69e..1b8ca292 100644 --- a/fotoapparat/src/test/java/io/fotoapparat/parameter/provider/InitialParametersProviderTest.java +++ b/fotoapparat/src/test/java/io/fotoapparat/parameter/provider/InitialParametersProviderTest.java @@ -13,7 +13,6 @@ import io.fotoapparat.parameter.Flash; import io.fotoapparat.parameter.FocusMode; import io.fotoapparat.parameter.Parameters; -import io.fotoapparat.parameter.ScaleType; import io.fotoapparat.parameter.Size; import io.fotoapparat.parameter.selector.SelectorFunction; @@ -31,11 +30,6 @@ public class InitialParametersProviderTest { static final Size PREVIEW_SIZE = new Size(2000, 1500); static final Size PREVIEW_SIZE_WRONG_ASPECT_RATIO = new Size(1000, 1000); - static final Set ALL_PREVIEW_SCALE_TYPE = asSet( - ScaleType.CENTER_CROP, - ScaleType.CENTER_INSIDE - ); - static final Set FOCUS_MODES = asSet(FocusMode.FIXED); static final Set FLASH = asSet(Flash.AUTO_RED_EYE); @@ -53,8 +47,6 @@ public class InitialParametersProviderTest { @Mock SelectorFunction previewSizeSelector; @Mock - SelectorFunction previewScaleTypeSelector; - @Mock SelectorFunction focusModeSelector; @Mock SelectorFunction flashModeSelector; @@ -69,7 +61,6 @@ public void setUp() throws Exception { cameraDevice, photoSizeSelector, previewSizeSelector, - previewScaleTypeSelector, focusModeSelector, flashModeSelector, initialParametersValidator @@ -79,7 +70,6 @@ public void setUp() throws Exception { .willReturn(new Capabilities( PHOTO_SIZES, ALL_PREVIEW_SIZES, - ALL_PREVIEW_SCALE_TYPE, FOCUS_MODES, FLASH )); @@ -88,8 +78,6 @@ public void setUp() throws Exception { .willReturn(PHOTO_SIZE); given(previewSizeSelector.select(VALID_PREVIEW_SIZES)) .willReturn(PREVIEW_SIZE); - given(previewScaleTypeSelector.select(ALL_PREVIEW_SCALE_TYPE)) - .willReturn(ScaleType.CENTER_CROP); given(focusModeSelector.select(FOCUS_MODES)) .willReturn(FocusMode.FIXED); given(flashModeSelector.select(FLASH)) @@ -132,18 +120,6 @@ public void selectPhotoSize() throws Exception { ); } - @Test - public void selectPreviewScaleType() throws Exception { - // When - Parameters parameters = testee.initialParameters(); - - // Then - assertEquals( - ScaleType.CENTER_CROP, - parameters.getValue(Parameters.Type.PREVIEW_SCALE_TYPE) - ); - } - @Test public void selectPreviewSize_WithValidAspectRatio() throws Exception { // When @@ -171,7 +147,6 @@ public void selectPreviewSize_SameAspectRatioNotAvailable() throws Exception { .willReturn(new Capabilities( photoSizes, ALL_PREVIEW_SIZES, - ALL_PREVIEW_SCALE_TYPE, FOCUS_MODES, FLASH )); diff --git a/fotoapparat/src/test/java/io/fotoapparat/parameter/selector/ScaleTypeSelectorsTest.java b/fotoapparat/src/test/java/io/fotoapparat/parameter/selector/ScaleTypeSelectorsTest.java index e881a264..62949d63 100644 --- a/fotoapparat/src/test/java/io/fotoapparat/parameter/selector/ScaleTypeSelectorsTest.java +++ b/fotoapparat/src/test/java/io/fotoapparat/parameter/selector/ScaleTypeSelectorsTest.java @@ -4,11 +4,8 @@ import org.junit.runner.RunWith; import org.mockito.junit.MockitoJUnitRunner; -import java.util.List; - import io.fotoapparat.parameter.ScaleType; -import static java.util.Arrays.asList; import static junit.framework.Assert.assertEquals; @RunWith(MockitoJUnitRunner.class) @@ -17,16 +14,9 @@ public class ScaleTypeSelectorsTest { @Test public void select_crop() throws Exception { - // Given - List availableScaleTypes = asList( - ScaleType.CENTER_CROP, - ScaleType.CENTER_INSIDE - ); - // When ScaleType result = ScaleTypeSelectors - .centerCropped() - .select(availableScaleTypes); + .centerCropped(); // Then assertEquals( @@ -37,16 +27,9 @@ public void select_crop() throws Exception { @Test public void select_centerInside() throws Exception { - // Given - List availableScaleTypes = asList( - ScaleType.CENTER_CROP, - ScaleType.CENTER_INSIDE - ); - // When ScaleType result = ScaleTypeSelectors - .centerInside() - .select(availableScaleTypes); + .centerInside(); // Then assertEquals( diff --git a/fotoapparat/src/test/java/io/fotoapparat/routine/StartCameraRoutineTest.java b/fotoapparat/src/test/java/io/fotoapparat/routine/StartCameraRoutineTest.java index 07cf56cf..d9aeb1fe 100644 --- a/fotoapparat/src/test/java/io/fotoapparat/routine/StartCameraRoutineTest.java +++ b/fotoapparat/src/test/java/io/fotoapparat/routine/StartCameraRoutineTest.java @@ -4,7 +4,6 @@ import org.junit.runner.RunWith; import org.mockito.ArgumentMatchers; import org.mockito.InOrder; -import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.junit.MockitoJUnitRunner; @@ -14,6 +13,7 @@ import io.fotoapparat.hardware.orientation.ScreenOrientationProvider; import io.fotoapparat.parameter.LensPosition; import io.fotoapparat.parameter.Parameters; +import io.fotoapparat.parameter.ScaleType; import io.fotoapparat.parameter.provider.InitialParametersProvider; import io.fotoapparat.parameter.selector.SelectorFunction; import io.fotoapparat.view.CameraRenderer; @@ -25,74 +25,83 @@ @RunWith(MockitoJUnitRunner.class) public class StartCameraRoutineTest { - static final int SCREEN_ROTATION_DEGREES = 90; - static final Parameters INITIAL_PARAMETERS = new Parameters(); - - @Mock - CameraDevice cameraDevice; - @Mock - CameraRenderer cameraRenderer; - @Mock - SelectorFunction lensPositionSelector; - @Mock - ScreenOrientationProvider screenOrientationProvider; - @Mock - InitialParametersProvider initialParametersProvider; - - @InjectMocks - StartCameraRoutine testee; - - @Test - public void routine() throws Exception { - // Given - List availableLensPositions = asList( - LensPosition.FRONT, - LensPosition.BACK - ); - - LensPosition preferredLensPosition = LensPosition.FRONT; - - givenLensPositionsAvailable(availableLensPositions); - givenPositionSelected(preferredLensPosition); - givenScreenRotation(); - givenInitialParametersAvailable(); - - // When - testee.run(); - - // Then - InOrder inOrder = inOrder( - cameraDevice, - cameraRenderer, - lensPositionSelector - ); - - inOrder.verify(lensPositionSelector).select(availableLensPositions); - inOrder.verify(cameraDevice).open(preferredLensPosition); - inOrder.verify(cameraDevice).updateParameters(INITIAL_PARAMETERS); - inOrder.verify(cameraDevice).setDisplayOrientation(SCREEN_ROTATION_DEGREES); - inOrder.verify(cameraRenderer).attachCamera(cameraDevice); - inOrder.verify(cameraDevice).startPreview(); - } - - private void givenInitialParametersAvailable() { - given(initialParametersProvider.initialParameters()) - .willReturn(INITIAL_PARAMETERS); - } - - private void givenScreenRotation() { - given(screenOrientationProvider.getScreenRotation()) - .willReturn(SCREEN_ROTATION_DEGREES); - } - - private void givenPositionSelected(LensPosition lensPosition) { - given(lensPositionSelector.select(ArgumentMatchers.anyCollection())) - .willReturn(lensPosition); - } - - private void givenLensPositionsAvailable(List lensPositions) { - given(cameraDevice.getAvailableLensPositions()) - .willReturn(lensPositions); - } + static final int SCREEN_ROTATION_DEGREES = 90; + static final Parameters INITIAL_PARAMETERS = new Parameters(); + + @Mock + CameraDevice cameraDevice; + @Mock + CameraRenderer cameraRenderer; + @Mock + SelectorFunction lensPositionSelector; + @Mock + ScreenOrientationProvider screenOrientationProvider; + @Mock + InitialParametersProvider initialParametersProvider; + + @Test + public void routine() throws Exception { + // Given + StartCameraRoutine testee = new StartCameraRoutine( + cameraDevice, + cameraRenderer, + ScaleType.CENTER_INSIDE, + lensPositionSelector, + screenOrientationProvider, + initialParametersProvider + ); + + + List availableLensPositions = asList( + LensPosition.FRONT, + LensPosition.BACK + ); + + LensPosition preferredLensPosition = LensPosition.FRONT; + ScaleType scaleType = ScaleType.CENTER_INSIDE; + + givenLensPositionsAvailable(availableLensPositions); + givenPositionSelected(preferredLensPosition); + givenScreenRotation(); + givenInitialParametersAvailable(); + + // When + testee.run(); + + // Then + InOrder inOrder = inOrder( + cameraDevice, + cameraRenderer, + lensPositionSelector + ); + + inOrder.verify(lensPositionSelector).select(availableLensPositions); + inOrder.verify(cameraDevice).open(preferredLensPosition); + inOrder.verify(cameraDevice).updateParameters(INITIAL_PARAMETERS); + inOrder.verify(cameraDevice).setDisplayOrientation(SCREEN_ROTATION_DEGREES); + inOrder.verify(cameraRenderer).setScaleType(scaleType); + inOrder.verify(cameraRenderer).attachCamera(cameraDevice); + inOrder.verify(cameraDevice).startPreview(); + } + + private void givenInitialParametersAvailable() { + given(initialParametersProvider.initialParameters()) + .willReturn(INITIAL_PARAMETERS); + } + + private void givenScreenRotation() { + given(screenOrientationProvider.getScreenRotation()) + .willReturn(SCREEN_ROTATION_DEGREES); + } + + private void givenPositionSelected(LensPosition lensPosition) { + given(lensPositionSelector.select(ArgumentMatchers.anyCollection())) + .willReturn(lensPosition); + } + + private void givenLensPositionsAvailable(List lensPositions) { + given(cameraDevice.getAvailableLensPositions()) + .willReturn(lensPositions); + } } \ No newline at end of file diff --git a/fotoapparat/src/test/java/io/fotoapparat/task/GetCapabilitiesTaskTest.java b/fotoapparat/src/test/java/io/fotoapparat/task/GetCapabilitiesTaskTest.java index a004d82b..87a0f963 100644 --- a/fotoapparat/src/test/java/io/fotoapparat/task/GetCapabilitiesTaskTest.java +++ b/fotoapparat/src/test/java/io/fotoapparat/task/GetCapabilitiesTaskTest.java @@ -13,7 +13,6 @@ import io.fotoapparat.hardware.Capabilities; import io.fotoapparat.parameter.Flash; import io.fotoapparat.parameter.FocusMode; -import io.fotoapparat.parameter.ScaleType; import io.fotoapparat.parameter.Size; import io.fotoapparat.parameter.provider.GetCapabilitiesTask; @@ -28,7 +27,6 @@ public class GetCapabilitiesTaskTest { private static final Capabilities CAPABILITIES = new Capabilities( Collections.singleton(new Size(1400, 1080)), Collections.singleton(new Size(1400, 1080)), - Collections.singleton(ScaleType.CENTER_CROP), Collections.singleton(FocusMode.CONTINUOUS_FOCUS), Collections.singleton(Flash.OFF) ); From 0bf4a3df8ffe3f42993a0bc7ece2a1352e5ab750 Mon Sep 17 00:00:00 2001 From: Dionysis Date: Sun, 16 Jul 2017 21:45:02 +0200 Subject: [PATCH 08/11] Remove redundant code --- .../src/main/java/io/fotoapparat/hardware/v1/Camera1.java | 5 ----- .../src/main/java/io/fotoapparat/parameter/Parameters.java | 5 ----- .../parameter/provider/InitialParametersProvider.java | 1 - 3 files changed, 11 deletions(-) diff --git a/fotoapparat/src/main/java/io/fotoapparat/hardware/v1/Camera1.java b/fotoapparat/src/main/java/io/fotoapparat/hardware/v1/Camera1.java index 35829284..1a3e1205 100644 --- a/fotoapparat/src/main/java/io/fotoapparat/hardware/v1/Camera1.java +++ b/fotoapparat/src/main/java/io/fotoapparat/hardware/v1/Camera1.java @@ -21,9 +21,7 @@ import io.fotoapparat.log.Logger; import io.fotoapparat.parameter.LensPosition; import io.fotoapparat.parameter.Parameters; -import io.fotoapparat.parameter.Parameters.Type; import io.fotoapparat.parameter.RendererParameters; -import io.fotoapparat.parameter.ScaleType; import io.fotoapparat.parameter.Size; import io.fotoapparat.photo.Photo; import io.fotoapparat.preview.PreviewStream; @@ -45,7 +43,6 @@ public class Camera1 implements CameraDevice { private Throwable lastStacktrace; private int imageRotation; - private ScaleType previewScaleType; public Camera1(Logger logger) { this.capabilitiesFactory = new CapabilitiesFactory(); @@ -179,8 +176,6 @@ public void updateParameters(Parameters parameters) { ); camera.setParameters(cameraParameters); - - previewScaleType = parameters.getValue(Type.PREVIEW_SCALE_TYPE); } @Override diff --git a/fotoapparat/src/main/java/io/fotoapparat/parameter/Parameters.java b/fotoapparat/src/main/java/io/fotoapparat/parameter/Parameters.java index 4eb3908a..661c6562 100644 --- a/fotoapparat/src/main/java/io/fotoapparat/parameter/Parameters.java +++ b/fotoapparat/src/main/java/io/fotoapparat/parameter/Parameters.java @@ -79,11 +79,6 @@ public enum Type { */ PREVIEW_SIZE(Size.class), - /** - * Scale type of the preview stream frames. Expected type: {@link ScaleType}. - */ - PREVIEW_SCALE_TYPE(ScaleType.class), - /** * Focus mode of the camera. Expected type: {@link FocusMode}. */ diff --git a/fotoapparat/src/main/java/io/fotoapparat/parameter/provider/InitialParametersProvider.java b/fotoapparat/src/main/java/io/fotoapparat/parameter/provider/InitialParametersProvider.java index 4314174f..0338b8af 100644 --- a/fotoapparat/src/main/java/io/fotoapparat/parameter/provider/InitialParametersProvider.java +++ b/fotoapparat/src/main/java/io/fotoapparat/parameter/provider/InitialParametersProvider.java @@ -56,7 +56,6 @@ public Parameters initialParameters() { return parameters; } - private void putPreviewSize(Capabilities capabilities, Parameters parameters) { Size photoSize = photoSize(capabilities); From 58e5e9338280361bef6f1c4fb68173a583118faa Mon Sep 17 00:00:00 2001 From: Dionysis Date: Sun, 16 Jul 2017 21:53:07 +0200 Subject: [PATCH 09/11] Merge branch 'develop' into feature/preview_scaletype # Conflicts: # fotoapparat/src/main/java/io/fotoapparat/hardware/v1/capabilities/CapabilitiesFactory.java # fotoapparat/src/main/java/io/fotoapparat/routine/StartCameraRoutine.java # fotoapparat/src/test/java/io/fotoapparat/FotoapparatBuilderTest.java # fotoapparat/src/test/java/io/fotoapparat/routine/StartCameraRoutineTest.java --- .../routine/StartCameraRoutine.java | 61 ++++++++++--------- .../routine/StartCameraRoutineTest.java | 52 +++++++++++----- 2 files changed, 69 insertions(+), 44 deletions(-) diff --git a/fotoapparat/src/main/java/io/fotoapparat/routine/StartCameraRoutine.java b/fotoapparat/src/main/java/io/fotoapparat/routine/StartCameraRoutine.java index c987aee0..94d12dce 100644 --- a/fotoapparat/src/main/java/io/fotoapparat/routine/StartCameraRoutine.java +++ b/fotoapparat/src/main/java/io/fotoapparat/routine/StartCameraRoutine.java @@ -23,40 +23,45 @@ public class StartCameraRoutine implements Runnable { private final InitialParametersProvider initialParametersProvider; private final CameraErrorCallback cameraErrorCallback; - public StartCameraRoutine(CameraDevice cameraDevice, - CameraRenderer cameraRenderer, - ScaleType scaleType, SelectorFunction lensPositionSelector, - ScreenOrientationProvider screenOrientationProvider, - InitialParametersProvider initialParametersProvider, - CameraErrorCallback cameraErrorCallback) {this.cameraDevice = cameraDevice; - this.cameraRenderer = cameraRenderer;this.scaleType = scaleType; - this.lensPositionSelector = lensPositionSelector; - this.screenOrientationProvider = screenOrientationProvider; - this.initialParametersProvider = initialParametersProvider; - this.cameraErrorCallback = cameraErrorCallback;} + public StartCameraRoutine(CameraDevice cameraDevice, + CameraRenderer cameraRenderer, + ScaleType scaleType, + SelectorFunction lensPositionSelector, + ScreenOrientationProvider screenOrientationProvider, + InitialParametersProvider initialParametersProvider, + CameraErrorCallback cameraErrorCallback) { + this.cameraDevice = cameraDevice; + this.cameraRenderer = cameraRenderer; + this.scaleType = scaleType; + this.lensPositionSelector = lensPositionSelector; + this.screenOrientationProvider = screenOrientationProvider; + this.initialParametersProvider = initialParametersProvider; + this.cameraErrorCallback = cameraErrorCallback; + } - @Override - public void run() { - try { + @Override + public void run() { + try { tryToStartCamera(); } catch (CameraException e) { cameraErrorCallback.onError(e); } } - private void tryToStartCamera() {LensPosition lensPosition = lensPositionSelector.select( - cameraDevice.getAvailableLensPositions() - ); + private void tryToStartCamera() { + LensPosition lensPosition = lensPositionSelector.select( + cameraDevice.getAvailableLensPositions() + ); - cameraDevice.open(lensPosition); - cameraDevice.updateParameters( - initialParametersProvider.initialParameters() - ); - cameraDevice.setDisplayOrientation( - screenOrientationProvider.getScreenRotation() - ); - cameraRenderer.setScaleType(scaleType); - cameraRenderer.attachCamera(cameraDevice); - cameraDevice.startPreview(); - } + cameraDevice.open(lensPosition); + cameraDevice.updateParameters( + initialParametersProvider.initialParameters() + ); + cameraDevice.setDisplayOrientation( + screenOrientationProvider.getScreenRotation() + ); + cameraRenderer.setScaleType(scaleType); + cameraRenderer.attachCamera(cameraDevice); + cameraDevice.startPreview(); + } } diff --git a/fotoapparat/src/test/java/io/fotoapparat/routine/StartCameraRoutineTest.java b/fotoapparat/src/test/java/io/fotoapparat/routine/StartCameraRoutineTest.java index 0eeb524b..794c6fda 100644 --- a/fotoapparat/src/test/java/io/fotoapparat/routine/StartCameraRoutineTest.java +++ b/fotoapparat/src/test/java/io/fotoapparat/routine/StartCameraRoutineTest.java @@ -1,5 +1,6 @@ package io.fotoapparat.routine; +import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.ArgumentMatchers; @@ -50,20 +51,37 @@ public class StartCameraRoutineTest { @Mock CameraErrorCallback cameraErrorCallback; - @Test - public void routine() throws Exception { - // GivenStartCameraRoutine testee = new StartCameraRoutine( + StartCameraRoutine testee; + + @Before + public void setUp() throws Exception { + testee = new StartCameraRoutine( + cameraDevice, + cameraRenderer, + ScaleType.CENTER_INSIDE, + lensPositionSelector, + screenOrientationProvider, + initialParametersProvider, + cameraErrorCallback + ); + } + + @Test + public void routine() throws Exception { + // Given + StartCameraRoutine testee = new StartCameraRoutine( cameraDevice, cameraRenderer, ScaleType.CENTER_INSIDE, lensPositionSelector, screenOrientationProvider, - initialParametersProvider + initialParametersProvider, + cameraErrorCallback + ); + List availableLensPositions = asList( + LensPosition.FRONT, + LensPosition.BACK ); - List availableLensPositions = asList( - LensPosition.FRONT, - LensPosition.BACK - ); LensPosition preferredLensPosition = LensPosition.FRONT; ScaleType scaleType = ScaleType.CENTER_INSIDE; @@ -83,13 +101,14 @@ public void routine() throws Exception { lensPositionSelector ); - inOrder.verify(lensPositionSelector).select(availableLensPositions); - inOrder.verify(cameraDevice).open(preferredLensPosition); - inOrder.verify(cameraDevice).updateParameters(INITIAL_PARAMETERS); - inOrder.verify(cameraDevice).setDisplayOrientation(SCREEN_ROTATION_DEGREES); - inOrder.verify(cameraRenderer).setScaleType(scaleType);inOrder.verify(cameraRenderer).attachCamera(cameraDevice); - inOrder.verify(cameraDevice).startPreview(); - verifyZeroInteractions(cameraErrorCallback); + inOrder.verify(lensPositionSelector).select(availableLensPositions); + inOrder.verify(cameraDevice).open(preferredLensPosition); + inOrder.verify(cameraDevice).updateParameters(INITIAL_PARAMETERS); + inOrder.verify(cameraDevice).setDisplayOrientation(SCREEN_ROTATION_DEGREES); + inOrder.verify(cameraRenderer).setScaleType(scaleType); + inOrder.verify(cameraRenderer).attachCamera(cameraDevice); + inOrder.verify(cameraDevice).startPreview(); + verifyZeroInteractions(cameraErrorCallback); } @Test @@ -117,7 +136,8 @@ public void failedToOpenCamera() throws Exception { verify(cameraDevice).getAvailableLensPositions(); verify(cameraDevice).open(preferredLensPosition); - verifyNoMoreInteractions(cameraDevice);} + verifyNoMoreInteractions(cameraDevice); + } private void givenInitialParametersAvailable() { given(initialParametersProvider.initialParameters()) From eb0746a78011ec47dab9a84c1cfce7a432ea02e7 Mon Sep 17 00:00:00 2001 From: Dionysis Date: Sun, 16 Jul 2017 21:54:38 +0200 Subject: [PATCH 10/11] Format --- .../hardware/v1/capabilities/CapabilitiesFactory.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/fotoapparat/src/main/java/io/fotoapparat/hardware/v1/capabilities/CapabilitiesFactory.java b/fotoapparat/src/main/java/io/fotoapparat/hardware/v1/capabilities/CapabilitiesFactory.java index b44ad2ed..6984a454 100644 --- a/fotoapparat/src/main/java/io/fotoapparat/hardware/v1/capabilities/CapabilitiesFactory.java +++ b/fotoapparat/src/main/java/io/fotoapparat/hardware/v1/capabilities/CapabilitiesFactory.java @@ -83,7 +83,8 @@ private Set extractFocusModes(Camera.Parameters parameters) { ); } - result.add(FocusMode.FIXED);return result; - } + result.add(FocusMode.FIXED); + return result; + } } From 358bd1b7f372f5257c8c7c631606861b2d03e183 Mon Sep 17 00:00:00 2001 From: Dionysis Date: Sun, 16 Jul 2017 22:00:43 +0200 Subject: [PATCH 11/11] Remove ScaleTypeSelectors --- .../io/fotoapparat/FotoapparatBuilder.java | 3 +- .../selector/ScaleTypeSelectors.java | 27 ------------- .../selector/ScaleTypeSelectorsTest.java | 40 ------------------- .../io/fotoapparat/sample/MainActivity.java | 4 +- 4 files changed, 3 insertions(+), 71 deletions(-) delete mode 100644 fotoapparat/src/main/java/io/fotoapparat/parameter/selector/ScaleTypeSelectors.java delete mode 100644 fotoapparat/src/test/java/io/fotoapparat/parameter/selector/ScaleTypeSelectorsTest.java diff --git a/fotoapparat/src/main/java/io/fotoapparat/FotoapparatBuilder.java b/fotoapparat/src/main/java/io/fotoapparat/FotoapparatBuilder.java index 8126779f..fdfdd851 100644 --- a/fotoapparat/src/main/java/io/fotoapparat/FotoapparatBuilder.java +++ b/fotoapparat/src/main/java/io/fotoapparat/FotoapparatBuilder.java @@ -26,7 +26,6 @@ import static io.fotoapparat.parameter.selector.LensPositionSelectors.back; import static io.fotoapparat.parameter.selector.LensPositionSelectors.external; import static io.fotoapparat.parameter.selector.LensPositionSelectors.front; -import static io.fotoapparat.parameter.selector.ScaleTypeSelectors.centerCropped; import static io.fotoapparat.parameter.selector.Selectors.firstAvailable; import static io.fotoapparat.parameter.selector.SizeSelectors.biggestSize; @@ -53,7 +52,7 @@ public class FotoapparatBuilder { ); SelectorFunction flashSelector = FlashSelectors.off(); - ScaleType scaleType = centerCropped(); + ScaleType scaleType = ScaleType.CENTER_CROP; FrameProcessor frameProcessor = null; diff --git a/fotoapparat/src/main/java/io/fotoapparat/parameter/selector/ScaleTypeSelectors.java b/fotoapparat/src/main/java/io/fotoapparat/parameter/selector/ScaleTypeSelectors.java deleted file mode 100644 index e7337a73..00000000 --- a/fotoapparat/src/main/java/io/fotoapparat/parameter/selector/ScaleTypeSelectors.java +++ /dev/null @@ -1,27 +0,0 @@ -package io.fotoapparat.parameter.selector; - -import io.fotoapparat.parameter.ScaleType; - -/** - * Selector functions for {@link ScaleType}. - */ -public class ScaleTypeSelectors { - - /** - * @return {@link ScaleType} which will make the preview to be scaled so as its one dimensions - * will be equal and the other one equal or larger than the corresponding dimension of the - * view. - */ - public static ScaleType centerCropped() { - return ScaleType.CENTER_CROP; - } - - /** - * @return {@link ScaleType} which will make the preview to so as its one dimensions will be - * equal and the other one equal or smaller than the corresponding dimension of the view - */ - public static ScaleType centerInside() { - return ScaleType.CENTER_INSIDE; - } - -} diff --git a/fotoapparat/src/test/java/io/fotoapparat/parameter/selector/ScaleTypeSelectorsTest.java b/fotoapparat/src/test/java/io/fotoapparat/parameter/selector/ScaleTypeSelectorsTest.java deleted file mode 100644 index 62949d63..00000000 --- a/fotoapparat/src/test/java/io/fotoapparat/parameter/selector/ScaleTypeSelectorsTest.java +++ /dev/null @@ -1,40 +0,0 @@ -package io.fotoapparat.parameter.selector; - -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.junit.MockitoJUnitRunner; - -import io.fotoapparat.parameter.ScaleType; - -import static junit.framework.Assert.assertEquals; - -@RunWith(MockitoJUnitRunner.class) -public class ScaleTypeSelectorsTest { - - - @Test - public void select_crop() throws Exception { - // When - ScaleType result = ScaleTypeSelectors - .centerCropped(); - - // Then - assertEquals( - ScaleType.CENTER_CROP, - result - ); - } - - @Test - public void select_centerInside() throws Exception { - // When - ScaleType result = ScaleTypeSelectors - .centerInside(); - - // Then - assertEquals( - ScaleType.CENTER_INSIDE, - result - ); - } -} \ No newline at end of file diff --git a/sample/src/main/java/io/fotoapparat/sample/MainActivity.java b/sample/src/main/java/io/fotoapparat/sample/MainActivity.java index 50af626f..84a69da9 100644 --- a/sample/src/main/java/io/fotoapparat/sample/MainActivity.java +++ b/sample/src/main/java/io/fotoapparat/sample/MainActivity.java @@ -14,6 +14,7 @@ import io.fotoapparat.error.CameraErrorCallback; import io.fotoapparat.hardware.CameraException; import io.fotoapparat.parameter.LensPosition; +import io.fotoapparat.parameter.ScaleType; import io.fotoapparat.photo.BitmapPhoto; import io.fotoapparat.preview.Frame; import io.fotoapparat.preview.FrameProcessor; @@ -33,7 +34,6 @@ import static io.fotoapparat.parameter.selector.FocusModeSelectors.continuousFocus; import static io.fotoapparat.parameter.selector.FocusModeSelectors.fixed; import static io.fotoapparat.parameter.selector.LensPositionSelectors.lensPosition; -import static io.fotoapparat.parameter.selector.ScaleTypeSelectors.centerCropped; import static io.fotoapparat.parameter.selector.Selectors.firstAvailable; import static io.fotoapparat.parameter.selector.SizeSelectors.biggestSize; import static io.fotoapparat.result.transformer.SizeTransformers.scaled; @@ -123,8 +123,8 @@ private Fotoapparat createFotoapparat(LensPosition position) { return Fotoapparat .with(this) .into(cameraView) + .previewScaleType(ScaleType.CENTER_CROP) .photoSize(standardRatio(biggestSize())) - .previewScaleType(centerCropped()) .lensPosition(lensPosition(position)) .focusMode(firstAvailable( continuousFocus(),