【问题标题】:Camera 2 Preview is stretched相机 2 预览被拉伸
【发布时间】:2019-05-14 06:14:54
【问题描述】:

我目前正在使用 Camera2 API 制作一个 android 应用程序。我遇到了 AutoFitTextureView 的问题,图像显示为拉伸。 我的 AutoFitTextureView 代码如下

public class AutoFitTextureView extends TextureView {
private int mRatioWidth = 0;
private int mRatioHeight = 0;
private boolean mWithMargin = false;
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, attrs, 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 {
        if (width < height * mRatioWidth / mRatioHeight) {
            setMeasuredDimension(width, width * mRatioHeight / mRatioWidth);
        } else {
            setMeasuredDimension(height * mRatioWidth / mRatioHeight, height);
        }
    }
}

XML 代码-

拉伸图像:

我正在使用的 XML 代码如下所示,预览即将拉长-

 <com.qopius.AutoFitTextureViewNew
        android:id="@+id/texture"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
         />

【问题讨论】:

  • 仍在寻找有效的答案

标签: android android-camera2 textureview


【解决方案1】:

试试这个代码

@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 {
            if (width < height * mRatioWidth / mRatioHeight) {
            if (width > height * mRatioWidth / mRatioHeight) {
                setMeasuredDimension(width, width * mRatioHeight / mRatioWidth);
            } else {
                setMeasuredDimension(height * mRatioWidth / mRatioHeight, height);
}
}

【讨论】:

  • 请同时分享 AutoFitTextureView XML
  • @Bjha 尝试将其设为 wrap_content
  • 试过了,但没有任何反应图片被拉长了
【解决方案2】:

试试这个

@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 {
        if (width > height * mRatioWidth / mRatioHeight) {
            setMeasuredDimension(width, width * mRatioHeight / mRatioWidth);
        } else {
            setMeasuredDimension(height * mRatioWidth / mRatioHeight, height);
        }
    }
}

【讨论】:

    【解决方案3】:

    您需要将 AutoFitTextureView 放置在允许其调整大小的父视图/容器中。如果父视图不允许这样做,无论您在其 onMeasure 中做什么,它都会调整纹理视图的大小。

    例如,Camera2Basic 示例将 AutoFitTextureView 放置在 RelativeLayout 中:

    https://github.com/googlesamples/android-Camera2Basic/blob/master/Application/src/main/res/layout/fragment_camera2_basic.xml

    【讨论】:

    • 我已将 AutoFitTextureView 放在 relativeLayout 中,并为其指定宽度和高度匹配父级,但仍然无法调整预览,并且似乎被挤压了
    【解决方案4】:

    1.) 你的纵向除数和除数错误地颠倒了:

    setMeasuredDimension(width, width * mRatioHeight / mRatioWidth);
    

    2.) AutoFitTextureView 示例在 onMeasure(..) 中区分纹理方向的逻辑具有误导性:

    if (width > height * mRatioWidth / mRatioHeight)
    

    虽然它在 4:3 左右(模拟器、S2、S4)运行良好,但在更大的纵横比下会失败(Essential PH-1)。 将纹理方向添加到 setAspectRatio(..) 方法可提供所需的信息。

    注意:下面的示例忽略了 180° 和 270° 的方向。

    import android.util.AttributeSet;
    class AutoFitTextureView extends TextureView {
        private int mRatioWidth = 0;
        private int mRatioHeight = 0;
        private int mTextureOrientation = 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, attrs, defStyle);
        }
        public void setAspectRatio(int width, int height, int textureOrientation) {
            if ( width < 0 || height < 0 ) {
                throw new IllegalArgumentException("Size cannot be negative.");
            }
            mRatioWidth = width;
            mRatioHeight = height;
            mTextureOrientation = textureOrientation;
            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 {
                if ( mTextureOrientation == 0 ) { // 0 == portrait, 1 == landscape
                    setMeasuredDimension(width, width * mRatioWidth/mRatioHeight);
                } else {
                    setMeasuredDimension(height * mRatioWidth/mRatioHeight, height);
                }
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2016-08-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-16
      相关资源
      最近更新 更多