Skip to content

Commit 3d1a254

Browse files
lnishanowahltinez
authored andcommitted
Fix transformation matrix calculation and layout (#127)
The CL fixes the following issues: - Transformation matrix calculation When calculating transformation, we need to take sensor orientation into consideration. The first step of the calculation tries to resize the rectangle back to its original buffer dimensions. However it falsely assumes that the buffer dimensions are (preview height, preview width) (i.e., buffer is rotated by 90/270 degrees). Another bug happens when scaling the rectangle down/up so that it just covers the viewfinder. It falsely assumes that viewWidth is the longer edge and viewHeight is the shorter edge. This causes the preview to be potentially scaled up too large, leading to a "zoom-in" effect. - Landscape layout When texture view is able to fill most of the screen, the control layout will be squished to a point the width becomes too small to make the shutter button visible. Test: Tested this on a Chromebook and Google Pixel 3
1 parent 6455bb6 commit 3d1a254

2 files changed

Lines changed: 15 additions & 8 deletions

File tree

Camera2BasicJava/Application/src/main/java/com/example/android/camera2basic/Camera2BasicFragment.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -745,16 +745,26 @@ private void configureTransform(int viewWidth, int viewHeight) {
745745
int rotation = activity.getWindowManager().getDefaultDisplay().getRotation();
746746
Matrix matrix = new Matrix();
747747
RectF viewRect = new RectF(0, 0, viewWidth, viewHeight);
748-
RectF bufferRect = new RectF(0, 0, mPreviewSize.getHeight(), mPreviewSize.getWidth());
748+
RectF bufferRect;
749+
if (mSensorOrientation == 0 || mSensorOrientation == 180) {
750+
bufferRect = new RectF(0, 0, mPreviewSize.getWidth(), mPreviewSize.getHeight());
751+
} else { // 90, 270
752+
bufferRect = new RectF(0, 0, mPreviewSize.getHeight(), mPreviewSize.getWidth());
753+
}
749754
float centerX = viewRect.centerX();
750755
float centerY = viewRect.centerY();
751756
if (Surface.ROTATION_90 == rotation || Surface.ROTATION_270 == rotation) {
757+
// Resize the distorted rectangle in viewRect back to the dimensions of the source (bufferRect).
752758
bufferRect.offset(centerX - bufferRect.centerX(), centerY - bufferRect.centerY());
753759
matrix.setRectToRect(viewRect, bufferRect, Matrix.ScaleToFit.FILL);
760+
// Scale the rectangle back so that it just covers the viewfinder.
761+
float viewLongEdge = viewWidth > viewHeight ? viewWidth : viewHeight;
762+
float viewShortEdge = viewWidth <= viewHeight ? viewWidth : viewHeight;
754763
float scale = Math.max(
755-
(float) viewHeight / mPreviewSize.getHeight(),
756-
(float) viewWidth / mPreviewSize.getWidth());
764+
(float) viewShortEdge / mPreviewSize.getHeight(),
765+
(float) viewLongEdge / mPreviewSize.getWidth());
757766
matrix.postScale(scale, scale, centerX, centerY);
767+
// Rotate the rectangle to the correct orientation.
758768
matrix.postRotate(90 * (rotation - 2), centerX, centerY);
759769
} else if (Surface.ROTATION_180 == rotation) {
760770
matrix.postRotate(180, centerX, centerY);

Camera2BasicJava/Application/src/main/res/layout-land/fragment_camera2_basic.xml

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,9 @@
2727

2828
<FrameLayout
2929
android:id="@+id/control"
30-
android:layout_width="match_parent"
31-
android:layout_height="wrap_content"
32-
android:layout_alignParentBottom="true"
30+
android:layout_width="wrap_content"
31+
android:layout_height="match_parent"
3332
android:layout_alignParentEnd="true"
34-
android:layout_alignParentTop="true"
35-
android:layout_toRightOf="@id/texture"
3633
android:background="@color/control_background"
3734
android:orientation="horizontal">
3835

0 commit comments

Comments
 (0)