-
Notifications
You must be signed in to change notification settings - Fork 3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[camerax] Use AspectRatioStrategy
and ResolutionFilter
to help automatic selection of expected resolution
#6357
Merged
Merged
Changes from 1 commit
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
fc073eb
Add aspect ratio where I can
camsim99 a0da398
Start adding tests
camsim99 3d6a9f1
Add tests
camsim99 02d8bb0
Bump version
camsim99 e22de74
Merge remote-tracking branch 'upstream/main' into camx_aspect
camsim99 4151936
Update packages/camera/camera_android_camerax/lib/src/camerax_proxy.dart
camsim99 559df9b
Add functionality + java tests
camsim99 67a324e
Add dart tests
camsim99 e585343
Merge remote-tracking branch 'upstream/main' into camx_aspect
camsim99 f45e9d1
Update changelog
camsim99 92939cd
Merge remote-tracking branch 'refs/remotes/origin/camx_aspect' into c…
camsim99 196fd89
Nits
camsim99 29a77d5
Address review
camsim99 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
...camerax/android/src/main/java/io/flutter/plugins/camerax/ResolutionFilterHostApiImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
// Copyright 2013 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
package io.flutter.plugins.camerax; | ||
|
||
import android.util.Size; | ||
import androidx.annotation.NonNull; | ||
import androidx.annotation.VisibleForTesting; | ||
import androidx.camera.core.resolutionselector.ResolutionFilter; | ||
import io.flutter.plugins.camerax.GeneratedCameraXLibrary.ResolutionFilterHostApi; | ||
import io.flutter.plugins.camerax.GeneratedCameraXLibrary.ResolutionInfo; | ||
import java.util.List; | ||
|
||
/** | ||
* Host API implementation for {@link ResolutionFilter}. | ||
* | ||
* <p>This class handles instantiating and adding native object instances that are attached to a | ||
* Dart instance or handle method calls on the associated native class or an instance of the class. | ||
*/ | ||
public class ResolutionFilterHostApiImpl implements ResolutionFilterHostApi { | ||
private final InstanceManager instanceManager; | ||
private final ResolutionFilterProxy proxy; | ||
|
||
/** Proxy for constructor of {@link ResolutionFilter}. */ | ||
@VisibleForTesting | ||
public static class ResolutionFilterProxy { | ||
/** | ||
* Creates an instance of {@link ResolutionFilter} that moves the {@code preferredSize} to the | ||
* front of the list of supported resolutions such that it can be prioritized by CameraX. | ||
camsim99 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
*/ | ||
@NonNull | ||
public ResolutionFilter createWithOnePreferredSize(@NonNull Size preferredSize) { | ||
return new ResolutionFilter() { | ||
@Override | ||
@NonNull | ||
public List<Size> filter(@NonNull List<Size> supportedSizes, int rotationDegrees) { | ||
int preferredSizeIndex = supportedSizes.indexOf(preferredSize); | ||
|
||
if (preferredSizeIndex > -1) { | ||
supportedSizes.remove(preferredSizeIndex); | ||
supportedSizes.add(0, preferredSize); | ||
} | ||
|
||
return supportedSizes; | ||
} | ||
}; | ||
} | ||
} | ||
|
||
/** | ||
* Constructs a {@link ResolutionFilterHostApiImpl}. | ||
* | ||
* @param instanceManager maintains instances stored to communicate with attached Dart objects | ||
*/ | ||
public ResolutionFilterHostApiImpl(@NonNull InstanceManager instanceManager) { | ||
this(instanceManager, new ResolutionFilterProxy()); | ||
} | ||
|
||
/** | ||
* Constructs a {@link ResolutionFilterHostApiImpl}. | ||
* | ||
* @param instanceManager maintains instances stored to communicate with attached Dart objects | ||
* @param proxy proxy for constructor of {@link ResolutionFilter} | ||
*/ | ||
@VisibleForTesting | ||
ResolutionFilterHostApiImpl( | ||
@NonNull InstanceManager instanceManager, @NonNull ResolutionFilterProxy proxy) { | ||
this.instanceManager = instanceManager; | ||
this.proxy = proxy; | ||
} | ||
|
||
/** | ||
* Creates a {@link ResolutionFilter} that prioritizes the specified {@code preferredResolution} | ||
* over all other resolutions. | ||
*/ | ||
@Override | ||
public void createWithOnePreferredSize( | ||
@NonNull Long identifier, @NonNull ResolutionInfo preferredResolution) { | ||
Size preferredSize = | ||
new Size( | ||
preferredResolution.getWidth().intValue(), preferredResolution.getHeight().intValue()); | ||
instanceManager.addDartCreatedInstance( | ||
proxy.createWithOnePreferredSize(preferredSize), identifier); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
...ndroid_camerax/android/src/test/java/io/flutter/plugins/camerax/ResolutionFilterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
// Copyright 2013 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
package io.flutter.plugins.camerax; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
import android.util.Size; | ||
import androidx.camera.core.resolutionselector.ResolutionFilter; | ||
import io.flutter.plugins.camerax.GeneratedCameraXLibrary.ResolutionInfo; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.MockitoJUnit; | ||
import org.mockito.junit.MockitoRule; | ||
import org.robolectric.RobolectricTestRunner; | ||
|
||
@RunWith(RobolectricTestRunner.class) | ||
public class ResolutionFilterTest { | ||
@Rule public MockitoRule mockitoRule = MockitoJUnit.rule(); | ||
@Mock public ResolutionFilter mockResolutionFilter; | ||
|
||
InstanceManager instanceManager; | ||
|
||
@Before | ||
public void setUp() { | ||
instanceManager = InstanceManager.create(identifier -> {}); | ||
} | ||
|
||
@After | ||
public void tearDown() { | ||
instanceManager.stopFinalizationListener(); | ||
} | ||
|
||
@Test | ||
public void hostApiCreateWithOnePreferredSize_createsExpectedResolutionFilterInstance() { | ||
final android.util.Size lala = new android.util.Size(720, 480); | ||
final ResolutionFilterHostApiImpl hostApi = new ResolutionFilterHostApiImpl(instanceManager); | ||
final long instanceIdentifier = 50; | ||
final long preferredResolutionWidth = 20; | ||
final long preferredResolutionHeight = 80; | ||
final ResolutionInfo preferredResolution = | ||
new ResolutionInfo.Builder() | ||
.setWidth(preferredResolutionWidth) | ||
.setHeight(preferredResolutionHeight) | ||
.build(); | ||
|
||
hostApi.createWithOnePreferredSize(instanceIdentifier, preferredResolution); | ||
|
||
// Test instance filters supported resolutions as expected. | ||
final ResolutionFilter resolutionFilter = instanceManager.getInstance(instanceIdentifier); | ||
final Size fakeSupportedSize1 = new Size(720, 480); | ||
final Size fakeSupportedSize2 = new Size(20, 80); | ||
final Size fakeSupportedSize3 = new Size(2, 8); | ||
final Size preferredSize = | ||
new Size((int) preferredResolutionWidth, (int) preferredResolutionHeight); | ||
|
||
final ArrayList<Size> fakeSupportedSizes = new ArrayList<Size>(); | ||
fakeSupportedSizes.add(fakeSupportedSize1); | ||
fakeSupportedSizes.add(fakeSupportedSize2); | ||
fakeSupportedSizes.add(preferredSize); | ||
fakeSupportedSizes.add(fakeSupportedSize3); | ||
|
||
// Test case where preferred resolution is supported. | ||
List<Size> filteredSizes = resolutionFilter.filter(fakeSupportedSizes, 90); | ||
assertEquals(filteredSizes.get(0), preferredSize); | ||
|
||
// Test case where preferred resolution is not supported. | ||
fakeSupportedSizes.remove(0); | ||
filteredSizes = resolutionFilter.filter(fakeSupportedSizes, 90); | ||
assertEquals(filteredSizes, fakeSupportedSizes); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(I don't think this matters that much but just for my understanding) Isn't this more of a
ResolutionFilterConstructor/FactoryHostApiImpl
? Because we aren't wrapping the methods of theResolutionFilter
interface but rather of a class that defines and creates an instance of aResolutionFilter
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ooo yes I like this, thank you! I will rename the proxy class it uses to clarify that this calls into a factory of sorts.