diff --git a/src/main/java/eu/siacs/conversations/ui/service/CameraManager.java b/src/main/java/eu/siacs/conversations/ui/service/CameraManager.java index a818368de..9f1fc99a7 100644 --- a/src/main/java/eu/siacs/conversations/ui/service/CameraManager.java +++ b/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();