From 280f0055f69073d8ff6c4ce99cf5da6a545f1c9d Mon Sep 17 00:00:00 2001 From: Matthew Date: Tue, 22 Oct 2024 13:43:54 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E8=B0=83=E6=95=B4UI?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../xypower/mppreview/AutoFitTextureView.java | 10 +++ .../xypower/mppreview/Camera2RawFragment.java | 28 +++++--- .../layout-land/fragment_camera2_basic.xml | 69 +++++++++---------- 3 files changed, 62 insertions(+), 45 deletions(-) diff --git a/Application/src/main/java/com/xypower/mppreview/AutoFitTextureView.java b/Application/src/main/java/com/xypower/mppreview/AutoFitTextureView.java index f3940f3..d5bf540 100644 --- a/Application/src/main/java/com/xypower/mppreview/AutoFitTextureView.java +++ b/Application/src/main/java/com/xypower/mppreview/AutoFitTextureView.java @@ -18,6 +18,7 @@ package com.xypower.mppreview; import android.content.Context; import android.util.AttributeSet; +import android.util.Log; import android.view.TextureView; /** @@ -52,11 +53,15 @@ public class AutoFitTextureView extends TextureView { if (width < 0 || height < 0) { throw new IllegalArgumentException("Size cannot be negative."); } + + Log.i("AutoFit", "setAspectRatio Width=" + width + " Height=" + height); + if (mRatioWidth == width && mRatioHeight == height) { return; } mRatioWidth = width; mRatioHeight = height; + requestLayout(); } @@ -65,12 +70,17 @@ public class AutoFitTextureView extends TextureView { super.onMeasure(widthMeasureSpec, heightMeasureSpec); int width = MeasureSpec.getSize(widthMeasureSpec); int height = MeasureSpec.getSize(heightMeasureSpec); + + if (0 == mRatioWidth || 0 == mRatioHeight) { + Log.i("AutoFit", "0 Width=" + width + " Height=" + height); setMeasuredDimension(width, height); } else { if (width < height * mRatioWidth / mRatioHeight) { + Log.i("AutoFit", "Width=" + width + " Height=" + ((int)(width * mRatioHeight / mRatioWidth))); setMeasuredDimension(width, width * mRatioHeight / mRatioWidth); } else { + Log.i("AutoFit", "Width=" + ((int)height * mRatioWidth / mRatioHeight) + " Height=" + height); setMeasuredDimension(height * mRatioWidth / mRatioHeight, height); } } diff --git a/Application/src/main/java/com/xypower/mppreview/Camera2RawFragment.java b/Application/src/main/java/com/xypower/mppreview/Camera2RawFragment.java index 55628d2..7ca0a19 100644 --- a/Application/src/main/java/com/xypower/mppreview/Camera2RawFragment.java +++ b/Application/src/main/java/com/xypower/mppreview/Camera2RawFragment.java @@ -177,7 +177,7 @@ public class Camera2RawFragment extends Fragment /** * Tag for the {@link Log}. */ - private static final String TAG = "Camera2RawFragment"; + private static final String TAG = "MpPreview"; /** * Camera state: Device is closed. @@ -626,6 +626,7 @@ public class Camera2RawFragment extends Fragment } else { mTextureView.setSurfaceTextureListener(mSurfaceTextureListener); } + if (mOrientationListener != null && mOrientationListener.canDetectOrientation()) { mOrientationListener.enable(); } @@ -1033,7 +1034,7 @@ public class Camera2RawFragment extends Fragment // Swap the view dimensions for calculation as needed if they are rotated relative to // the sensor. - boolean swappedDimensions = totalRotation == 90 || totalRotation == 270; + final boolean swappedDimensions = totalRotation == 90 || totalRotation == 270; int rotatedViewWidth = viewWidth; int rotatedViewHeight = viewHeight; int maxPreviewWidth = displaySize.x; @@ -1056,17 +1057,24 @@ public class Camera2RawFragment extends Fragment } // Find the best preview size for these view dimensions and configured JPEG size. - Size previewSize = chooseOptimalSize(map.getOutputSizes(SurfaceTexture.class), + final Size previewSize = chooseOptimalSize(map.getOutputSizes(SurfaceTexture.class), rotatedViewWidth, rotatedViewHeight, maxPreviewWidth, maxPreviewHeight, largestJpeg); - if (swappedDimensions) { - mTextureView.setAspectRatio( - previewSize.getHeight(), previewSize.getWidth()); - } else { - mTextureView.setAspectRatio( - previewSize.getWidth(), previewSize.getHeight()); - } + mMessageHandler.postDelayed(new Runnable() { + @Override + public void run() { + if (swappedDimensions) { + mTextureView.setAspectRatio( + previewSize.getHeight(), previewSize.getWidth()); + } else { + mTextureView.setAspectRatio( + previewSize.getWidth(), previewSize.getHeight()); + } + } + }, 0); + + // Find rotation of device in degrees (reverse device orientation for front-facing // cameras). diff --git a/Application/src/main/res/layout-land/fragment_camera2_basic.xml b/Application/src/main/res/layout-land/fragment_camera2_basic.xml index 76e78de..7ccd3cf 100644 --- a/Application/src/main/res/layout-land/fragment_camera2_basic.xml +++ b/Application/src/main/res/layout-land/fragment_camera2_basic.xml @@ -19,49 +19,48 @@ android:layout_width="match_parent" android:layout_height="match_parent"> - + android:background="#000" > - + + - - - - + From afefe0027f40c91a14ecc89cbf2176ce34a43ed7 Mon Sep 17 00:00:00 2001 From: Matthew Date: Tue, 22 Oct 2024 14:27:20 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../xypower/mppreview/Camera2RawFragment.java | 30 +++++++++---------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/Application/src/main/java/com/xypower/mppreview/Camera2RawFragment.java b/Application/src/main/java/com/xypower/mppreview/Camera2RawFragment.java index 7ca0a19..fea00b0 100644 --- a/Application/src/main/java/com/xypower/mppreview/Camera2RawFragment.java +++ b/Application/src/main/java/com/xypower/mppreview/Camera2RawFragment.java @@ -706,9 +706,11 @@ public class Camera2RawFragment extends Fragment CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP); // For still image captures, we use the largest available size. - Size largestJpeg = Collections.max( - Arrays.asList(map.getOutputSizes(ImageFormat.JPEG)), - new CompareSizesByArea()); + Size[] resolutions = map.getHighResolutionOutputSizes(ImageFormat.JPEG); + if (resolutions == null) { + resolutions = map.getOutputSizes(ImageFormat.JPEG); + } + Size largestJpeg = Collections.max(Arrays.asList(resolutions), new CompareSizesByArea()); synchronized (mCameraStateLock) { // Set up ImageReaders for JPEG and RAW outputs. Place these in a reference @@ -1028,7 +1030,7 @@ public class Camera2RawFragment extends Fragment int deviceRotation = activity.getWindowManager().getDefaultDisplay().getRotation(); Point displaySize = new Point(); activity.getWindowManager().getDefaultDisplay().getSize(displaySize); - + // Find the rotation of the device relative to the camera sensor's orientation. int totalRotation = sensorToDeviceRotation(mCharacteristics, deviceRotation, getRotationAdjustment()); @@ -1057,22 +1059,18 @@ public class Camera2RawFragment extends Fragment } // Find the best preview size for these view dimensions and configured JPEG size. + final Size previewSize = chooseOptimalSize(map.getOutputSizes(SurfaceTexture.class), rotatedViewWidth, rotatedViewHeight, maxPreviewWidth, maxPreviewHeight, largestJpeg); - mMessageHandler.postDelayed(new Runnable() { - @Override - public void run() { - if (swappedDimensions) { - mTextureView.setAspectRatio( - previewSize.getHeight(), previewSize.getWidth()); - } else { - mTextureView.setAspectRatio( - previewSize.getWidth(), previewSize.getHeight()); - } - } - }, 0); + if (swappedDimensions) { + mTextureView.setAspectRatio( + previewSize.getHeight(), previewSize.getWidth()); + } else { + mTextureView.setAspectRatio( + previewSize.getWidth(), previewSize.getHeight()); + }