Avoid weird sizes in snapshot output

pull/524/head
Mattia Iavarone 6 years ago
parent 50e9a2499a
commit bcd331c495
  1. 10
      cameraview/src/main/java/com/otaliastudios/cameraview/internal/utils/CropHelper.java
  2. 8
      cameraview/src/main/java/com/otaliastudios/cameraview/size/AspectRatio.java

@ -17,7 +17,7 @@ public class CropHelper {
public static Rect computeCrop(@NonNull Size currentSize, @NonNull AspectRatio targetRatio) { public static Rect computeCrop(@NonNull Size currentSize, @NonNull AspectRatio targetRatio) {
int currentWidth = currentSize.getWidth(); int currentWidth = currentSize.getWidth();
int currentHeight = currentSize.getHeight(); int currentHeight = currentSize.getHeight();
if (targetRatio.matches(currentSize)) { if (targetRatio.matches(currentSize, 0.0005F)) {
return new Rect(0, 0, currentWidth, currentHeight); return new Rect(0, 0, currentWidth, currentHeight);
} }
@ -26,13 +26,13 @@ public class CropHelper {
int x, y, width, height; int x, y, width, height;
if (currentRatio.toFloat() > targetRatio.toFloat()) { if (currentRatio.toFloat() > targetRatio.toFloat()) {
height = currentHeight; height = currentHeight;
width = (int) (height * targetRatio.toFloat()); width = Math.round(height * targetRatio.toFloat());
y = 0; y = 0;
x = (currentWidth - width) / 2; x = Math.round((currentWidth - width) / 2F);
} else { } else {
width = currentWidth; width = currentWidth;
height = (int) (width / targetRatio.toFloat()); height = Math.round(width / targetRatio.toFloat());
y = (currentHeight - height) / 2; y = Math.round((currentHeight - height) / 2F);
x = 0; x = 0;
} }
return new Rect(x, y, x + width, y + height); return new Rect(x, y, x + width, y + height);

@ -19,7 +19,7 @@ public class AspectRatio implements Comparable<AspectRatio> {
* @return a (possibly cached) aspect ratio * @return a (possibly cached) aspect ratio
*/ */
@NonNull @NonNull
public static AspectRatio of(Size size) { public static AspectRatio of(@NonNull Size size) {
return AspectRatio.of(size.getWidth(), size.getHeight()); return AspectRatio.of(size.getWidth(), size.getHeight());
} }
@ -78,7 +78,6 @@ public class AspectRatio implements Comparable<AspectRatio> {
return mY; return mY;
} }
@SuppressWarnings("WeakerAccess")
public boolean matches(@NonNull Size size) { public boolean matches(@NonNull Size size) {
int gcd = gcd(size.getWidth(), size.getHeight()); int gcd = gcd(size.getWidth(), size.getHeight());
int x = size.getWidth() / gcd; int x = size.getWidth() / gcd;
@ -86,6 +85,10 @@ public class AspectRatio implements Comparable<AspectRatio> {
return mX == x && mY == y; return mX == x && mY == y;
} }
public boolean matches(@NonNull Size size, float tolerance) {
return Math.abs(toFloat() - (float) size.getWidth() / size.getHeight()) <= tolerance;
}
@Override @Override
public boolean equals(Object o) { public boolean equals(Object o) {
if (o == null) { if (o == null) {
@ -107,7 +110,6 @@ public class AspectRatio implements Comparable<AspectRatio> {
return mX + ":" + mY; return mX + ":" + mY;
} }
@SuppressWarnings("WeakerAccess")
public float toFloat() { public float toFloat() {
return (float) mX / mY; return (float) mX / mY;
} }

Loading…
Cancel
Save