根据纹理视图的大小自动调整大小的camera2预览

问题描述

我正在为我的项目使用camera2 API。预览是在Google推荐的AutofitTextureView上设置的。
但是使用他们的代码,当textureview填满整个屏幕时,预览就被拉伸了。
因此,我找到了一个stackoverflow答案,并将代码编辑为此:

AutoFitTextureView.java:

package com.example.android.camera2basic;
    
    import android.content.Context;
    import android.util.AttributeSet;
    import android.view.TextureView;
    
    public class AutoFitTextureView extends TextureView {
    
        private int mRatioWidth = 0;
        private int mRatioHeight = 0;
    
        public AutoFitTextureView(Context context) {
            this(context,null);
        }
    
        public AutoFitTextureView(Context context,AttributeSet attrs) {
            this(context,attrs,0);
        }
    
        public AutoFitTextureView(Context context,AttributeSet attrs,int defStyle) {
            super(context,defStyle);
        }
    
        public void setAspectRatio(int width,int height) {
            if (width < 0 || height < 0) {
                throw new IllegalArgumentException("Size cannot be negative.");
            }
            mRatioWidth = width;
            mRatioHeight = height;
            requestLayout();
        }
    
        @Override
        protected void onMeasure(int widthMeasureSpec,int heightMeasureSpec) {
            super.onMeasure(widthMeasureSpec,heightMeasureSpec);
            int width = MeasureSpec.getSize(widthMeasureSpec);
            int height = MeasureSpec.getSize(heightMeasureSpec);
            if (0 == mRatioWidth || 0 == mRatioHeight) {
                setMeasuredDimension(width,height);
            } else {
    
                #This is the line that I have changed:
                if (width > height * mRatioWidth / mRatioHeight) {
    
                    setMeasuredDimension(width,width * mRatioHeight / mRatioWidth);
                } else {
                    setMeasuredDimension(height * mRatioWidth / mRatioHeight,height);
                }
            }
        }
    
    }

使用上面的代码和此代码:

private static final int MAX_PREVIEW_WIDTH = 1920;
private static final int MAX_PREVIEW_HEIGHT = 1080;
private Size previewSize;
            int displayRotation = getWindowManager().getDefaultDisplay().getRotation();
            int mSensorOrientation = cameraCharacteristics.get(CameraCharacteristics.SENSOR_ORIENTATION);
            boolean swappedDimensions = false;
            switch (displayRotation) {
                case Surface.ROTATION_0:
                case Surface.ROTATION_180:
                    if (mSensorOrientation == 90 || mSensorOrientation == 270) {
                        swappedDimensions = true;
                    }
                    break;
                case Surface.ROTATION_90:
                case Surface.ROTATION_270:
                    if (mSensorOrientation == 0 || mSensorOrientation == 180) {
                        swappedDimensions = true;
                    }
                    break;
                default:
                    //Log.e(TAG,"Display rotation is invalid: " + displayRotation);
            }
            Point displaySize = new Point();
            getWindowManager().getDefaultDisplay().getSize(displaySize);
            int rotatedPreviewWidth = width;
            int rotatedPreviewHeight = height;
            int maxPreviewWidth = displaySize.x;
            int maxPreviewHeight = displaySize.y;
    
            if (swappedDimensions) {
                rotatedPreviewWidth = height;
                rotatedPreviewHeight = width;
                maxPreviewWidth = displaySize.y;
                maxPreviewHeight = displaySize.x;
            }
    
            if (maxPreviewWidth > MAX_PREVIEW_WIDTH) {
                maxPreviewWidth = MAX_PREVIEW_WIDTH;
            }
    
            if (maxPreviewHeight > MAX_PREVIEW_HEIGHT) {
                maxPreviewHeight = MAX_PREVIEW_HEIGHT;
            }
    
Size largest = Collections.max(Arrays.asList(map.getOutputSizes(SurfaceTexture.class)),new PrismaCamera.CompareSizesByArea());
            previewSize = chooseOptimalSize(map.getOutputSizes(SurfaceTexture.class),rotatedPreviewWidth,rotatedPreviewHeight,maxPreviewWidth,maxPreviewHeight,largest);
    
    
    public static Size chooseOptimalSize(Size[] choices,int textureViewWidth,int textureViewHeight,int maxWidth,int maxHeight,Size aspectRatio) {
    
            // Collect the supported resolutions that are at least as big as the preview Surface
            List<Size> bigEnough = new ArrayList<>();
            // Collect the supported resolutions that are smaller than the preview Surface
            List<Size> notBigEnough = new ArrayList<>();
            int w = aspectRatio.getWidth();
            int h = aspectRatio.getHeight();
            for (Size option : choices) {
                if (option.getWidth() <= maxWidth && option.getHeight() <= maxHeight &&
                        option.getHeight() == option.getWidth() * h / w) {
                    if (option.getWidth() >= textureViewWidth &&
                            option.getHeight() >= textureViewHeight) {
                        bigEnough.add(option);
                    } else {
                        notBigEnough.add(option);
                    }
                }
            }
    
            // Pick the smallest of those big enough. If there is no one big enough,pick the
            // largest of those not big enough.
            if (bigEnough.size() > 0) {
                return Collections.min(bigEnough,new CompareSizesByArea());
            } else if (notBigEnough.size() > 0) {
                return Collections.max(notBigEnough,new CompareSizesByArea());
            } else {
                Log.e(TAG,"Couldn't find any suitable preview size");
                return choices[0];
            }
        }

我能够使相机预览全屏显示,而无需拉伸预览。但是问题在于,当TextureView的宽度和高度设置为match_parent以外的值时,相机预览仍会占据整个屏幕。

例如,此:

<com.camera.AutoFitTextureView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/textureView"
        />

这:

<com.camera.AutoFitTextureView
        android:layout_width="500dp"
        android:layout_height="500dp"
        android:id="@+id/textureView"
        />

将预览设置为全屏。我想要的是,相机预览应该完全适合textureview的宽度和高度,而不会占用整个空间并填满屏幕。如何实现呢?摄像机预览应该在9:16、1:1、3:4和其他所有比例下都是完美的。
这可能吗?等待你的答复。问候。

更新: 这是我当前的布局文件:


    <?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:background="@android:color/black">

    <TextureView
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:id="@+id/textureView"
        android:layout_marginEnd="0dp"
        android:layout_marginStart="0dp"
        android:layout_marginTop="0dp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        />


</androidx.constraintlayout.widget.ConstraintLayout>

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...