Skip to content
This repository has been archived by the owner on Dec 29, 2022. It is now read-only.

Commit

Permalink
Fix preview aspect ratios
Browse files Browse the repository at this point in the history
Change-Id: I140883c9b6e22d12670e9c7e8278103f3b21bc68
  • Loading branch information
yaraki committed Mar 24, 2017
1 parent 6ceaf86 commit cd7405c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 16 deletions.
27 changes: 14 additions & 13 deletions library/src/main/api21/com/google/android/cameraview/Camera2.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@
import java.util.Arrays;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;

@SuppressWarnings("MissingPermission")
@TargetApi(21)
class Camera2 extends CameraViewImpl {

Expand Down Expand Up @@ -417,10 +417,19 @@ private void collectCameraInfo() {
}
mPreviewSizes.clear();
for (android.util.Size size : map.getOutputSizes(mPreview.getOutputClass())) {
mPreviewSizes.add(new Size(size.getWidth(), size.getHeight()));
int width = size.getWidth();
int height = size.getHeight();
if (width <= MAX_PREVIEW_WIDTH && height <= MAX_PREVIEW_HEIGHT) {
mPreviewSizes.add(new Size(width, height));
}
}
mPictureSizes.clear();
collectPictureSizes(mPictureSizes, map);
for (AspectRatio ratio : mPreviewSizes.ratios()) {
if (!mPictureSizes.ratios().contains(ratio)) {
mPreviewSizes.remove(ratio);
}
}

if (!mPreviewSizes.ratios().contains(mAspectRatio)) {
mAspectRatio = mPreviewSizes.ratios().iterator().next();
Expand Down Expand Up @@ -490,24 +499,16 @@ private Size chooseOptimalSize() {
surfaceLonger = surfaceWidth;
surfaceShorter = surfaceHeight;
}
SortedSet<Size> allCandidates = mPreviewSizes.sizes(mAspectRatio);

// Eliminate candidates that are bigger than Camera2 PREVIEW guarantees
SortedSet<Size> guaranteedCandidates = new TreeSet<>();
for (Size size: allCandidates) {
if (size.getWidth() <= MAX_PREVIEW_WIDTH && size.getHeight() <= MAX_PREVIEW_HEIGHT) {
guaranteedCandidates.add(size);
}
}
SortedSet<Size> candidates = mPreviewSizes.sizes(mAspectRatio);

// Pick the smallest of those big enough
for (Size size : guaranteedCandidates) {
for (Size size : candidates) {
if (size.getWidth() >= surfaceLonger && size.getHeight() >= surfaceShorter) {
return size;
}
}
// If no size is big enough, pick the largest one.
return guaranteedCandidates.last();
return candidates.last();
}

/**
Expand Down
12 changes: 9 additions & 3 deletions library/src/main/base/com/google/android/cameraview/SizeMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,15 @@ public boolean add(Size size) {
return true;
}

/**
* Removes the specified aspect ratio and all sizes associated with it.
*
* @param ratio The aspect ratio to be removed.
*/
public void remove(AspectRatio ratio) {
mRatios.remove(ratio);
}

Set<AspectRatio> ratios() {
return mRatios.keySet();
}
Expand All @@ -70,7 +79,4 @@ boolean isEmpty() {
return mRatios.isEmpty();
}

public void remove(AspectRatio ratio) {
mRatios.remove(ratio);
}
}

0 comments on commit cd7405c

Please sign in to comment.