Merge branch 'qr_scan_fix' of https://github.com/klonfish/Conversations into klonfish-qr_scan_fix

Daniel Gultsch created

Change summary

src/main/java/eu/siacs/conversations/ui/service/CameraManager.java | 30 
1 file changed, 23 insertions(+), 7 deletions(-)

Detailed changes

src/main/java/eu/siacs/conversations/ui/service/CameraManager.java 🔗

@@ -85,22 +85,38 @@ public final class CameraManager {
 
         camera.setPreviewTexture(textureView.getSurfaceTexture());
 
-        final Camera.Parameters parameters = camera.getParameters();
-
-        cameraResolution = findBestPreviewSizeValue(parameters, textureView.getWidth(), textureView.getHeight());
-
         final int width = textureView.getWidth();
         final int height = textureView.getHeight();
 
+        final Camera.Parameters parameters = camera.getParameters();
+
+        cameraResolution = findBestPreviewSizeValue(parameters, width, height);
+
         final int rawSize = Math.min(width * 2 / 3, height * 2 / 3);
         final int frameSize = Math.max(MIN_FRAME_SIZE, Math.min(MAX_FRAME_SIZE, rawSize));
 
         final int leftOffset = (width - frameSize) / 2;
         final int topOffset = (height - frameSize) / 2;
         frame = new Rect(leftOffset, topOffset, leftOffset + frameSize, topOffset + frameSize);
-        framePreview = new RectF(frame.left * cameraResolution.width / width,
-                frame.top * cameraResolution.height / height, frame.right * cameraResolution.width / width,
-                frame.bottom * cameraResolution.height / height);
+
+        float widthFactor;
+        float heightFactor;
+        Rect orientedFrame;
+        boolean isTexturePortrait = width < height;
+        boolean isCameraPortrait = cameraResolution.width < cameraResolution.height;
+        if (isTexturePortrait == isCameraPortrait) {
+            widthFactor = (float)cameraResolution.width / width;
+            heightFactor = (float)cameraResolution.height / height;
+            orientedFrame = new Rect(frame);
+        } else {
+            widthFactor = (float)cameraResolution.width / height;
+            heightFactor = (float)cameraResolution.height / width;
+            // Swap X and Y coordinates to flip frame to the same orientation as cameraResolution
+            orientedFrame = new Rect(frame.top, frame.left, frame.bottom, frame.right);
+        }
+
+        framePreview = new RectF(orientedFrame.left * widthFactor, orientedFrame.top * heightFactor,
+                orientedFrame.right * widthFactor, orientedFrame.bottom * heightFactor);
 
         final String savedParameters = parameters == null ? null : parameters.flatten();