简体   繁体   English

camera2捕获与预览不同的图像

[英]camera2 capture image different with preview

my capture picture output not same with my camera preview in landscape mode 我的拍摄图片输出与横向模式下的相机预览不一样

  1. before cpture cpture之前 捕获前

  2. after capture 捕获后

捕获后

whats wrong ? 怎么了 ? and whats have i do. 我该怎么办。 thanks 谢谢

This is the AutoFitTextView class which I pulled from Google sample. 这是我从Google示例中提取的AutoFitTextView类。 You can take a look at here . 您可以在这里看看。 It aims to show camera view and config the ratio base on the physical size of device. 它旨在显示摄像机视图并根据设备的物理尺寸配置比率。

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

There are 2 points in this class: 本课程有2分:

  1. You can't ensure the ratio works properly in every device . 您无法确保该比率在每个设备上都能正常运行 However, we are able to choose optimized size which is already defined in this class . 但是,我们可以选择它在这个已经定义了优化的大小
  2. This condition is wrong: if (width < height * mRatioWidth / mRatioHeight) . 此条件是错误的: if (width < height * mRatioWidth / mRatioHeight) It should be > because when width is bigger than height, we calculate and set measure dimension base on width (not height). 应该是>,因为当宽度大于高度时,我们将基于宽度(而非高度)计算并设置度量尺寸。

UPDATED 更新

If you just want every device will work properly in a particular ratio, then set hard ratio for it (for instance: 4/3) 如果您只是希望每个设备都能以特定的比率正常工作,请为其设置硬比率(例如:4/3)

You can achieve that by replacing those lines of code: 您可以通过替换以下代码行来实现:

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