[英]camera2 capture image different with preview
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分:
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.