diff --git a/cameraview/src/main/java/com/otaliastudios/cameraview/internal/utils/CropHelper.java b/cameraview/src/main/java/com/otaliastudios/cameraview/internal/utils/CropHelper.java index 59586120..f7f71ee7 100644 --- a/cameraview/src/main/java/com/otaliastudios/cameraview/internal/utils/CropHelper.java +++ b/cameraview/src/main/java/com/otaliastudios/cameraview/internal/utils/CropHelper.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); diff --git a/cameraview/src/main/java/com/otaliastudios/cameraview/size/AspectRatio.java b/cameraview/src/main/java/com/otaliastudios/cameraview/size/AspectRatio.java index 71a47572..e13b73c1 100644 --- a/cameraview/src/main/java/com/otaliastudios/cameraview/size/AspectRatio.java +++ b/cameraview/src/main/java/com/otaliastudios/cameraview/size/AspectRatio.java @@ -19,7 +19,7 @@ public class AspectRatio implements Comparable { * @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 { 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 { 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 { return mX + ":" + mY; } - @SuppressWarnings("WeakerAccess") public float toFloat() { return (float) mX / mY; }