Skip to content

Instantly share code, notes, and snippets.

@extralam
Last active December 29, 2015 22:29
Show Gist options
  • Save extralam/7736871 to your computer and use it in GitHub Desktop.
Save extralam/7736871 to your computer and use it in GitHub Desktop.
Something wrong in the API DEMO camera
// Something wrong in the API DEMO
private Size getOptimalPreviewSize(List<Size> sizes, int w, int h) {
final double ASPECT_TOLERANCE = 0.17;
double targetRatio = (double) h / w;
if (sizes == null) return null;
Size optimalSize = null;
double minDiff = Double.MAX_VALUE;
int targetWidth = w;
// Try to find an size match aspect ratio and size
for (Size size : sizes) {
double ratio = (double) size.height / size.width;
if (Math.abs(ratio - targetRatio) > ASPECT_TOLERANCE) continue;
// added targetWidth >= size.width
if (targetWidth >= size.width && Math.abs(size.width - targetWidth) < minDiff){
optimalSize = size;
minDiff = Math.abs(size.width - targetWidth);
}
}
// Cannot find the one match the aspect ratio, ignore the requirement
if (optimalSize == null) {
minDiff = Double.MAX_VALUE;
for (Size size : sizes) {
// added targetWidth >= size.width
if (targetWidth >= size.width && Math.abs(size.width - targetWidth) < minDiff) {
optimalSize = size;
minDiff = Math.abs(size.width - targetWidth);
}
}
}
return optimalSize;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment