Skip to content

Instantly share code, notes, and snippets.

@Arjun-sna
Last active January 12, 2024 09:41
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Arjun-sna/58251a60d86239f196e735447a97dab2 to your computer and use it in GitHub Desktop.
Save Arjun-sna/58251a60d86239f196e735447a97dab2 to your computer and use it in GitHub Desktop.
Get best preview size for android camera
private static final double MAX_ASPECT_DISTORTION = 0.15;
private static final float ASPECT_RATIO_TOLERANCE = 0.01f;
//desiredWidth and desiredHeight can be the screen size of mobile device
private static SizePair generateValidPreviewSize(Camera camera, int desiredWidth,
int desiredHeight) {
Camera.Parameters parameters = camera.getParameters();
double screenAspectRatio = desiredWidth / (double) desiredHeight;
List<Camera.Size> supportedPreviewSizes = parameters.getSupportedPreviewSizes();
List<Camera.Size> supportedPictureSizes = parameters.getSupportedPictureSizes();
SizePair bestPair = null;
double currentMinDistortion = MAX_ASPECT_DISTORTION;
for (Camera.Size previewSize : supportedPreviewSizes) {
float previewAspectRatio = (float) previewSize.width / (float) previewSize.height;
for (Camera.Size pictureSize : supportedPictureSizes) {
float pictureAspectRatio = (float) pictureSize.width / (float) pictureSize.height;
if (Math.abs(previewAspectRatio - pictureAspectRatio) < ASPECT_RATIO_TOLERANCE) {
SizePair sizePair = new SizePair(previewSize, pictureSize);
boolean isCandidatePortrait = previewSize.width < previewSize.height;
int maybeFlippedWidth = isCandidatePortrait ? previewSize.width : previewSize.height;
int maybeFlippedHeight = isCandidatePortrait ? previewSize.height : previewSize.width;
double aspectRatio = maybeFlippedWidth / (double) maybeFlippedHeight;
double distortion = Math.abs(aspectRatio - screenAspectRatio);
if (distortion < currentMinDistortion) {
currentMinDistortion = distortion;
bestPair = sizePair;
}
break;
}
}
}
return bestPair;
}
private static class SizePair {
private Size mPreview;
private Size mPicture;
public SizePair(Camera.Size previewSize, Camera.Size pictureSize) {
mPreview = new Size(previewSize.width, previewSize.height);
if (pictureSize != null) {
mPicture = new Size(pictureSize.width, pictureSize.height);
}
}
public Size previewSize() {
return mPreview;
}
@SuppressWarnings("unused") public Size pictureSize() {
return mPicture;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment