繁体   English   中英

camera2捕获与预览不同的图像

[英]camera2 capture image different with preview

我的拍摄图片输出与横向模式下的相机预览不一样

  1. cpture之前 捕获前

  2. 捕获后

捕获后

怎么了 ? 我该怎么办。 谢谢

这是我从Google示例中提取的AutoFitTextView类。 您可以在这里看看。 它旨在显示摄像机视图并根据设备的物理尺寸配置比率。

public class AutoFitTextureView extends TextureView {

    private int mRatioWidth = 0;
    private int mRatioHeight = 0;

    // Some codes here...

    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);
            }
        }
    }
}

本课程有2分:

  1. 您无法确保该比率在每个设备上都能正常运行 但是,我们可以选择它在这个已经定义了优化的大小
  2. 此条件是错误的: if (width < height * mRatioWidth / mRatioHeight) 应该是>,因为当宽度大于高度时,我们将基于宽度(而非高度)计算并设置度量尺寸。

更新

如果您只是希望每个设备都能以特定的比率正常工作,请为其设置硬比率(例如:4/3)

您可以通过替换以下代码行来实现:

mPreviewSize = chooseOptimalSize(map.getOutputSizes(SurfaceTexture.class),
                        rotatedPreviewWidth, rotatedPreviewHeight, maxPreviewWidth,
                        maxPreviewHeight, largest);

-> previewSize = Size(4, 3)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM