Skip to content

Commit cd7405c

Browse files
committed
Fix preview aspect ratios
Change-Id: I140883c9b6e22d12670e9c7e8278103f3b21bc68
1 parent 6ceaf86 commit cd7405c

File tree

2 files changed

+23
-16
lines changed

2 files changed

+23
-16
lines changed

library/src/main/api21/com/google/android/cameraview/Camera2.java

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@
3939
import java.util.Arrays;
4040
import java.util.Set;
4141
import java.util.SortedSet;
42-
import java.util.TreeSet;
4342

43+
@SuppressWarnings("MissingPermission")
4444
@TargetApi(21)
4545
class Camera2 extends CameraViewImpl {
4646

@@ -417,10 +417,19 @@ private void collectCameraInfo() {
417417
}
418418
mPreviewSizes.clear();
419419
for (android.util.Size size : map.getOutputSizes(mPreview.getOutputClass())) {
420-
mPreviewSizes.add(new Size(size.getWidth(), size.getHeight()));
420+
int width = size.getWidth();
421+
int height = size.getHeight();
422+
if (width <= MAX_PREVIEW_WIDTH && height <= MAX_PREVIEW_HEIGHT) {
423+
mPreviewSizes.add(new Size(width, height));
424+
}
421425
}
422426
mPictureSizes.clear();
423427
collectPictureSizes(mPictureSizes, map);
428+
for (AspectRatio ratio : mPreviewSizes.ratios()) {
429+
if (!mPictureSizes.ratios().contains(ratio)) {
430+
mPreviewSizes.remove(ratio);
431+
}
432+
}
424433

425434
if (!mPreviewSizes.ratios().contains(mAspectRatio)) {
426435
mAspectRatio = mPreviewSizes.ratios().iterator().next();
@@ -490,24 +499,16 @@ private Size chooseOptimalSize() {
490499
surfaceLonger = surfaceWidth;
491500
surfaceShorter = surfaceHeight;
492501
}
493-
SortedSet<Size> allCandidates = mPreviewSizes.sizes(mAspectRatio);
494-
495-
// Eliminate candidates that are bigger than Camera2 PREVIEW guarantees
496-
SortedSet<Size> guaranteedCandidates = new TreeSet<>();
497-
for (Size size: allCandidates) {
498-
if (size.getWidth() <= MAX_PREVIEW_WIDTH && size.getHeight() <= MAX_PREVIEW_HEIGHT) {
499-
guaranteedCandidates.add(size);
500-
}
501-
}
502+
SortedSet<Size> candidates = mPreviewSizes.sizes(mAspectRatio);
502503

503504
// Pick the smallest of those big enough
504-
for (Size size : guaranteedCandidates) {
505+
for (Size size : candidates) {
505506
if (size.getWidth() >= surfaceLonger && size.getHeight() >= surfaceShorter) {
506507
return size;
507508
}
508509
}
509510
// If no size is big enough, pick the largest one.
510-
return guaranteedCandidates.last();
511+
return candidates.last();
511512
}
512513

513514
/**

library/src/main/base/com/google/android/cameraview/SizeMap.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,15 @@ public boolean add(Size size) {
5454
return true;
5555
}
5656

57+
/**
58+
* Removes the specified aspect ratio and all sizes associated with it.
59+
*
60+
* @param ratio The aspect ratio to be removed.
61+
*/
62+
public void remove(AspectRatio ratio) {
63+
mRatios.remove(ratio);
64+
}
65+
5766
Set<AspectRatio> ratios() {
5867
return mRatios.keySet();
5968
}
@@ -70,7 +79,4 @@ boolean isEmpty() {
7079
return mRatios.isEmpty();
7180
}
7281

73-
public void remove(AspectRatio ratio) {
74-
mRatios.remove(ratio);
75-
}
7682
}

0 commit comments

Comments
 (0)