Issue #795: avoid sorting preview sizes

This commit is contained in:
Sean Owen 2017-05-05 00:29:57 +01:00
parent ff7137e512
commit 3d0468e92f

View file

@ -22,11 +22,9 @@ import android.hardware.Camera;
import android.os.Build; import android.os.Build;
import android.util.Log; import android.util.Log;
import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@ -284,42 +282,24 @@ public final class CameraConfigurationUtils {
return new Point(defaultSize.width, defaultSize.height); return new Point(defaultSize.width, defaultSize.height);
} }
// Sort by size, descending
List<Camera.Size> supportedPreviewSizes = new ArrayList<>(rawSupportedSizes);
Collections.sort(supportedPreviewSizes, new Comparator<Camera.Size>() {
@Override
public int compare(Camera.Size a, Camera.Size b) {
int aPixels = a.height * a.width;
int bPixels = b.height * b.width;
if (bPixels < aPixels) {
return -1;
}
if (bPixels > aPixels) {
return 1;
}
return 0;
}
});
if (Log.isLoggable(TAG, Log.INFO)) { if (Log.isLoggable(TAG, Log.INFO)) {
StringBuilder previewSizesString = new StringBuilder(); StringBuilder previewSizesString = new StringBuilder();
for (Camera.Size supportedPreviewSize : supportedPreviewSizes) { for (Camera.Size size : rawSupportedSizes) {
previewSizesString.append(supportedPreviewSize.width).append('x') previewSizesString.append(size.width).append('x').append(size.height).append(' ');
.append(supportedPreviewSize.height).append(' ');
} }
Log.i(TAG, "Supported preview sizes: " + previewSizesString); Log.i(TAG, "Supported preview sizes: " + previewSizesString);
} }
double screenAspectRatio = screenResolution.x / (double) screenResolution.y; double screenAspectRatio = screenResolution.x / (double) screenResolution.y;
// Remove sizes that are unsuitable // Find a suitable size, with max resolution
Iterator<Camera.Size> it = supportedPreviewSizes.iterator(); int maxResolution = 0;
while (it.hasNext()) { Camera.Size maxResPreviewSize = null;
Camera.Size supportedPreviewSize = it.next(); for (Camera.Size size : rawSupportedSizes) {
int realWidth = supportedPreviewSize.width; int realWidth = size.width;
int realHeight = supportedPreviewSize.height; int realHeight = size.height;
if (realWidth * realHeight < MIN_PREVIEW_PIXELS) { int resolution = realWidth * realHeight;
it.remove(); if (resolution < MIN_PREVIEW_PIXELS) {
continue; continue;
} }
@ -329,7 +309,6 @@ public final class CameraConfigurationUtils {
double aspectRatio = maybeFlippedWidth / (double) maybeFlippedHeight; double aspectRatio = maybeFlippedWidth / (double) maybeFlippedHeight;
double distortion = Math.abs(aspectRatio - screenAspectRatio); double distortion = Math.abs(aspectRatio - screenAspectRatio);
if (distortion > MAX_ASPECT_DISTORTION) { if (distortion > MAX_ASPECT_DISTORTION) {
it.remove();
continue; continue;
} }
@ -338,14 +317,19 @@ public final class CameraConfigurationUtils {
Log.i(TAG, "Found preview size exactly matching screen size: " + exactPoint); Log.i(TAG, "Found preview size exactly matching screen size: " + exactPoint);
return exactPoint; return exactPoint;
} }
// Resolution is suitable; record the one with max resolution
if (resolution > maxResolution) {
maxResolution = resolution;
maxResPreviewSize = size;
}
} }
// If no exact match, use largest preview size. This was not a great idea on older devices because // If no exact match, use largest preview size. This was not a great idea on older devices because
// of the additional computation needed. We're likely to get here on newer Android 4+ devices, where // of the additional computation needed. We're likely to get here on newer Android 4+ devices, where
// the CPU is much more powerful. // the CPU is much more powerful.
if (!supportedPreviewSizes.isEmpty()) { if (maxResPreviewSize != null) {
Camera.Size largestPreview = supportedPreviewSizes.get(0); Point largestSize = new Point(maxResPreviewSize.width, maxResPreviewSize.height);
Point largestSize = new Point(largestPreview.width, largestPreview.height);
Log.i(TAG, "Using largest suitable preview size: " + largestSize); Log.i(TAG, "Using largest suitable preview size: " + largestSize);
return largestSize; return largestSize;
} }