From dd66e224436dd870b581aad28466221fb978ae7b Mon Sep 17 00:00:00 2001 From: Greyson Parrelli Date: Thu, 18 Jul 2019 16:10:59 -0400 Subject: [PATCH] Ensure that camera captures have correct dimensions. --- .../securesms/mediasend/CameraXFragment.java | 17 +------- .../mediasend/camerax/CameraXUtil.java | 39 ++++++++++++++++++- 2 files changed, 39 insertions(+), 17 deletions(-) diff --git a/src/org/thoughtcrime/securesms/mediasend/CameraXFragment.java b/src/org/thoughtcrime/securesms/mediasend/CameraXFragment.java index 86e0c4eb7..da607679c 100644 --- a/src/org/thoughtcrime/securesms/mediasend/CameraXFragment.java +++ b/src/org/thoughtcrime/securesms/mediasend/CameraXFragment.java @@ -230,8 +230,7 @@ public class CameraXFragment extends Fragment implements CameraFragment { SimpleTask.run(CameraXFragment.this.getLifecycle(), () -> { stopwatch.split("captured"); try { - byte[] bytes = CameraXUtil.toJpegBytes(image, rotationDegrees, camera.getCameraLensFacing() == CameraX.LensFacing.FRONT); - return new CaptureResult(bytes, image.getWidth(), image.getHeight()); + return CameraXUtil.toJpeg(image, rotationDegrees, camera.getCameraLensFacing() == CameraX.LensFacing.FRONT); } catch (IOException e) { return null; } finally { @@ -242,7 +241,7 @@ public class CameraXFragment extends Fragment implements CameraFragment { stopwatch.stop(TAG); if (result != null) { - controller.onImageCaptured(result.data, result.width, result.height); + controller.onImageCaptured(result.getData(), result.getWidth(), result.getHeight()); } else { controller.onCameraError(); } @@ -255,16 +254,4 @@ public class CameraXFragment extends Fragment implements CameraFragment { } }); } - - private static final class CaptureResult { - public final byte[] data; - public final int width; - public final int height; - - private CaptureResult(byte[] data, int width, int height) { - this.data = data; - this.width = width; - this.height = height; - } - } } diff --git a/src/org/thoughtcrime/securesms/mediasend/camerax/CameraXUtil.java b/src/org/thoughtcrime/securesms/mediasend/camerax/CameraXUtil.java index 36905e5cd..fd3454c12 100644 --- a/src/org/thoughtcrime/securesms/mediasend/camerax/CameraXUtil.java +++ b/src/org/thoughtcrime/securesms/mediasend/camerax/CameraXUtil.java @@ -27,8 +27,9 @@ public class CameraXUtil { private static final String TAG = Log.tag(CameraXUtil.class); + @SuppressWarnings("SuspiciousNameCombination") @RequiresApi(21) - public static byte[] toJpegBytes(@NonNull ImageProxy image, int rotation, boolean flip) throws IOException { + public static ImageResult toJpeg(@NonNull ImageProxy image, int rotation, boolean flip) throws IOException { ImageProxy.PlaneProxy[] planes = image.getPlanes(); ByteBuffer buffer = planes[0].getBuffer(); Rect cropRect = shouldCropImage(image) ? image.getCropRect() : null; @@ -40,7 +41,17 @@ public class CameraXUtil { data = transformByteArray(data, cropRect, rotation, flip); } - return data; + int width = cropRect != null ? (cropRect.right - cropRect.left) : image.getWidth(); + int height = cropRect != null ? (cropRect.bottom - cropRect.top) : image.getHeight(); + + if (rotation == 90 || rotation == 270) { + int swap = width; + + width = height; + height = swap; + } + + return new ImageResult(data, width, height); } public static int toCameraDirectionInt(@Nullable CameraX.LensFacing facing) { @@ -119,4 +130,28 @@ public class CameraXUtil { return out.toByteArray(); } + + public static class ImageResult { + private final byte[] data; + private final int width; + private final int height; + + public ImageResult(@NonNull byte[] data, int width, int height) { + this.data = data; + this.width = width; + this.height = height; + } + + public byte[] getData() { + return data; + } + + public int getWidth() { + return width; + } + + public int getHeight() { + return height; + } + } }