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) {
int currentWidth = currentSize.getWidth();
int currentHeight = currentSize.getHeight();
if (targetRatio.matches(currentSize)) {
if (targetRatio.matches(currentSize, 0.0005F)) {
return new Rect(0, 0, currentWidth, currentHeight);
}
@ -26,13 +26,13 @@ public class CropHelper {
int x, y, width, height;
if (currentRatio.toFloat() > targetRatio.toFloat()) {
height = currentHeight;
width = (int) (height * targetRatio.toFloat());
width = Math.round(height * targetRatio.toFloat());
y = 0;
x = (currentWidth - width) / 2;
x = Math.round((currentWidth - width) / 2F);
} else {
width = currentWidth;
height = (int) (width / targetRatio.toFloat());
y = (currentHeight - height) / 2;
height = Math.round(width / targetRatio.toFloat());
y = Math.round((currentHeight - height) / 2F);
x = 0;
}
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
*/
@NonNull
public static AspectRatio of(Size size) {
public static AspectRatio of(@NonNull Size size) {
return AspectRatio.of(size.getWidth(), size.getHeight());
}
@ -78,7 +78,6 @@ public class AspectRatio implements Comparable<AspectRatio> {
return mY;
}
@SuppressWarnings("WeakerAccess")
public boolean matches(@NonNull Size size) {
int gcd = gcd(size.getWidth(), size.getHeight());
int x = size.getWidth() / gcd;
@ -86,6 +85,10 @@ public class AspectRatio implements Comparable<AspectRatio> {
return mX == x && mY == y;
}
public boolean matches(@NonNull Size size, float tolerance) {
return Math.abs(toFloat() - (float) size.getWidth() / size.getHeight()) <= tolerance;
}
@Override
public boolean equals(Object o) {
if (o == null) {
@ -107,7 +110,6 @@ public class AspectRatio implements Comparable<AspectRatio> {
return mX + ":" + mY;
}
@SuppressWarnings("WeakerAccess")
public float toFloat() {
return (float) mX / mY;
}

Loading…
Cancel
Save