Skip to content

Instantly share code, notes, and snippets.

@osamaqarem
Created January 7, 2021 05:40
Show Gist options
  • Save osamaqarem/c4901513253100b295641486d24f7a57 to your computer and use it in GitHub Desktop.
Save osamaqarem/c4901513253100b295641486d24f7a57 to your computer and use it in GitHub Desktop.
Calculate Closest Aspect Ratio
private Camera.Size calBestPreviewSize(Camera.Parameters camPara,
final int width, final int height) {
List<Camera.Size> allSupportedSize = camPara.getSupportedPreviewSizes();
ArrayList<Camera.Size> widthLargerSize = new ArrayList<Camera.Size>();
for (Camera.Size tmpSize : allSupportedSize) {
if (tmpSize.width > tmpSize.height) {
widthLargerSize.add(tmpSize);
}
}
Collections.sort(widthLargerSize, new Comparator<Camera.Size>() {
@Override
public int compare(Camera.Size lhs, Camera.Size rhs) {
int off_one = Math.abs(lhs.width * lhs.height - width * height);
int off_two = Math.abs(rhs.width * rhs.height - width * height);
return off_one - off_two;
}
});
return widthLargerSize.get(0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment