() {
@Override
public int compare(ResolutionBucket resolutionBucket, ResolutionBucket resolutionBucket2) {
return Integer.compare(resolutionBucket2.maxPixels, resolutionBucket.maxPixels);
}
});
return sortedBuckets;
}
/**
* Given a size, return a string describing the aspect ratio by reducing the
*
* @param size the size to describe
* @return a string description of the aspect ratio
*/
public static String aspectRatioDescription(Size size) {
Size aspectRatio = reduce(size);
return aspectRatio.width() + "x" + aspectRatio.height();
}
/**
* Reduce an aspect ratio to its lowest common denominator. The ratio of the
* input and output sizes is guaranteed to be the same.
*
* @param aspectRatio the aspect ratio to reduce
* @return The reduced aspect ratio which may equal the original.
*/
public static Size reduce(Size aspectRatio) {
BigInteger width = BigInteger.valueOf(aspectRatio.width());
BigInteger height = BigInteger.valueOf(aspectRatio.height());
BigInteger gcd = width.gcd(height);
int numerator = Math.max(width.intValue(), height.intValue()) / gcd.intValue();
int denominator = Math.min(width.intValue(), height.intValue()) / gcd.intValue();
return new Size(numerator, denominator);
}
/**
* Given a size return the numerator of its aspect ratio
*
* @param size the size to measure
* @return the numerator
*/
public static int aspectRatioNumerator(Size size) {
Size aspectRatio = reduce(size);
return aspectRatio.width();
}
/**
* Given a size, return the closest aspect ratio that falls close to the
* given size.
*
* @param size the size to approximate
* @return the closest desired aspect ratio, or the original aspect ratio if
* none were close enough
*/
public static Size getApproximateSize(Size size) {
Size aspectRatio = reduce(size);
float fuzzy = fuzzAspectRatio(size.width() / (float) size.height());
int index = Arrays.asList(sDesiredAspectRatios).indexOf(fuzzy);
if (index != -1) {
aspectRatio = new Size(sDesiredAspectRatioSizes[index]);
}
return aspectRatio;
}
/**
* See {@link #getApproximateSize(Size)}.
*
* TODO: Move this whole util to {@link android.util.Size}
*/
public static com.android.camera.util.Size getApproximateSize(
com.android.camera.util.Size size) {
Size result = getApproximateSize(new Size(size.getWidth(), size.getHeight()));
return new com.android.camera.util.Size(result.width(), result.height());
}
/**
* Given a size return the numerator of its aspect ratio
*
* @param size
* @return the denominator
*/
public static int aspectRatioDenominator(Size size) {
BigInteger width = BigInteger.valueOf(size.width());
BigInteger height = BigInteger.valueOf(size.height());
BigInteger gcd = width.gcd(height);
int denominator = Math.min(width.intValue(), height.intValue()) / gcd.intValue();
return denominator;
}
}