Skip to content

Commit

Permalink
Added worst-case fix
Browse files Browse the repository at this point in the history
  • Loading branch information
fkeglevich committed Jan 19, 2019
1 parent b1940c9 commit 895962b
Showing 1 changed file with 24 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,31 @@ public CaptureSize fixValue(ValueValidator<CaptureSize, List<CaptureSize>> newVa
if (preview.hasSameAspectRatio(pictureSize))
previewCandidates.add(preview);

if (previewCandidates.isEmpty())
return getAtLeastOnePreviewSize(pictureSize, newValidator.getAvailableValues());

Collections.sort(previewCandidates);
return previewCandidates.get(previewCandidates.size() - 1);
}

private CaptureSize getAtLeastOnePreviewSize(CaptureSize pictureSize, List<CaptureSize> available)
{
double ratio = ((double) pictureSize.getAspectRatioX()) / ((double) pictureSize.getAspectRatioY());
double minDiff = Double.POSITIVE_INFINITY;
CaptureSize result = available.get(0);

for (CaptureSize preview : available)
{
double previewRatio = ((double) preview.getAspectRatioX()) / ((double) preview.getAspectRatioY());
double diff = Math.abs(ratio - previewRatio);

if (diff < minDiff)
{
minDiff = diff;
result = preview;
}
}

return result;
}
}

0 comments on commit 895962b

Please sign in to comment.