Camera2 不会使用 AutoFitTextureView 显示预览

Camera2 would not show preview using AutoFitTextureView

我在使用 AutoFitTextureView 进行 camera2 预览时遇到问题。执行此代码后,布局正常显示。相机预览不会显示。我有使用相机的权限。在此之前我写了

setContentView(mTextureView)

这有效。提前谢谢你。

主要活动

public class MainActivity extends AppCompatActivity implements TextureView.SurfaceTextureListener, ActivityCompat.OnRequestPermissionsResultCallback {
private AutoFitTextureView mTextureView;
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mTextureView = new AutoFitTextureView(this);
    mTextureView.setSurfaceTextureListener(this);
}

表面纹理

 @RequiresApi(api = Build.VERSION_CODES.M)
public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
    openCamera(width, height);
}

public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
}

public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
    closeCamera();
    return true;
}

public void onSurfaceTextureUpdated(SurfaceTexture surface) {
}

AutoFitTextureView

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

}

布局

<?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">

<FrameLayout
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000" >

    <TextureView
        android:id="@+id/texture"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</FrameLayout>


<Button
    android:id="@+id/button_take_picture"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintStart_toStartOf="parent" />

布局文件中的 TextureView 不会自动成为代码中的 mTextureView。它是一个完全独立的 TextureView,您可以通过通常的 findViewById 方法获取对它的引用。

如果你想在你的布局中使用 AutoFitTextureView,你必须实际调用它,这需要更多的工作,因为你必须告诉布局系统关于 AutoFixTextureViews。

或者,您可以手动将创建的 mTextureView 添加到布局中,方法是找到 FrameLayout,然后向其添加子项。

这些都是标准的 Android UI 创建主题,而不是特定于相机的主题,因此您应该能够找到大量关于在 Android 上创建自定义视图或如何操作的资源XML 和程序化 UI 布局的混合体。

通过添加 init(){}AutoFitTextureView 的方法class 和 findViewById 到 MainActivity class。

并删除 mTextureView = new AutoFitTextureView(this); mTextureView.setSurfaceTextureListener(this);

您还必须将 XML 文件从 TextureView 更改为 "package name".AutoFitTextureView.

在此更改之前,没有通过 AutoFitTextureView 执行 TextureView 的方法。

对于 Android 的新手和无法理解的人,请查看此 Youtube 视频:https://youtu.be/sb9OEl4k9Dk

希望对您有所帮助。